Thread: C++ calculator with menu and do...while loops- something's wrong.

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    8

    C++ calculator with menu and do...while loops- something's wrong.

    hello!
    im new to c++ (and to this forum). i've been struggling with this hw assignment for days now and still cant seem to get it right.
    i attached my assignment.
    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    int main ()
    {
    	const int exit_out= 9;
    	int n1, n2, result, selc;
    	char sign;
    	result=0;
    	cout<<"Enter one of the following values:"<<endl;
    	cout<<setw(10)<<left<<"1"<<"Add two numbers"<<endl;
    	cout<<setw(10)<<left<<"2"<<"Subtract two numbers"<<endl;
    	cout<<setw(10)<<left<<"3"<<"Multiply two numbers"<<endl;
    	cout<<setw(10)<<left<<"4"<<"Find the modulus of dividing two numbers"<<endl;
    	cout<<setw(10)<<left<<"9"<<"Quit program"<<endl<<endl;
    	cout<<setw(4)<<left<<" "<<"Enter selection now: ";
    	cin>> selc;
    	cout<<endl;
    	do
    	{
    
    		cout<<"Enter the first number: ";
    		cin>>n1;
    		cout<<"Enter the second number: ";
    		cin>>n2;
    				switch (selc)
    				{
    					case 1: result= n1+n2;
    							sign='+';
    							break;
    					case 2: result= n1-n2;
    							sign= '-';
    							break;
    					case 3: result=n1*n2;
    							sign= '*';
    							break;
    					case 4: result=n1%n2;
    							sign= '%';
    							break;
    					default: cout<<"Invalid selection, please try again."<<endl;
    							break;
    				}
    		cout<<"The result of "<<n1<<sign<<n2<<" is "<<result<<endl<<endl;
    		cout<<"Enter one of the following values:"<<endl;
    		cout<<setw(10)<<left<<"1"<<"Add two numbers"<<endl;
    		cout<<setw(10)<<left<<"2"<<"Subtract two numbers"<<endl;
    		cout<<setw(10)<<left<<"3"<<"Multiply two numbers"<<endl;
    		cout<<setw(10)<<left<<"4"<<"Find the modulus of dividing two numbers"<<endl;
    		cout<<setw(10)<<left<<"9"<<"Quit program"<<endl<<endl;
    		cout<<setw(4)<<left<<" "<<"Enter selection now: ";
    		cin>> selc;
    		cout<<endl;
    		cout<<"Enter the first number: ";
    		cin>>n1;
    		cout<<"Enter the second number: ";
    		cin>>n2;
    	}
    			
    	while (selc != exit_out);
    
    	return 0;
    }
    what i cannot seem to get right is how to make the program exit without having it ask me again to enter my two values and then exit.
    in the switch function, the default does show up right, but it attempts to continue asking for more values and tries to give a result, when i do not want it to.
    thanks for any help

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    so after you entered selc - check whether its value is 9 - and exit program

    use if statement
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User UltraKing227's Avatar
    Join Date
    Jan 2010
    Location
    USA, New york
    Posts
    123
    you can quit a program like the following:

    Code:
     exit(1);

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    8
    oh thanks you guys!
    for exiting the program i used
    Code:
    return 1;
    and that worked fine (we were taught that in lecture)

    i didnt think that it would be as simple as an if statement that my program needed, i thought the while loop was more than enough to tell the program when to stop- oh well guess i was wrong

    thanks again!

Popular pages Recent additions subscribe to a feed