#include <stack>
#include <iostream>
#include<cstdlib>
#include "calc_useful.h"
usingnamespace 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);
}
elseif(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.
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.
#include <stack>
#include <iostream>
//#include "calc_useful.h"
usingnamespace 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);
} elseif (isop(c)) {
if (nums.size() >= 2 /* check to see if there's anything on the stack */) {
constauto two {nums.top()};
nums.pop();
constauto one {nums.top()};
nums.pop(); //pop two numbers from the stack
constauto 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';
}
}