This example is a very basic and simple program.
I want to be able to exit from a function whenever the user wants to.
In the add() function i'm able to exit on the
cin>>NumA;
line by typing 9999 and it will return me to the menu. However i would only be able to exit at this point, so if i wanted to exit at any time i'd have to add the
if (NumA == 9999)
{
return;
}
throught all of the program. I want to be able to exit at any time inside of the function, even better if it can be done by pressing a key, like backspace. I suppose there is a better way to implement this, how can i do this? Maybe another function :^)
void add()
{
cout << "Addition" << endl;
cout << "Number A: ";
int NumA;
cin>>NumA;
if (NumA == 9999)
{
return;
}
cout << "Number B: ";
int NumB;
cin>>NumB;
int Result = NumA + NumB;
cout << NumA << " + " << NumB << " = " << Result<<endl;
}
int main()
{
int Op;
do
{
cout << "Main" << endl;
cout << "1) Add" << endl;
cout << "2) Another function call" << endl;
cout << "3) Yet another function call" << endl;
cout << "n) ..." << endl;
cout << "0, to exit" << endl;
cin>>Op;
switch (Op)
{
case 1:
{
add();
}
default:
{
break;
}
}
}
while (Op != 0);
}