Loop not continuing in stack program

Hey guys my code is working just fine for a postfix calculator but for some reason, the code provided isn't doing its job and looping properly

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
  #include <stack>
#include <iostream>
#include<cstdlib>
#include "calc_useful.h"
using namespace std;


int main(){
    char c;
    char tmp;
    int onenum, twonum;
    int count;
    bool continu = true;
	// declare an STL stack called nums right here:
	stack <double> nums;

    cout<<"Please enter your expression:\n";

    c = cin.get(); // priming read for the sentinel loop.
   while(continu==true){
    while(c != '\n'){
 	if(isdigit(c)){
	    cin.putback(c);
	    cin>>onenum;
		nums.push(onenum);
	}
	else if(isop(c)){
		if(  !nums.empty()/* check to see if there's anyting on the stack */){
			double two = nums.top();
			nums.pop();
			double one = nums.top();
			nums.pop();//pop two  numbers from the stack
			double result = evaluate(one, two, c);// evaluate them using the evaluate from stack_useful
			cout << result << endl;
			nums.push(result);// push result onto the stack
			}
			else{
				cout<<"Error:";
				cout<<"...  "; // what did this error tell us about the user's expression?
        			return -1;
			}

		}
	
	c = cin.get(); // reading at the bottom of the sentinel loop
    } // bottom of the loop that reads a single expression from the keyboard
		if(!nums.empty())
		{
			cout << nums.top() << endl;
			break;
		} else{
			cout << "Error: No number"<< endl;
			return 0;
		}
	  // output the final result from the top of the stack
	  // but only after you check to make sure there's something on the stack
   }
    cout << "Enter another equation?(y or n)";
    cin>> tmp;
    if(tmp=='y' || tmp=='Y')
	continu=true;
    else
	continu=false;
	
    return 0;
	
}


All the called functions for the stack are fine so I haven't included them here. Why is my loop to enter another equation not working?

gdb examination has the code termination with a 0x00000000004013c7 in __tmainCRTStartup () right after my stack reinitilizes during the loop.
First, you need use consistent indentation, to even see where the loops are. Otherwise, even relatively simple code is hard to read.

When you pop the numbers on line 30 and 31, you should check before each pop to make sure the stack isn't empty.
Or, on line 28, you should check if (nums.size() >= 2) because you are popping two items, not just one.

For maintainability and readability, I suggest breaking up your program into multiple functions. Your main loop can still be in main, but evaluating the expression itself can be its own function.

If feasible, you should separate user input from the logic of your expression/stack manipulation. I suggest just a single call to getline(cin, my_string) to get a line of input from the user, and you can look at each character of the string in an "evaluate_expression(string input)" function.
Last edited on
Right, I get that the indentation is messed up (most of the code is provided and I have been instructed not to change it) and I will fix the popping loop. But why does the loop for the repeating equation not work? I'm not sure as to why its not working.
1
2
3
4
5
6
 cout << "Enter another equation?(y or n)";
    cin>> tmp;
    if(tmp=='y' || tmp=='Y')
	continu=true;
    else
	continu=false;


Is outside of the while (continu) loop. It should be at the end.

Consider (not tried as missing functions):

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
#include <stack>
#include <iostream>
//#include "calc_useful.h"
using namespace std;

int main() {
	// declare an STL stack called nums right here:
	stack <double> nums;

	cout << "Please enter your expression:\n";

	for (bool continu {true}; continu; ) {
		for (char c {}; (c = cin.get()) != '\n'; ) {
			if (isdigit(c)) {
				double onenum {};

				cin.putback(c);
				cin >> onenum;
				nums.push(onenum);
			} else if (isop(c)) {
				if (nums.size() >= 2 /* check to see if there's anything on the stack */) {
					const auto two {nums.top()};
					nums.pop();

					const auto one {nums.top()};
					nums.pop();		//pop two  numbers from the stack

					const auto result {evaluate(one, two, c)};// evaluate them using the evaluate from stack_useful
					cout << result << '\n';
					nums.push(result);// push result onto the stack
				} else {
					cout << "Error:";
					cout << "...  "; // what did this error tell us about the user's expression?
					return -1;
				}

			}
		} // bottom of the loop that reads a single expression from the keyboard

		// output the final result from the top of the stack
		// but only after you check to make sure there's something on the stack

		if (nums.size() == 1)
			cout << nums.top() << '\n';
		else
			cout << (nums.empty() ? "Error: No number\n" : "Error: Too many numbers left\n");

		char tmp {};

		cout << "Enter another equation? (y or n)";
		cin >> tmp;
		continu = tmp == 'y' || tmp == 'Y';
	}
}

Last edited on
Topic archived. No new replies allowed.