Help with homework
Oct 17, 2013 at 2:26am UTC
This program is supposed to be a math tutor using functions. I could also use arrays.My problem is when I run the program it shows the menu, I enter 1 for addition (That is all I setup so far) I enter the correct answer, it tells me very good and goes back to the menu. It is supposed to ask another question.
Can someone tell me what I did wrong.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
// This program is a math tutor
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std;
void getMenu ();
int addition (int , int );
void getRandon ( int &, int &);
int main()
{
int num1, num2;
int choice;
int answer;
do
{
getMenu();
cin >> choice;
while (choice < 1 || choice > 6)
{
cout << "Enter a choice in the range of 1 - 6: \n" ;
cin >> choice;
}
getRandon (num1, num2);
switch (choice)
{
case 1: answer = addition (num1, num2);
break ;
}
} while (choice != 6 && answer != -1);
return 0;
}
void getMenu()
{
cout << "Menu \n\n"
<< "1. Addition \n"
<< "2. Subtraction \n"
<< "3. Multiplication \n"
<< "4. Division \n"
<< "5. Modulus \n"
<< "6. Quit \n\n"
<< "Enter your choice: " ;
}
int addition (int n1, int n2)
{
int answer;
int countCorrect = 0;
int countIncorrect = 0;
cout << "What is " << n1 << " + " << n2 << endl;
cout << "Enter your answer or (-1 to return to the menu.)\n" ;
cin >> answer;
if (answer != -1)
{
if (answer != n1 + n2)
{
cout << "No. Try again!\a\n" ;
cin >> answer;
}
else
{
cout << "Very Good \n" ;
}
}
return answer;
}
void getRandon (int &rNum1, int &rNum2)
{
srand(time(0));
rNum1 = 1 + rand() % 50;
rNum2 = 1 + rand() % 50;
}
Oct 17, 2013 at 3:44am UTC
remove getMenu from the do-while loop in main
Oct 18, 2013 at 12:19am UTC
I have changed the code to this. The problem is that when I enter -1 the program does not go back to the menu.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
// This program is a math tutor
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
using namespace std;
void getMenu (int &);
void addition (int , int );
void getRandon ( int &, int &);
int main()
{
int num1, num2;
int choice;
getMenu(choice);
do
{
getRandon (num1, num2);
switch (choice)
{
case 1: addition(num1,num2);
break ;
}
} while (choice != 6);
return 0;
}
void getMenu(int &rchoice)
{
cout << "Menu \n\n"
<< "1. Addition \n"
<< "2. Subtraction \n"
<< "3. Multiplication \n"
<< "4. Division \n"
<< "5. Modulus \n"
<< "6. Quit \n\n"
<< "Enter your choice: " ;
cin >> rchoice;
while (rchoice < 1 || rchoice > 6)
{
cout << "Enter a choice in the range of 1 - 6: \n" ;
cin >> rchoice;
}
}
void addition (int n1, int n2)
{
int answer;
int countCorrect = 0;
int countIncorrect = 0;
//do
//{
cout << "What is " << n1 << " + " << n2 << endl;
cout << "Enter your answer or (-1 to return to the menu.)\n" ;
cin >> answer;
if (answer != -1)
{
if (answer != n1 + n2)
{
cout << "No. Try again!\n" ;
cin >> answer;
}
else
{
cout << "Very Good \n" ;
}
}
//} while (answer != -1);
}
void getRandon (int &rNum1, int &rNum2)
{
srand(time(0));
rNum1 = 1 + rand() % 50;
rNum2 = 1 + rand() % 50;
}
Topic archived. No new replies allowed.