#My cin statement wont work and keeps repeating.

141 messages · Page 1 of 1 (latest)

keen zodiac
#

Heres the question and expected output:

Heres the error im getting:

Heres my code:

// list of libraries
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
#include "STACKPAC.h"
using namespace std;

//all function prototypes

//main function
int main()
{
    int value, x, y, r;
    int tom, jerry, mickey;

    Stack<int, 10> p;
    p.clearStack();
    cout << "Enter a postfix expression: ";

    string id;
    while (cin >> id, id != "$")
    {
        //chech whether the input is a number 
        //. If it is, then convert it to its 
        //numeric value
        if (isdigit(id[0]))
        {
            value = stoi(id);
            p.pushStack(value);
        }

        if (id == "tom")
        {
            cout << "Enter the value of tom: "; cin >> tom;
            p.pushStack(tom);
        }
        else if (id == "jerry")
        {
            cout << "Enter the value of jerry: "; cin >> jerry;
            p.pushStack(jerry);
        }
        else if (id == "mickey")
        {
            cout << "Enter the value of mickey: "; cin >> mickey;
            p.pushStack(mickey);
        }
        else if (id == "+")
        {
            x = p.popStack();
            y = p.popStack();
            r = y + x;
            p.pushStack(r);
        }
        else //if (id=="*")
        {
            x = p.popStack();
            y = p.popStack();
            r = y * x;
            p.pushStack(r);
        }
    }
    //pop the stck to see the result
    cout << "\tValue of the expression is: " << p.popStack() << endl;
    system("pause");
    return 0;
}
zealous dawnBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For more information use !howto ask.

keen zodiac
#

Heres the question and expected output:

#

Heres the error I get when running my code:

faint pine
#

didn't look through the whole thing

#

but sometimes you have to clear the buffer for cin

#

might be less of a hassle to just do a getline

#

and break the input up yourself

keen zodiac
faint pine
#

delimit by space

#

anything that's not a digit or operator has it's own cin, like you were doing

#

then every time you run into an operator you pop out the two operands

#

on which you're going to perform the operation

#

it is saying explicitly tom jerry mickey

#

but it should be able to be arbitrary

#

one temp string

#

that you convert into a value and push onto the stack

#

in that case its

stack:
7
7, 10
+ pulls them out and pushes 17 onto stack
17, 2, 23 on the stack 
* pulls out last two, 2 and 23 and multiplies them for 46
17, 46 on the stack
- pulls them both out and you get -29
keen zodiac
#

is it possible for u to do it by editing my code?

faint pine
#
  string x = "who are you $";
  while (x.find(" ") != string::npos) {
    int index = x.find(" ");
    string temp = x.substr(0, index);
    cout << temp << endl;
    x.erase(0, index + 1);
  }
#

this would pull out

#

who

#

are

#

you

#

and outside of the while loop

#

all that would be left is $

#

so now instead of x = "hardcoded string"

#

you would do

#

getline(cin, x)

#

and you push/pop and operate on the stack as you extract each delimited piece of the string

#

a delimiter is just how you split something

#

in our case " " is the delimiter

#

a single space

keen zodiac
#

so instead of using cin >> tom i use getline(cin, tom)?

faint pine
#

i'd make it a string

#

not an int

#

getline(cin, stringvariable)

keen zodiac
#

ok and then how do i make the string an int varuable?

faint pine
#

in that code snippet i sent

keen zodiac
#

using stoi?

faint pine
#

as you extract each piece of the string

#

if it's not a digit

#

or an operator like + - * /

#

or w/e

#

it means it's going to be a variable

#

so you'll have an if branch

#

if this piece of the string isn't a number or operator

#

ask them to cin >> an int

#

and push that onto the stack

keen zodiac
#

oh so u want me to use that instead of while(cin >> id, id != $)

faint pine
#

yeah i think that there is just some weird stuff around cin/cout

#

where you get \n caught in the buffer

#

so if you don't cin.ignore it

#

at the right time

#

you'll get those annoying loops

keen zodiac
#

thats what i was thinking

#

so is there a way to cin.ignore() it>

faint pine
#

you might even want to do a cin ignore

#

after the getline

keen zodiac
#

i tried it but i still get the same error

#

ok

faint pine
#

the problem is

#

if you cin ignore immediately

#

you're only extracting one piece of the string

#

right

#

so if it's tom jerry bla bla

#

and you pull in tom

#

then cin ignore

#

you're going to erase the rest of the buffer

#

there's probably a way to do it your way

#

i'm just too lazy to think about it

keen zodiac
#

so for
string x = "who are you $";
while (x.find(" ") != string::npos) {
int index = x.find(" ");
string temp = x.substr(0, index);
cout << temp << endl;
x.erase(0, index + 1);
}

#

it finds the spaces in the string then gets the first part then erases that then gets the next part right?

faint pine
#

yes

#

so you load up everything up to the space into temp

#

then you would check what temp is

keen zodiac
#

so where would i put my parrt of the code in?

faint pine
#

so every time you pull out a number i.e.,

#

every part of the string is a digit

#

you stoi it

#

and push it onto the stack

#

every time you get an operator

#

you pop out the last two things on the stack and apply the operation

#

if you get anything else

#

