why random symbol printed postfix

i don't get why im getting a random symbol printed before the result after compiling the program.

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
 #include <iostream>
#include <stack>
#include <limits>
#include <string>
using namespace std;

int main()
{
    string input;
    cout << "Enter a postfix expression: " << endl;
    getline(cin, input);
    int operand1, operand2, result;
    stack<char>operation;

    int i=0;
    while (i < input.length())
    {
        if ( isspace(input[i]) )
        {
        }
        else if ( isdigit( input[i] ))
        {
          int num = 0;

          do
          {
            num = num*10 + (input[i]-'0');
            i++;
            if ( i >= input.length())
            {

            }
          } while (isdigit(input[i]));
          operation.push(num);
        }
        else
        {
          operand2 = operation.top();
          operation.pop();
          operand1 = operation.top();
          operation.pop();
          switch(input[i])
          {
                  case '+': result=operand1 + operand2;
                  operation.push(result);
                  break;
                  case '-': result=operand1 - operand2;
                  operation.push(result);

                  break;
                  case '*': result=operand1 * operand2;
                  operation.push(result);

                  break;
                  case '/': result=operand1 / operand2;
                  operation.push(result);

                  break;
          }

        }
        i++;
    }

    while (!operation.empty())
    {
        cout << operation.top();
        operation.pop();
    }


    cout << "The result is: "<<result<<endl;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    return 0;
}
You were using a stack of characters (only one byte) and you pushed your result into the stack of characters. When you pushed your result into the stack your stack still had items in it, so you made a while loop that output what was still in the stack. Your character result was output leaving you confused.

I put my working code with change comments below.
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <stack>
#include <limits>
#include <string>
using namespace std;

int main()
{
    string input;
    cout << "Enter a postfix expression: " << endl;
    getline(cin, input);
    int operand1, operand2, result;
    stack<int>operation;  //should be an int not a char

    int i=0;
    while (i < input.length())
    {
        if ( isspace(input[i]) )
        {
        }
        else if ( isdigit( input[i] ))
        {
          int num = 0;

          do
          {
            num = num*10 + (input[i]-'0');
            i++;
            if ( i >= input.length())
            {

            }
          } while (isdigit(input[i]));
          operation.push(num);
        }
        else
        {
          operand2 = operation.top();
          operation.pop();
          operand1 = operation.top();
          operation.pop();
		  cout << operand1 << " " << operand2 << endl;

          //you don't need the result in the stack if it's already stored
          switch(input[i])
          {
                  case '+': result=operand1 + operand2;
                  break;
                  case '-': result=operand1 - operand2;

                  break;
                  case '*': result=operand1 * operand2;

                  break;
                  case '/': result=operand1 / operand2;

                  break;
          }

        }
        i++;
    }

    while (!operation.empty())
    {
        //removed cout << operation.top();
        operation.pop();
    }

    cout << "The result is: "<<result<<endl;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    return 0;
}
Topic archived. No new replies allowed.