calculator program error

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 ...

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    127

    calculator program error

    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?
    Attached Files Attached Files

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    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

  3. #3
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I do not see number ever being assigned a value. So 0 would be the correct result. Always.

  4. #4
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    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;
    }

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    127
    The second switch case for operation adds or subracts the nomial to the result.
    P.S Sorry. I thought posting might be to long...

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    127
    Sorry, but I'm still not seeing what I did wrong.

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    You have two variables: number and nominal. Pick one.

    Also, this logic is wrong:
    Code:
    while(operation!='+'||operation!='-')
    If operation is '+', then the second half of your logic will be true. You should use AND, not OR.
    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!

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Let's examine your logic in small pieces, right?
    Code:
    		getline(cin,sequation);
    		equation<<sequation;
    You read in the equation.

    Code:
    		while(!equation.end)
    		{
    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:
    			nomial=0;
    You set the subresult for multiplicative operations to 0.

    Code:
    			while(operation!='+'||operation!='-')
    			{
    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.
    Also, because operation is only given a value inside this loop, it will be uninitialized the first time around. Undefined behaviour.

    Code:
    				equation>>number>>operation;
    You parse a single number and operation.

    Code:
    				switch(operation)
    				{
    					case '*':
    					nomial*=number;
    					break;
    					case '/':
    					if(number!=0)
    					nomial/=number;
    					break;
    				}
    If it's multiplication or division, you perform it on the subresult.
    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.

    Code:
    			}
    			switch(operation)
    			{
    				case '+':
    				result+=nomial;
    				break;
    				case '-':
    				result-=nomial;
    				break;
    			}
    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.
    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.
    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;
    Let's start simple:
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. An error is driving me nuts!
    By ulillillia in forum C Programming
    Replies: 5
    Last Post: 04-04-2009, 09:15 PM
  3. Making C DLL using MSVC++ 2005
    By chico1st in forum C Programming
    Replies: 26
    Last Post: 05-28-2008, 01:17 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 09:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 12:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21