Please help me with outputting the contents of my stack on the input line.

fixed
Last edited on
I'd suggest to introduce another string which you show on line 18 and add variable + space to (after line 19). Reset it after compute
Thanks for the tip but I'm a little confused on how I woudl go about resetting the string after "compute"?
you can use clear:

http://www.cplusplus.com/reference/string/string/clear/

or assign an empty string (x = "")
On line 18 right now I have cout << "Enter: " << variable << " ";

What this does is output the previous variable I typed before the second input. When I add variable + space after line 19 like this
1
2
3
cout << "Enter: "; 
cin >> variable; 
cout << variable << " " << endl;


I get this

Enter: 1
1
Enter:2
2
Enter:3
3

I need to be able to have the stack on the same line as my input line. They can't be on separate lines.

Thanks!


Last edited on
fixed
Last edited on
Any help? Been stuck on this for a couple of hours now :(
Don't modify the stack just for output. My suggestion was to introdurce a new variable for the output. Like so:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
int main()
{
  stack<double> rpnStack;
  string variable;
  string expression;
  
  while (true)
  {
    // collect user input
    cout << "Enter: " << expression;
    cin >> variable;
    expression += variable + " ";
    
    
    // get number
    double num;
    if(istringstream(variable) >> num) // input stream class to operate on strings
    {
      rpnStack.push(num);
    }
    
    // operator
    else if (operatorNum(variable))
    {
      compute(variable, rpnStack);
      expression.clear();
    }
    
    // when user quits "Q" or "q"
    else if(variable == "q" || "Q")
    {
      break;
    }
    
    // invalid output
    else
    {
      cout << "Try again" << endl;
    }

  }
  
}
Topic archived. No new replies allowed.