I wrote wrote a calculator program just to try out the sstream, which I've never used before. I keep on getting 0 for a result no matter what I type. I've attached the code. Any help here?
This is a discussion on calculator program error within the C++ Programming forums, part of the General Programming Boards category; I wrote wrote a calculator program just to try out the sstream, which I've never used before. I keep on ...
I wrote wrote a calculator program just to try out the sstream, which I've never used before. I keep on getting 0 for a result no matter what I type. I've attached the code. Any help here?
The program is 749 bytes long. Just paste it into a code block instead of attaching it.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
I do not see number ever being assigned a value. So 0 would be the correct result. Always.
Her code
Code:#include <iostream> #include <sstream> #include <string> using namespace std; int main() { stringstream equation; int result=0; int nomial; int number; char operation; string sequation; while(cin) { getline(cin,sequation); equation<<sequation; while(!equation.end) { nomial=0; while(operation!='+'||operation!='-') { equation>>number>>operation; switch(operation) { case '*': nomial*=number; break; case '/': if(number!=0) nomial/=number; break; } } switch(operation) { case '+': result+=nomial; break; case '-': result-=nomial; break; } } cout<<result<<endl; result=0; } return 0; }
The second switch case for operation adds or subracts the nomial to the result.
P.S Sorry. I thought posting might be to long...
Sorry, but I'm still not seeing what I did wrong.
You have two variables: number and nominal. Pick one.
Also, this logic is wrong:
If operation is '+', then the second half of your logic will be true. You should use AND, not OR.Code:while(operation!='+'||operation!='-')
Mac and Windows cross platform programmer. Ruby lover.
Quote of the Day
12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.
Amen brother!
Let's examine your logic in small pieces, right?
You read in the equation.Code:getline(cin,sequation); equation<<sequation;
You loop while the logical negation of the ios::end (a constant flag indicating "seek from end" in the seekg and seekp calls) is true - this is never the case. Error number one. You want while(equation).Code:while(!equation.end) {
You set the subresult for multiplicative operations to 0.Code:nomial=0;
You loop while the operation is not '+' or not '-'. This is trivially always true, since it would only be false if operation was both '+' and '-', which is impossible.Code:while(operation!='+'||operation!='-') {
Also, because operation is only given a value inside this loop, it will be uninitialized the first time around. Undefined behaviour.
You parse a single number and operation.Code:equation>>number>>operation;
If it's multiplication or division, you perform it on the subresult.Code:switch(operation) { case '*': nomial*=number; break; case '/': if(number!=0) nomial/=number; break; }
Of course, since the subresult starts out as 0, any multiplicative operation yields 0 again, so this is all a big no-op. Error number 4.
Oh, and you should check for division by zero, since the language doesn't do it for you.
Now, if the loop ends (i.e. operation is '+' or '-' if we correct your code for intention), you perform the additive operation using the subresult. Incidently, this means you throw away the number you just parsed.Code:} switch(operation) { case '+': result+=nomial; break; case '-': result-=nomial; break; }
Interestingly, since operation keeps its value, which must be '+' or '-' at this point, the inner loop (which is the only place operation might change its value) is never entered again. Oops.
}
cout<<result<<endl;[/code]
Finally, you write the result.
OK, now let's look at the big picture. Unfortunately, the whole logic is completely broken. I'll look at what the code does for some test strings, assuming a few small things are corrected. I'll post the code again with numbered lines for easy reference.
Let's start simple:Code:1 getline(cin,sequation); 2 equation<<sequation; 3 while(equation) 4 { 5 nomial=0; 6 while(operation!='+'&&operation!='-') 7 { 8 equation>>number>>operation; 9 switch(operation) 10 { 11 case '*': 12 nomial*=number; 13 break; 14 case '/': 15 if(number!=0) 16 nomial/=number; 17 break; 18 } 19 } 20 switch(operation) 21 { 22 case '+': 23 result+=nomial; 24 break; 25 case '-': 26 result-=nomial; 27 break; 28 } 29 } 30 cout<<result<<endl;
1 + 2
OK, line 8 is interesting.
nomial = 0
number = 1
operation = +
The switch on line 9 skips completely. The while on line 6 is terminated.
We get to line 20. nomial (0) is added to result (0), yielding 0.
Restart. The while loop on line 6 is skipped, since operation is still '+'.
We get to line 20. nomial (0) is added to result (0), yielding 0.
And so on, forever.
Another simple input:
1 * 2
After line 8, we have:
nomial = 0
number = 1
operation = *
The switch on line 9 gets something this time. nomial (0) is multiplied by number (1), yielding 0.
Restart the while loop. Line 8 executes again.
nomial = 0
number = 2
operation cannot be parsed for lack of input and retains the value *.
equation goes into end of file and failure state.
The switch on line 9 hits a target. nomial (0) is multiplied by number (2), yielding 0.
Restart the while loop. Line 8 executes again.
Since equation is not in good state, it doesn't read anything. All values remain unchanged.
Oops, looks like another infinite loop.
So, simply put, if line 3 hadn't stopped you from ever parsing the equations, your program would have simply hung in one endless loop or another.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law