Random Number Generator Addition Problem.

Hello everyone, First time post here so I hope I'm not breaking any rules.
This assignment is a combination of loops and random number generators. Unfortunately my professor did not lecture over random number generation and I've been using this forum to try to teach myself.

I have a problem for my class that requires us to generate two random numbers between 10 and 50. The program then should ask the user to guess the sum of the two generated numbers. Depending on whether correct or incorrect, a counter will be updated and then they'll be asked to run it again until they decide they no longer want to run it. At the end the program should display the number of correct and incorrect guesses.

I will copy paste what I have for code, from what we had been taught in class it seemed to me that a do-while loop would have been the optimal one.

The only issue I have is that while executing the code it will always tell me the guess is correct no matter what the number I enter is. What can I do to properly check the sum, as I'm sure more often than not I should be guessing incorrectly not correctly.

Below is the loop portion of the code as I have it written where I am trying to see what I did wrong. If you require my variables I can post those as well. Code is just long and I didn't want to post a bunch of unecessary lines if I didn't need to.

do
{
// We will now generate our two random numbers.
num1 = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE;
num2 = (rand() % (MAX_VALUE - MIN_VALUE + 1)) + MIN_VALUE;
// Now we will ask the user to calculate the sum of the two numbers.
cout << "Two random numbers have been generated,\n";
cout << "Calculate the sum of x+y";
cin >> guess;
//Now we will check to see if our guess is equal to the sum of our two
//Random numbers.
if (guess = num1 + num2)
{
cout << "You guessed correctly";
correct++;
cout << "Would you like to try again? Enter Y/N." << endl;
cin >> again;
}
else
{
cout << "You guessed incorrectly";
incorrect++;
cout << "Would you like to try again? Enter Y/N." << endl;
cin >> again;
}
}
while (again == 'y' || again == 'Y');
{
if (again == 'n' || again == 'N')
{
cout << "The number of correct guesses is: " << correct << "." << endl;
cout << "The number of incorrect guesses is: " << incorrect << "." << endl;
}
}
return 0;
}


Last edited on
I'd just like to let everyone know that I figured it out. I took a sum of my two random numbers, and then instead of setting (guess = num1+num2) I did (guess == sum) and it works perfect now. Thanks to everyone who took the time to read this.
Topic archived. No new replies allowed.