rand()% help

I am using this code to ask the user multiple addition question by using a do while loop. It works fine but the question are the same every time and the numbers do not change from question to question. It is different every time the code is run but the same question in each code like so:

What is 4+7?
11
Correct!
What is 4+7?
11
Correct!
etc. etc.

So i am asking how to make the numbers different every loop.

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
#include <iostream>
#include <cmath>
#include <cstdlib>

int main(){
    using namespace std;
srand(time(0));

int num = rand()%40+1;
int num2 = rand()%15;
int answer;

cout << "Welcome to the RANDOM math game." << endl;
cout << "Each time is a new expierence." << endl;
cout << endl;

do{
cout << "What is " << num << "+" << num2 << "?" << endl;
cin >> answer;

if (answer == num+num2){
           cout << "Correct!" << endl;
           }
else{
     cout << "Incorrect!" << endl;
     } 
     }    
while(answer == num+num2);




system ("pause");
return 0;
}
You do not change values of num and num2. Change the program the following way

#include <iostream>
#include <cmath>
#include <cstdlib>

int main(){
using namespace std;
srand(time(0));

int answer;
int num;
int num2;

cout << "Welcome to the RANDOM math game." << endl;
cout << "Each time is a new expierence." << endl;
cout << endl;

do{
num = rand()%40+1;
num2 = rand()%15;

cout << "What is " << num << "+" << num2 << "?" << endl;
cin >> answer;
....
Last edited on
The randomization is happening outside of your loop. Have it done within the loop and you should have different values each time. After all, as you have it now, the values are assigned to num and num2 one time so they will always be the same.
Last edited on
I changed it like so

1
2
3
4
5
6
7
8
9
10
11
int num;
int num2;
int answer;

cout << "Welcome to the RANDOM math game." << endl;
cout << "Each time is a new expierence." << endl;
cout << endl;

do{
int num = rand()%40+1;
int num2 = rand()%15;


and when i run the code it stops at the first one. Is this because when it reruns the loop the numbers change so it is no longer equal and it stops?
I think it is because you did not guess the answer. Your loop continues when

while(answer == num+num2);

Maybe you meant

while(answer != num+num2);

THANK you Vlad! It works that way but now it doesn't work with incorrect:/
It doesn't stop the loop when you get one wrong.
What you do is what you get. If you want to have only one attempt then remove the loop.

Or change it one more

int num;
int num2;
int answer;

do{
cout << "Welcome to the RANDOM math game." << endl;
cout << "Each time is a new expierence." << endl;
cout << endl;


int num = rand()%40+1;
int num2 = rand()%15;
Last edited on
So technically i can't have a piece of code that keeps asking a question random numbers each time until i get it wrong, then it quit?
If it gets a correct answer it stops. It repeats while the answer is incorrect. But you can make it another way

#include <iostream>
#include <cstdlib>
#include <ctime>

int main(){
using namespace std;
srand(time(0));

char c;

do{

cout << "Welcome to the RANDOM math game." << endl;
cout << "Each time is a new expierence." << endl;
cout << endl;

int num = rand()%40+1;
int num2 = rand()%15;
int answer;

cout << "What is " << num << "+" << num2 << "?" << endl;
cin >> answer;

if (answer == num+num2){
cout << "Correct!" << endl;
}
else{
cout << "Incorrect!" << endl;
}

cout << "Continue (y/n)? ";
cin >> c;
}
while ( c == 'y' || c == 'Y' );




system ("pause");
return 0;
}

And you need not <cmath>

You need <ctime>
Last edited on
Ok. I do not know much about all the include stuff. So the code above does what at the end? I see it asks you if you want to continue, but what is after that?
If you press 'y' or 'Y' the loop will be repeated. If you press any other character the program will be ended.
Last edited on
Ok. thank you.
Topic archived. No new replies allowed.