Thread: Need some help with Calculator program

  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
    Chappell Hill, Texas
    Posts
    2,332
    Why is FirstNumber equal to zero on divide an error? It's a valid operation.
    Mainframe assembler programmer by trade. C coder when I can.

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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