Repeating a question, not asking an identical question

How do I repeat a question to the user? I don't want to ask an identical question I want to repeat the same one.

I have written my code so it will ask a question, wait for a responce, determine if that response is correct or incorrect. If the response is correct the code will move on. If the response is incorrect the code should repeat the question until the user gets it right.

Up until this point I thought I had it figured out but I will eventurally be asking randomly generated questions and if the user gets one of those wrong I will need to repeat that particular question until they get if correct.

line 81 is the problem area.

Bonus: Do you think that lines 97, 104, 111, and 118 are necessary?
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
//
//  main.cpp
//  Weekly Assignment #2
//
//  Created by Joshua Smith on 9/18/12.
//  Copyright (c) 2012 Joshua Smith. All rights reserved.
//
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>
#include <ctime>
#include <cmath>
#include <fstream>
using namespace std;






double correctAnswer = 0;
double incorrectAnswer = 0;
float percentCorrect;
int level;
double x;
double y;
int mathType;
int addition =1;
int subtraction = 2;
int multiplication = 3;
int division = 4;
int all = 5;



int main(int argc, const char * argv[])
{  
    

    
        
    
    //upon recieving a desired level program shall take the student to funciton 1 or function 2
    
    
    
        
    
    cout << "What difficulty level would you like to try?\n" ;
    cout << "Enter 1 for easy problems or 2 for harder problems.\n";
    cin >> level;
    cout << "Ok, you've chosen level " << level << " lets get started.\n" << endl;
    
    cout << "What type of math problem would you like to work on?\n Press 1 for Addition Problems\n Press 2 for Subtraction problems\n Press 3 for Multiplication problems\n Press 4 for Division problems only\n Press 5 for a random mixture of all types" << endl;;
    cin >> mathType;
    
    
        for(int counter = 1; counter<=5; counter++)
        {
            
            
            srand( (unsigned)time(0) );//randomize the randomizer with time as the seed
            if (level == 1) // if user wants easy questions
            {
                x = rand()  % 9 + 1;
                y = rand()  % 9 + 1;
            }
            
            else if (level == 2) // if user wants harder questions
            {
                x = rand() % 99 + 1;
                y = rand() % 99 + 1;
            }
            
            else if (level == 3){
                x = rand() % 999 + 1;
                y = rand() % 999 + 1;
            }
            
            
            double answer;//student's answer input
            int sum = x + y;
            int difference = x - y;
            int product = x * y;
            int quotient = x / y;
            
            if( mathType == 1){
                char cstring[] = " plus ";
                cout << "What is " << x << cstring << y << "?" << endl;//prompt user for mulitplication answer to randomly generated problem
                
                cin >> answer;//users answer to be checked and compared with computer's product
                correctAnswer = sum;
            }
            else if (mathType == 2){
                char cstring[] = " minus ";
                cout << "What is " << x << " cstring " << y << "?" << endl;//prompt user for mulitplication answer to randomly generated problem
                
                cin >> answer;//users answer to be checked and compared with computer's product
                correctAnswer = difference;
            }
            else if (mathType == 3){
                char cstring[] = " times ";
                cout << "What is " << x << " cstring " << y << "?" << endl;//prompt user for mulitplication answer to randomly generated problem
                
                cin >> answer;//users answer to be checked and compared with computer's product
                correctAnswer = product;
            }
            else if (mathType == 4){
                char cstring[] = " divided by ";
                cout << "What is " << x << " cstring by " << y << "?" << endl;//prompt user for mulitplication answer to randomly generated problem
                
                cin >> answer;//users answer to be checked and compared with computer's product
                correctAnswer = quotient;
            }
            else if (mathType == 5){
                char cstring[] = " plus ";
                cout << "What is " << x << " cstring " << y << "?" << endl;//prompt user for mulitplication answer to randomly generated problem
                
                cin >> answer;//users answer to be checked and compared with computer's product
            }
            
                    
            
            if (answer == correctAnswer)//checking to see correctness of user's answer
            {
                ++correctAnswer;
                int goodStatement = rand()  % 3 +1;
                switch (goodStatement)
                {
                    case 1:
                        puts("Very good!");
                        break;
                    case 2:
                        puts("Excellent");
                        break;
                    case 3:
                        puts("Nice work!");
                        break;
                    case 4:
                        puts("Keep up the good work");
                        break;
                        
                    default:
                        puts("Default good");
                        break;
                }

            }
            
            else
            while (answer!= correctAnswer)
            {//while loop shall run with the same x and y values being presented. The program will not move forward until a correct answer is entered by user.
         
                ++incorrectAnswer;
                int badStatement = rand()  % 3 +1;
                switch (badStatement)
                {
                    case 1:
                        puts("No. Please try again");
                        break;
                    case 2:
                        puts("Wrong. Try once more");
                        break;
                    case 3:
                        puts("Don't give up");
                        break;
                    case 4:
                        puts("No. Keep trying");
                        break;
                    default:
                        puts("This is the default no");
                        break;
                }
                

                //This should repeat the question that was answered incorrectly.
                //change operands to a string that reads out the mathmatical operation
                
                cout << "What is " << x <<  cstring  << y << "?" << endl;
                cin >> answer;
            
            if (answer == correctAnswer)//checking to see correctness of user's answer
            {
                ++correctAnswer;
                int goodStatement = rand()  % 3 +1;
                switch (goodStatement)
                {
                    case 1:
                        puts("Very good!");
                        break;
                    case 2:
                        puts("Excellent");
                        break;
                    case 3:
                        puts("Nice work!");
                        break;
                    case 4:
                        puts("Keep up the good work");
                        break;
                        
                    default:
                        puts("Default good");
                        break;
                }       
            }
        }            
    }
    //This is messed up. The math is wrong and needs to be fixed. 
    cout << "You got " << correctAnswer <<" of them correct. Great work!" << endl;
    cout << "You missed " << incorrectAnswer << "." << endl;

    percentCorrect = (correctAnswer / correctAnswer + incorrectAnswer)*100;
    cout << percentCorrect << (setprecision(2)) << "%" << endl;
        if (percentCorrect < .75) {
            cout << "Please ask you teacher for extra help." << endl;       
        } else {
            cout << "Congratulations, you are ready to go the next level!" << endl;
        }     
}
while loop with an if check and if its valid use bool values
@Aramil of Elixia

I don't understand.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
bool valid = false;
string answer;

do
{
	cout<<"Question: ..."<< endl;
	getline(cin, answer);

	if(answer == "correct answer")
		valid = true;

	else
		cerr<<"That is the wrong answer. Try again."<< endl;
}while(!valid);
Topic archived. No new replies allowed.