Continued Homework help

So i posted a question earlier to this problem. I had to write a program that randomly chooses two positive one-digit integers for a way to help students practice multiplication. I rewrote the code and everything works now. My problem is, how do I make the multiplication problem repeat itself if the student gets it 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
  #include "Program3.h"
using namespace std;

void correct_response();
void incorrect_response(int answer);

int main ()
    {
        int numbers[3];
        int answer = 0; /* USER INPUT */
        int keepPlaying = 1;
        srand (time(0));
    
       /* Initiate checking for correct answer */
       while (keepPlaying == 1) /* WHILE USER HASN'T ENTERED 1 */
       {
          numbers[0] = rand() % 9 + 1; /* load the first random number into numbers[0] */
          numbers[1] = rand() % 9 + 1; /* load the second into numbers[1] */
          numbers[2] = numbers[0] * numbers[1]; /* load the answer into numbers[2] */
          cout << "Correctly answer the following" << endl; /* PROMPT */
          cout << "\n\n""How much is " << numbers[0] << " * " << numbers[1] << "?""" << endl;
          cin >> answer;
          if (answer == numbers[2]) /* check against the numbers array */
          {
              correct_response();
             
          }
          else
          {
              incorrect_response(numbers[2]);
          }
          
           cout << "\nDo you want to play again? (enter 1 for yes or 2 for no)" << endl;
           cin >> keepPlaying;
       }
       
         return 0;
    }

void correct_response() {
    int choice = rand() % 4;
    switch (choice) {
        case 0:
            cout << "\nVery Good!" << endl;
            break;
           
        case 1:
            cout << "\nExcellent!" << endl;
            break;
           
        case 2:
            cout << "\nKeep up the good work!" << endl;
            break;
           
        case 3:
            cout << "\nNice Work!" << endl;
            break;
    }
}

void incorrect_response(int answer) {
    int choice = rand() % 4;
    switch (choice) {
        case 0:
            cout << "\nNo, please try again. ";
            break;
           
        case 1:
            cout << "\nWrong. Try once more. " << endl;
            break;
           
        case 2:
            cout << "\nDon't give up! " << endl;
            break;
           
        case 3:
            cout << "\nNo. Keep trying. " << endl;
            break;
    }
    cout << "The correct answer was " << answer << endl;
}
So I added a while and do statements and it still gives me the answer to the problem but doesn't let me try to re-answer the problem.

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
102
103
104
#include "Program3.h"
using namespace std;

void correct_response();
void incorrect_response(int answer);

int main ()
    {
        int numbers[3];
        int answer = 0; /* USER INPUT */
        int keepPlaying = 1;
        srand (time(0));
    
       /* Initiate checking for correct answer */
       while (keepPlaying == 1) /* WHILE USER HASN'T ENTERED 1 */
       {
          numbers[0] = rand() % 9 + 1; /* load the first random number into numbers[0] */
          numbers[1] = rand() % 9 + 1; /* load the second into numbers[1] */
          numbers[2] = numbers[0] * numbers[1]; /* load the answer into numbers[2] */
          cout << "Correctly answer the following" << endl; /* PROMPT */
          cout << "\n\n""How much is " << numbers[0] << " * " << numbers[1] << "?""" << endl;
          cin >> answer;
          if (answer == numbers[2]) /* check against the numbers array */
          {
              correct_response();
             
          }
          else
          {
              incorrect_response(numbers[2]);
          }
          
           cout << "\nDo you want to play again? (enter 1 for yes or 2 for no)" << endl;
           cin >> keepPlaying;
       }
       
         return 0;
    }

void correct_response() {
    int choice = rand() % 4;
    switch (choice) {
        case 0:
            cout << "\nVery Good!" << endl;
            break;
           
        case 1:
            cout << "\nExcellent!" << endl;
            break;
           
        case 2:
            cout << "\nKeep up the good work!" << endl;
            break;
           
        case 3:
            cout << "\nNice Work!" << endl;
            break;
    }
}