that means its one of those variables

#

like tom jerry mickey w/e

#

so you would say

#

cout << "Enter a value for " << temp << ": " << endl;
cin >> numToPushToStack (obviously make sure theyre giving you a valid int)

#

now technically since you aren't going to be cin'ing anymore strings

#

i think you may not even need a cin ignore

#

and if you do

#

it's just after the first getline

#

it doesn't have to be tom

#

so first you check if its a number

#

then if it's an operator

#

anything else is a "variable"

#

that the user has to put in

keen zodiac
#

string x = "who are you $";
while (x.find(" ") != string::npos) {
int index = x.find(" ");
string temp = x.substr(0, index);
if (temp == "tom")
{
cout << "Enter value of tom: "; cin >> tom;
p.pushStack(tom);
}
x.erase(0, index + 1);
}

faint pine
#

so they can pick their own names

keen zodiac
#

would this work?

#

for only tom

faint pine
#

pseudo code

#

one sec

#
while x find " " not npos bla bla
  if tempstring is number
    stoi and push on stack
  else if tempstring is operator
    pull out last two numbers in stack and apply operator, and push the result onto stack
  else
    tempint;
    cout << "Enter value for << temp << ": " << endl;
    cin >> tempint;
    push tempint onto stack
#

so in this case

#

if the string was

#
10 20 + banana * $

----------
10 goes on stack
20 goes on stack
+ pulls them both out adds them to 30 and adds it to stack (now the stack just has a 30 in it
your code now sees banana, which isn't a number or operator
you say "Enter a value for banana: " 
cin >> tempint
push tempint on stack
* pulls out whatever number they put in and 30 and multiplies them giving you your final result
#

in this case, you don't have to use words like "tom" or "jerry" or "banana" explicilty

#

the user picks whatever dumb names they want

#

when they put in the postfix expression

#

does that make sense?

#

now when you exit the while loop, the last thing you pop out of the stack should be the final result

keen zodiac
#

kinda but not really

#

cus im trying to do it as close as i can to the example i was given

#

which is similar to the initial clode i sent

faint pine
#

yeah but it says "suppose an expression was given"

#

then it gives sample input

#

it feels like the assumption is

#

that you don't necessarily want to just handle that specific case

#

with tom jerry and mickey

keen zodiac
#

thats what i t hought at first too

#

but its actually specifically tom and jerry and mickey

#

cus he wants me to somehow cin if tom is in the expression

#

otherwise dont ask for tom

#

it makes more sense that way cus its similar to the examples given

faint pine
#

i mean, you can do it that way then if you want

keen zodiac
#

do u know how to fix the issue im having with my way?

keen zodiac
#
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
#include "STACKPAC.h"
using namespace std;

//all function prototypes

//main function
int main()
{
    int value, x, y, r;
    int tom, jerry, mickey;
    string id;

    Stack<int, 10> p;
    p.clearStack();
    cout << "Enter a postfix expression: ";
    cin >> id;
    while (id.find(" ") != -1) 
    {
        int index = id.find(" ");
        string temp = id.substr(0, index);
        cout << temp << endl;
        id.erase(0, index + 1);
    }
    cout << "\tValue of the expression is: " << p.popStack() << endl;
    system("pause");
    return 0;
}

@faint pine why doesnt this work?

#

i tested it to print out temp cout << temp

faint pine
#

what is it doing

#

also you're doing cin and not getline

#

@keen zodiac

keen zodiac
#
#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>
#include "STACKPAC.h"
using namespace std;

//all function prototypes

//main function
int main()
{
    int value, x, y, r;
    int tom, jerry, mickey;
    string id;

    Stack<int, 10> p;
    p.clearStack();
    cout << "Enter a postfix expression: ";
    getline(cin, id);
    while (id.find(" ") != -1) 
    {
        int index = id.find(" ");
        string temp = id.substr(0, index);
        if (isdigit(temp[0]))
        {
            value = stoi(temp);
            p.pushStack(value);
        }
        if (temp == "tom")
        {
            cout << "Enter the value of tom: "; cin >> tom;
            p.pushStack(tom);
        }
        else if (temp == "jerry")
        {
            cout << "Enter the value of jerry: "; cin >> jerry;
            p.pushStack(jerry);
        }
        else if (temp == "mickey")
        {
            cout << "Enter the value of mickey: "; cin >> mickey;
            p.pushStack(mickey);
        }
        else if (temp == "+")
        {
            x = p.popStack();
            y = p.popStack();
            r = y + x;
            p.pushStack(r);
        }
        else //if (id=="*")
        {
            x = p.popStack();
            y = p.popStack();
            r = y * x;
            p.pushStack(r);
        }
        id.erase(0, index + 1);
    }
    cout << "\tValue of the expression is: " << p.popStack() << endl;
    system("pause");
    return 0;
}
#

@faint pine i replaced cin with getline and tested it and it got the correct values for temp

#

now this is the full code and it doesnt output the right value

#

it gives like -1294381347

#

tom jerry + mickey 23 * - $

faint pine
#

you're not handling -

wind yarrow
zealous dawnBOT
#

This question thread is being automatically closed. If your question is not answered feel free to bump the post or re-ask. Take a look at !howto ask for tips on improving your question.