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
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?
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.
@ 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;