how can i loop this case, and prompt the user for for everything again, i should do more case but i can do that later i first need to know how to loop it
#include <iostream>
usingnamespace std;
int main()
{
double answer,values[2];
int count=1,method;
string respond;
cout <<" +-+-+-+-+-+-+-+-+-+-+-+-+-+ "<<endl;
cout <<" + SIMPLE CALCUTOR MENU + "<<endl;
cout <<" +++++++++++++++++++++++++++ "<<endl;
cout <<" + SELECT 1 TO ADD + "<<endl;
cout <<" + SELECT 2 TO SUBTRACT + "<<endl;
cout <<" + SELECT 3 TO MULTIPLY + "<<endl;
cout <<" + SELECT 4 TO DIVDLE + "<<endl;
cout <<" + SELECT 5 TO MODULUS + "<<endl;
cout <<" +++++++++++++++++++++++++++\n"<<endl;
cout <<"NB. THIS CALCUTOR CAN ONLY ACCEPT TWO VALUES\n"<<endl;
do
{
cout<<"PLEASE SELECT AN OPERATION FROM MENU"<<endl;
cin>>method;
if (method>5 or method<1)
{
cout<<"ERROR: NOT VALID"<<endl;
cout<<"ENTER VALID NUMBER FROM MENU"<<endl;
}
switch (method)
{
case 1:
cout<<"YOU HAVE CHOSEN TO ADD"<<endl;
while(count<=2)
{
cout<<"ENTER VALUE "<<count<<endl;
cin>>values[count];
count++;
}
answer=values[1]+values[2];
cout <<"RESULT : "<<answer<<endl;
cout <<"DO YOU WISH TO DO ANOTHER OPERATION ? "<<endl;
cout <<"Y for Yes"<<endl;
cout <<"N for No"<<endl;
cin>>respond;
break;
case 2:
cout<<"YOU HAVE CHOSEN TO SUBTRACT"<<endl;
while(count<=2)
{
cout<<"FORMAT: VALUE 1 - VALUE 2 = DIFFERANCE"<<endl;
cout<<"ENTER VALUE "<<count<<endl;
cin>>values[count];
count++;
}
answer=values[1]-values[2];
cout <<"RESULT : "<<answer<<endl;
cout <<"DO YOU WISH TO DO ANOTHER OPERATION ? "<<endl;
cout <<"Y for Yes"<<endl;
cout <<"N for No"<<endl;
cin>>respond;
break;
case 3:
cout<<"YOU CHOSEN TO MULTIPLY"<<endl;
while(count<=2)
{
cout<<"ENTER VALUE "<<count<<endl;
cin>>values[count];
count++;
}
answer=values[1]*values[2];
cout <<"RESULT : "<<answer<<endl;
cout <<"DO YOU WISH TO DO ANOTHER OPERATION ? "<<endl;
cout <<"Y for Yes"<<endl;
cout <<"N for No"<<endl;
cin>>respond;
break;
case 4:
while(count<=2)
{
cout<<"FORMAT: VALUE 1 (DIVIDEND)/ VALUE 2 (DIVISOR)"<<endl;
cout<<"ENTER VALUE "<<count<<endl;
cin>>values[count];
count++;
}
answer=values[1]/values[2];
cout <<"RESULT : "<<answer<<endl;
cout <<"DO YOU WISH TO DO ANOTHER OPERATION ? "<<endl;
cout <<"Y for Yes"<<endl;
cout <<"N for No"<<endl;
cin>>respond;
break;
}
}while(respond=="y");
return 0;
}
This is where the loop comes in, the program should ask the user to decide whether he/she wants to continue the operation. If he/she input "y", the program will prompt the user to choose the operation again. otherwise the program will terminate. i'm beginner so i dont understand anything advance
I have put your program into my compiler. There was one error:
if (method>5 or method<1)
needed to be changed to if (method>5 || method<1)
Ignore this. My compiler puked with this, but it is legal.
The code seems to work just fine. Is it that you are having trouble understanding the code? You have working code.
You should probably have a default in your switch, just in case the user enters something unexpected (I entered 'N' and it went into an infinite loop).
If you want to loop inside any of the cases, just add a loop inside any of the case statements.
When i type in yes to continue , it loops but instead of prompting for new values it uses the result and the value 1 automatically, and thanks TheIdeasMan @TheIdeasMan
i tried to fix it by looking at the code,but i dont really understand it completely. I i could do was to remove the duplication. What is really want to know is how to make the user enter new values instead of using result.
@TheIdeasMan My apologies. My compiler does not recognize the keyword, so I myself had to change it to make it work (VS 2015), but I have used the alternative operators before.
I know of alternative operators.
I would recommend getting g++ and clang ,and a different IDE, so you can compile standard compliant code. I thought VS had finally come up to date with the standard, but apparently not.
@Tom
What didn't you understand?
Do you know how to use functions yet? If so, it's a good idea to have each case call a function to do it's work.
With a function, you can get the user to enter 2 values, once only - that's all you need for all 4 functions (+-*/)