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