Adding random numbers

I am designing a math program for kids. I want the program to produce 2 random numbers and check the sum of these numbers against the user's guess. I have the generating random numbers portion complete. What's the coding procedure to compare the sum to the user's guess? Thanks
show us the code you have so far...
1
2
3
4
5
6
7
8
9
10
11
int answer = 5, j; 
std::cout << "Enter your answer:\n"; 
cin >> j;  //Get input from user
if(answer == j) //Comparison
{
    std::cout << "They are equal\n"; 
}
else
{
    std::cout << "They are not equal\n"; 
}
As written, it replies that all of the answers are wrong. If I change the if statement to (sum != guess) all answers are correct.

int main(int argc, char *argv[])
{
sum = ((1 + (rand() % 10)) + (1 + (rand() % 10)));
int sum = 0;
//unsigned seed = (0);
srand(time(0));

for (i=1; i<=5; i=i+1)
{
cout << down10 << endl
<< over4 << " " << 1 + (rand() % 10) << endl
<< over4 << "+ " << 1 + (rand() % 10) << endl
<< over4 << "________" << endl
<< over4;
cin >> guess;
cout << down10;
if (sum == guess)
{
cout << down10 << endl
<< over3 << "Good job!" << endl
<< down10;
system("PAUSE");
}
else
{
cout << down10 << endl
<< over3 << "Sorry, try again." << endl;
cin >> guess;
cout << down10;
}
system("PAUSE");
}

return EXIT_SUCCESS;
}
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
int main(int argc, char *argv[])
{
sum = ((1 + (rand() % 10)) + (1 + (rand() % 10)));
int sum = 0;
//unsigned seed = (0);
srand(time(0));

for (i=1; i<=5; i=i+1)
{
cout << down10 << endl
<< over4 << " " << 1 + (rand() % 10) << endl
<< over4 << "+ " << 1 + (rand() % 10) << endl
<< over4 << "________" << endl
<< over4;
cin >> guess;
cout << down10; 
if (sum == guess) 
{
cout << down10 << endl
<< over3 << "Good job!" << endl
<< down10;
system("PAUSE");
}
else
{
cout << down10 << endl
<< over3 << "Sorry, try again." << endl;
cin >> guess;
cout << down10;
}
system("PAUSE");
}

return EXIT_SUCCESS;
}


I don't understand... over4, down10, over3, etc. are never declared and you put back your sum to zero after assigning it a value.... is this the whole code?
that's tab code
I hope you don't take this the wrong way, but your code is a total mess. I suggest starting this from scratch, because even if you patch this all together to work, this is not a good way of programming. Let's look at some code:

This is written in the opposite order.
1
2
3
4
5
 
sum = ((1 + (rand() % 10)) + (1 + (rand() % 10)));
int sum = 0;
//unsigned seed = (0);
srand(time(0));


What you want to do is first set the random seed (before creating ANY random numbers), then declare int sum before you use it and then make sum equal to the total of two random numbers. You also probably want to store what the two random numbers were to show them to the user...
1
2
3
4
5
6
 
srand(time(0)); //Seed before generating. 
int sum = 0, num1, num2; //Declare before using
num1 = rand() % 10 + 1; //Store the value of the first random number in num1;
num2 = rand() % 10 + 1; //Store the value of the second random number in num2; 
sum = num1 + num2; //Ask user for the guess.  


Try working from that and doing it again.

Edit: Typo

Last edited on
@ Mat, sorry about how the code looks. The user screen displays the random numbers as a math problem. That's the reason for " \n and \t". I implemented the code you suggested and it is comparing the sum to user input correctly. Now it is not generating a ctime number. I have the program generating 5 problems before it closes and now all 5 math problems are identical.
Can we see the code so we might find your problem? As an educated guess, I'd say it's likely you are either:

1) Trying to call this more than once, when it should only be called once.
 
srand(time(0)); //Seed before generating. 


2) Failing to call this code again:
1
2
num1 = rand() % 10 + 1; //Store the value of the first random number in num1;
num2 = rand() % 10 + 1; //Store the value of the second random number in num2; 


Edit: Typo
Last edited on
AeonFlux1212 gave you a really good anwser with 2 of the abilities of srand();

I will give you some of the rand(); abilities:

1.rand(); has got a number engine but every time you run the program the value of the variable a doesn't change.
1
2
3
4
5
6
7
8
#include<stdio.h>
#include<stdlib.h>

int main(void)
{
    int a;
    a=rand();
}


2.Here rand(); gives a really random value to a:
1
2
3
4
5
6
7
8
#include<stdio.h>
#include<stdlib.h>

int main(void)
{
    int a;
    a=rand(time(NULL));
}


3.Now you can select the minimum and the maximun value to a:
1
2
3
4
5
6
7
8
#include<stdio.h>
#include<stdlib.h>

int main(void)
{
    int a;
    a=rand() % 10 + 1;
}


If I didn't help you enough check those links:
1.http://www.cplusplus.com/reference/cstdlib/rand/

2.http://www.cplusplus.com/reference/cstdlib/srand/

I cannot help you with srand(); because I prefer rand();

Good luck with your program and let us know about your progress.
Last edited on
Topic archived. No new replies allowed.