void incorrect_response(int answer) {
    int choice = rand() % 4;
    switch (choice) {
        case 0:
		do
		{
            cout << "\nNo, please try again. ";
			cout << "\n";
            cout << "\n\n""How much is " << numbers[0] << " * " << numbers[1] << "?""" << endl;
            cin >> answer;
		}
		while (answer == numbers[2]);
        break;
        case 1:
		do
		{
            cout << "\nWrong. Try once more. " << endl;
            cout << "\n";
            cout << "\n\n""How much is " << numbers[0] << " * " << numbers[1] << "?""" << endl;
            cin >> answer;
		}
		while (answer == numbers[2]);
		break;
        case 2:
		do
		{
            cout << "\nDon't give up! " << endl;
            cout << "\n";
            cout << "\n\n""How much is " << numbers[0] << " * " << numbers[1] << "?""" << endl;
            cin >> answer;
		}
        while (answer == numbers[2]);
		break;
        case 3:
		do
		{
            cout << "\nNo. Keep trying. " << endl;
            cout << "\n";
            cout << "\n\n""How much is " << numbers[0] << " * " << numbers[1] << "?""" << endl;
            cin >> answer;
		}
        while (answer == numbers[2]);
		break;
    }
Your first answer was better. When you write functions your goal should be to limit what they do to a specific task. In your first response, correct_response and incorrect_response were responsible only for printing a response. They didn't have to do any sort of logic about asking questions and getting answers.

Look back at your original post. You're going to want to take lines 20 through 31 and wrap them in some kind of loop. do-while could work, see what you can come up with.
Thats why I did the do-while at the end in my second post. I keep getting the same thing when I change the code. It lets me enter a answer, if I am wrong it gives me the answer and ask if i want to play again. I want it to tell me to answer again in one of the responses instead of giving me the answer. I don't know how to change it to work . I keep trying and it gives me the same thing over and over again
So i changed it back and added things to the correct_response and incorrect_response and it gives me this when I compile it:

it ask how much is "" . I put in a wrong answer. It told me wrong, try once more and then I can't try it again, it says do you want to play again, i press 1 and it gives me a different problem.

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
using namespace std;

void correct_response();
void incorrect_response(int answer);

int main ()
    {
        int numbers[3];
        int answer = 0; /* USER INPUT */
        int keepPlaying = 1;
        int correctAnswer = 0;
        srand (time(0));
     
       /* Initiate checking for correct answer */
       while (keepPlaying == 1) /* WHILE USER HASN'T ENTERED 1 */
       {
          if (correctAnswer != 1) {
             numbers[0] = rand() % 9 + 1; /* load the first random number into numbers[0] */
             numbers[1] = rand() % 9 + 1; /* load the second into numbers[1] */
             numbers[2] = numbers[0] * numbers[1]; /* load the answer into numbers[2] */
          }

          //cout << "Correctly answer the following or enter 1 to quit" << endl; /* PROMPT */
          cout << "\n\n""How much is " << numbers[0] << " * " << numbers[1] << "?""" << endl;
          cin >> answer;
          if (answer == numbers[2]) /* check against the numbers array */
          {
              correct_response();
              correctAnswer = 1;       
          }
          else
          {
              incorrect_response(numbers[2]);
              correctAnswer = 0;
          }
           
           cout << "\nDo you want to play again? (enter 1 for yes or 2 for no)" << endl;
           cin >> keepPlaying;
       }
        
         return 0;
    }

void correct_response() {
    int choice = rand() % 4;
    switch (choice) {
        case 0:
            cout << "\nVery Good!" << endl;
            break;
            
        case 1:
            cout << "\nExcellent!" << endl;
            break;
            
        case 2:
            cout << "\nKeep up the good work!" << endl;
            break;
            
        case 3:
            cout << "\nNice Work!" << endl;
            break;
    }
}

void incorrect_response(int answer) {
    int choice = rand() % 4;
    switch (choice) {
        case 0:
            cout << "\nNo, please try again. " << endl;
            break;
            
        case 1:
            cout << "\nWrong. Try once more. " << endl;
            break;
            
        case 2:
            cout << "\nDon't give up! " << endl;
            break;
            
        case 3:
            cout << "\nNo. Keep trying. " << endl;
            break;
    }
}


change line 17 to:
if (correctAnswer == 1)
Yeah that gives me "How much is -858993460 * -858993460?"
It shouldn't if you are still using the code you posted above

E: Found something else - change line 11 to int correctAnswer = 1;

http://ideone.com/5aeTJ8
Last edited on
Here is the code I have right now. When I compile it. It ask "How much is ""?" I put in a wrong answer, it says "No, please try again." Then ask do you want to play again. I hit 1 for yes. Then it ask the same problem again. I put in the cprrect answer. It says nice work. do you want to play again. then it says same problem again. So something is backwards somewhere. lol

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
using namespace std;

void correct_response();
void incorrect_response(int answer);

int main ()
    {
        int numbers[3];
        int answer; /* USER INPUT */
        int keepPlaying = 1;
        
        srand (time(0));
     
       /* Initiate checking for correct answer */
       while (keepPlaying == 1) /* WHILE USER HASN'T ENTERED 1 */
       {
          if (answer != 1) {
             numbers[0] = rand() % 9 + 1; /* load the first random number into numbers[0] */
             numbers[1] = rand() % 9 + 1; /* load the second into numbers[1] */
             numbers[2] = numbers[0] * numbers[1]; /* load the answer into numbers[2] */
          }

          //cout << "Correctly answer the following or enter 1 to quit" << endl; /* PROMPT */
          cout << "\n\n""How much is " << numbers[0] << " * " << numbers[1] << "?""" << endl;
          cin >> answer;
          if (answer == numbers[2]) /* check against the numbers array */
          {
              correct_response();
              correctAnswer = 1;       
          }
          else
          {
              incorrect_response(numbers[2]);
              correctAnswer = 0;
          }
           
           cout << "\nDo you want to play again? (enter 1 for yes or 2 for no)" << endl;
           cin >> keepPlaying;
       }
        
         return 0;
    }

void correct_response() {
    int choice = rand() % 4;
    switch (choice) {
        case 0:
            cout << "\nVery Good!" << endl;
            break;
            
        case 1:
            cout << "\nExcellent!" << endl;
            break;
            
        case 2:
            cout << "\nKeep up the good work!" << endl;
            break;
            
        case 3:
            cout << "\nNice Work!" << endl;
            break;
    }
}

void incorrect_response(int answer) {
    int choice = rand() % 4;
    switch (choice) {
        case 0:
            cout << "\nNo, please try again. " << endl;
            break;
            
        case 1:
            cout << "\nWrong. Try once more. " << endl;
            break;
            
        case 2:
            cout << "\nDon't give up! " << endl;
            break;
            
        case 3:
            cout << "\nNo. Keep trying. " << endl;
            break;
    }
}
Basically when i put in the correct answer it and ask to play again it gives me the same problem. when i put in a wrong answer it says wrong and then gives me a different answer and my code above was 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
using namespace std;

void correct_response();
void incorrect_response(int answer);

int main ()
    {
        int numbers[3];
        int answer; /* USER INPUT */
        int keepPlaying = 1;
        int correctAnswer = 0;
        srand (time(0));
     
       /* Initiate checking for correct answer */
       while (keepPlaying == 1) /* WHILE USER HASN'T ENTERED 1 */
       {
          if (correctAnswer != 1) {
             numbers[0] = rand() % 9 + 1; /* load the first random number into numbers[0] */
             numbers[1] = rand() % 9 + 1; /* load the second into numbers[1] */
             numbers[2] = numbers[0] * numbers[1]; /* load the answer into numbers[2] */
          }

          //cout << "Correctly answer the following or enter 1 to quit" << endl; /* PROMPT */
          cout << "\n\n""How much is " << numbers[0] << " * " << numbers[1] << "?""" << endl;
          cin >> answer;
          if (answer == numbers[2]) /* check against the numbers array */
          {
              correct_response();
              correctAnswer = 1;       
          }
          else
          {
              incorrect_response(numbers[2]);
              correctAnswer = 0;
          }
           
           cout << "\nDo you want to play again? (enter 1 for yes or 2 for no)" << endl;
           cin >> keepPlaying;
       }
        
         return 0;
    }

void correct_response() {
    int choice = rand() % 4;
    switch (choice) {
        case 0:
            cout << "\nVery Good!" << endl;
            break;
            
        case 1:
            cout << "\nExcellent!" << endl;
            break;
            
        case 2:
            cout << "\nKeep up the good work!" << endl;
            break;
            
        case 3:
            cout << "\nNice Work!" << endl;
            break;
    }
}

void incorrect_response(int answer) {
    int choice = rand() % 4;
    switch (choice) {
        case 0:
            cout << "\nNo, please try again. " << endl;
            break;
            
        case 1:
            cout << "\nWrong. Try once more. " << endl;
            break;
            
        case 2:
            cout << "\nDon't give up! " << endl;
            break;
            
        case 3:
            cout << "\nNo. Keep trying. " << endl;
            break;
    }
}
I fixed it and plays right now I had to change the values of


1
2
correct_response();
              correctAnswer = 1;     


to

1
2
correct_response();
              correctAnswer = 0;     


and

1
2
incorrect_response(numbers[2]);
              correctAnswer = 0;


to

1
2
incorrect_response(numbers[2]);
              correctAnswer = 1;

You have line 29 and line 34 swapped.

*edit

On a side note why do you pass a parameter to your incorrect response?
Last edited on
this code works correctly

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
using namespace std;

void correct_response();
void incorrect_response(int answer);

int main ()
    {
        int numbers[3];
        int answer; /* USER INPUT */
        int keepPlaying = 1;
        int correctAnswer = 0;
        srand (time(0));
     
       /* Initiate checking for correct answer */
       while (keepPlaying == 1) /* WHILE USER HASN'T ENTERED 1 */
       {
          if (correctAnswer != 1) {
             numbers[0] = rand() % 9 + 1; /* load the first random number into numbers[0] */
             numbers[1] = rand() % 9 + 1; /* load the second into numbers[1] */
             numbers[2] = numbers[0] * numbers[1]; /* load the answer into numbers[2] */
          }

          //cout << "Correctly answer the following or enter 1 to quit" << endl; /* PROMPT */
          cout << "\n\n""How much is " << numbers[0] << " * " << numbers[1] << "?""" << endl;
          cin >> answer;
          if (answer == numbers[2]) /* check against the numbers array */
          {
              correct_response();
              correctAnswer = 0;       
          }
          else
          {
              incorrect_response(numbers[2]);
              correctAnswer = 1;
          }
           
           cout << "\nDo you want to play again? (enter 1 for yes or 2 for no)" << endl;
           cin >> keepPlaying;
       }
        
         return 0;
    }

void correct_response() {
    int choice = rand() % 4;
    switch (choice) {
        case 0:
            cout << "\nVery Good!" << endl;
            break;
            
        case 1:
            cout << "\nExcellent!" << endl;
            break;
            
        case 2:
            cout << "\nKeep up the good work!" << endl;
            break;
            
        case 3:
            cout << "\nNice Work!" << endl;
            break;
    }
}

void incorrect_response(int answer) {
    int choice = rand() % 4;
    switch (choice) {
        case 0:
            cout << "\nNo, please try again. " << endl;
            break;
            
        case 1:
            cout << "\nWrong. Try once more. " << endl;
            break;
            
        case 2:
            cout << "\nDon't give up! " << endl;
            break;
            
        case 3:
            cout << "\nNo. Keep trying. " << endl;
            break;
    }
}
Glad it's working you should remove your comment on line 15 or correct it :P It looks like you had != then changed it too == when you got rid of line 23 and forgot to fix the comment.
I fixed them. Thanks!!
Topic archived. No new replies allowed.