I was wondering what the best way to get back to the main function from the called functions: int addition() and int subtraction(). They are called from the switch(choice) and i just want the switch to replay itself after the user does an addition or subtraction problem.
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <iomanip>
#include <string>
usingnamespace std;
int menu();
int addition();
int subtraction();
int main()
{
int choice;
int score = 0;
choice = menu();
do
{
switch (choice){
case 0: cout << score; break;
case 1: addition(); break;
case 2: subtraction(); break;
default : cout << "Error in input\n\n"; break;
}
}
while (choice != 0);
}
int menu() //main menu that gets called by the switch
{
int MenuChoice;
cout << "Enter 1 for addition\n";
cout << "Enter 2 for Subtraction\n";
cout << "Enter 0 to quit\n";
cout << "-----------------\n";
cout << "Choice: ";
cin >> MenuChoice; // user input
return MenuChoice; //return the choice to choice to select a case
}
int addition() //function that is called by the users selection
{
int score = 0;
int answer;
int random1, random2;
srand(time(NULL));
random1 = 1+(rand()%99); // from 1-99
random2 = 1+(rand()%99); // from 1-99
cout << "\n" << random1 << " + " << random2 << " = "; //code to format the random math problem
cin >> answer; // user input of answer
{
if (answer == (random1+random2)){
cout << "Congratulations\n\n";
++score;
cout << score;} //if user is correct than the score is incremented
else{
cout << "Incorrect\n\n";} //if not than the score is not incremented
return 0;
}
}
int subtraction() // follows same model as previous function, but instead subtraction.
{
int answer;
int random1, random2;
int score = 0;
srand(time(NULL));
random1 = 1+(rand()%99); // from 1-99
random2 = 1+(rand()%99); // from 1-99
cout << "\n" << random1 << " - " << random2 << " = ";
cin >> answer;
if (answer == (random1-random2)){
cout << "Congratulations\n\n";
++score;
cout << score;}
else{
cout << "Incorrect\n\n";}
return 0;
}
but thing is i cant even get out of the addition or subtraction as is. The program runs and then u select the case and then it will just keep throwing math problems at you forever and ever. wouldnt i need some sort of statement to get me out of the function, within the function, before i use your advice?
Further investigate why I posted what I posted above. Meaning, what does your menu function do. Neither your subtraction or addition functions loop. So they are not the problem. Look at your existing loop. Look at the condition at which it is true and false. Know why I suggest what I suggested with everything in consideration. You will learn something and it will stick with you from this point on.
If then you still cant figure it out come back and ask.
Huge hint. I'm guessing the Quit option works as intended?
That worked perfectly thank you! One final question: How can I get the score counter to work properly. At the moment it just prints 1 every time you answer a question correctly. Any advice?
When you go out of scope( the function ends ) you lose the data stored within the variable. Either pass the variable as a reference to the function, or declare the variable as static. (:
EDIT:
You'd be better off passing to the functions by reference, because you'd be keeping two seperate scores, one for each function.