Need some help with Calculator program

This is a discussion on Need some help with Calculator program within the C++ Programming forums, part of the General Programming Boards category; Here a demo of the program. I trying to figure out how to make it take more then 2 number ...

  1. #1
    Registered User Sshakey6791's Avatar
    Join Date
    Nov 2008
    Location
    -
    Posts
    57

    Exclamation Need some help with Calculator program

    Here a demo of the program. I trying to figure out how to make it take more then 2 number without added more variable. I know you have to use some kind of loop just having a little bit of trouble figuring it out.... THANKS.....

    Code:
    #include <iostream>
    #include <cmath>
    #include <stdio.h>
    #include <windows.h>
    
    
    using namespace std;
    
    void ErrorMessage();
    
    
    
    int main() {
    
    	float FirstNumber;
    	float SecNumber;
    	char Sign;
    	float Equal;
    
    	cout << endl;
    	cout << "\t\t\t\tEnter Math Problem.\n";
    
    	while ( true ) {
    
    
    	
    		cout << ">";
    		cin >> FirstNumber >> Sign >> SecNumber;
    
    
    		switch ( Sign ) {
    
    			case '+':
    
    				Equal = ( FirstNumber + SecNumber );
    					
    				cout << endl;
    
    				cout << FirstNumber << " " << Sign << " " << SecNumber << " = " << Equal << "\n";
    
    				cout << endl;
    
    				break;
    
    			case '-':
    
    				Equal = ( FirstNumber - SecNumber );
    
    				cout << endl;
    
    				cout << FirstNumber << " " << Sign << " " << SecNumber << " = " << Equal << "\n";
    
    				cout << endl;
    
    				break;
    
    			case '*':
    
    				Equal = ( FirstNumber * SecNumber ); 
    
    				cout << endl;
    
    				cout << FirstNumber << " " << Sign << " " << SecNumber << " = " << Equal << "\n";
    
    				cout << endl;
    
    				break;
    
    			case '/':
    					
    				if  ( ( FirstNumber == 0 ) || ( SecNumber == 0 ) ) {
    						
    					ErrorMessage();
    
    					continue;
    					
    				} else {
    
    						Equal = ( FirstNumber / SecNumber );
    		
    						cout << endl;
    
    						cout << FirstNumber << " " << Sign << " " << SecNumber << " = " << Equal << "\n";
    
    						cout << endl;
    
    						break;
    				}
    
    			default: 
    
    				ErrorMessage();
    
    				cout << "You can't do that!\n\n";
    
    				continue;
    
    		}
    
    	}
    
    	
    	
    	system("pause");
    
    	return(0);
    
    
    }
    
    
    
    void ErrorMessage() {
    
    	cout << endl;
    
    	cout << "ERROR!!! \n";
    
    	cout << endl;
    
    }
    "Blood you have thirsted for -- now, drink your own!"
    (Dante)

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Why is FirstNumber equal to zero on divide an error? It's a valid 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!

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,758
    Well if you were doing subtraction for instance,

    a - b - c

    you would first subtract b from a and then finally c from the difference. So your loop control would be how many terms there is going to be. In this case, there are two, as you would read a and b and then c.

    It may be helpful to use a sentinel value like zero, to signal that the last term is complete.

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    If you have 1 + 2 * 3 that won't work. It is more complicated. If not, if you only + and - or * and / then you will have:
    firstNumber operator OtherNumber operator OtherNumber .... OtherNumber = Result

    So you read the first number, save it in TempResult and then in a loop you do:
    1) read operator
    2) read OtherNumber
    3) TempResult = TempResult operator OtherNumber

    And you are done.

Popular pages Recent additions subscribe to a feed

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