By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,686 Members | 1,558 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,686 IT Pros & Developers. It's quick & easy.

Adding Random Numbers help

P: 16
I need to create a program where the user selects addition, subtraction, or multiplication, and when they select one, 2 random numbers appear, and they must put in the right answer. The problem is, i cant get the correct answer. It just says: Would you like to play again?

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <cstdlib> 
  3. #include <ctime> 
  4. using namespace std;
  5.  
  6. void displayHeader();
  7. void problemType();
  8. void calcRealAnswer();
  9. bool goAgain();
  10.  
  11. int calculateRandomNumber();
  12.  
  13. int choice = 0;
  14. int userAnswer = 0;
  15. int realAnswer = 0;
  16. float randomNumber = 0;
  17.  
  18.  
  19. int main(){
  20.     srand((unsigned)time(0)); 
  21.  
  22. do {
  23.         displayHeader();
  24.         problemType();
  25.         }while(goAgain());
  26.         system("cls");
  27.  
  28. cin.get();
  29. cin.get();
  30. return 0;
  31. }
  32.  
  33. void displayHeader()
  34. {
  35.      cout<<"Welcome to my math game program. Sit back and enjoy!!"<<endl;
  36.      cout<<endl;
  37. }
  38.  
  39. void problemType()
  40. {
  41.  
  42.      cout << "Enter the number for the problem type desired:" << endl;
  43.      cout << "1.  Addition"  << endl;
  44.      cout << "2.  Subtraction"  << endl;
  45.      cout << "3.  Multiplication"  << endl;
  46.      cout << "Enter choice: ";
  47.      cin >> choice;
  48.  
  49.      if (choice==1){
  50.      cout << calculateRandomNumber();
  51.      cout << " + "; 
  52.      cout << calculateRandomNumber();
  53.      cout << " = ";
  54.      cin >> userAnswer;}
  55.  
  56.      else if (choice==2){
  57.      cout << calculateRandomNumber();
  58.      cout << " - ";
  59.      cout << calculateRandomNumber();
  60.      cout << " = ";
  61.      cin >> userAnswer;}
  62.  
  63.      else if (choice==3){
  64.      cout << calculateRandomNumber();
  65.      cout << " * "; 
  66.      cout << calculateRandomNumber();
  67.      cout << " = ";
  68.      cin >> userAnswer;}
  69.  
  70.      else{
  71.      cout << "Invalid answer. Please select a valid choice: ";
  72.      cin >> choice;
  73.      }
  74.  
  75. }
  76.  
  77. bool goAgain()
  78. {
  79.      char answer;
  80.  
  81.      cout << "Would you like to play again? ";
  82.      cin >> answer;
  83.  
  84.      system("cls");
  85.   while(answer != 'Y' && answer != 'N')
  86.   {
  87.   cout << "Invalid choice.  Would you like to go again? ";
  88.   cin >> answer;
  89.   }
  90.  
  91.   cout << endl;
  92.  
  93.  
  94.   if(answer == 'Y')
  95.   return true;
  96.   else
  97.   return false;
  98.     }
  99.  
  100. int calculateRandomNumber(){
  101.  
  102.     return (rand()%100) + 1;
  103. }
  104.  
Jan 15 '07 #1
Share this Question
Share on Google+
4 Replies


Ganon11
Expert 2.5K+
P: 3,652
void problemType()
{

cout << "Enter the number for the problem type desired:" << endl;
cout << "1. Addition" << endl;
cout << "2. Subtraction" << endl;
cout << "3. Multiplication" << endl;
cout << "Enter choice: ";
cin >> choice;

if (choice==1){
cout << calculateRandomNumber();
cout << " + ";
cout << calculateRandomNumber();
cout << " = ";
cin >> userAnswer;}

else if (choice==2){
cout << calculateRandomNumber();
cout << " - ";
cout << calculateRandomNumber();
cout << " = ";
cin >> userAnswer;}

else if (choice==3){
cout << calculateRandomNumber();
cout << " * ";
cout << calculateRandomNumber();
cout << " = ";
cin >> userAnswer;}

else{
cout << "Invalid answer. Please select a valid choice: ";
cin >> choice;
}

}

int calculateRandomNumber(){

return (rand()%100) + 1;
}
[/code]
Your problem is with your problemType() function. You never do anything. Suppose the user hits 1 for Addition. Your code outputs the random number plus another random number, and then asks for the answer. But it never does anything with the answer! You need to check and see if, in fact, their answer is correct. In order to do this, you will have to store the random numbers you generate. Since calculateRandomNumber returns an integer, you should store that integer into a value. For instance, after the user enters 1 for addition, make two int variables (num1 and num2) and set these variables to the value returned by calculateRandomNumber():

Expand|Select|Wrap|Line Numbers
  1. int num1, num2;
  2. num1 = calculateRandomNumber();
  3. num2 = calculateRandomNumber();
Now you can check to see if the value the user enters is equal to num1 + num2 (or minus, or multiplied, depending on the block you're in).
Jan 15 '07 #2

P: 16
OK i understand. But now, if i put the right answer, it says WRONG!. Even after i put num1 and num2.

Expand|Select|Wrap|Line Numbers
  1. int calcRealAnswer(){
  2. if (choice==1){
  3. int realAnswer;
  4. int num1, num2;
  5. realAnswer = num1 + num2;
  6.  
  7. if (userAnswer == realAnswer){
  8. cout << "Correct!";
  9. }
  10.  
  11. else
  12. {
  13. cout << "Wrong!";}
  14. }
  15. }
  16.  
Jan 15 '07 #3

P: 16
My bad, there were 2 equal signs :)

BUT is there a way i can make it so that after 3 guesses, it disables the user from entering anything else and it displays the real answer?
Jan 15 '07 #4

Ganon11
Expert 2.5K+
P: 3,652
OK i understand. But now, if i put the right answer, it says WRONG!. Even after i put num1 and num2.

Expand|Select|Wrap|Line Numbers
  1. int calcRealAnswer(){
  2. if (choice==1){
  3. int realAnswer;
  4. int num1, num2;
  5. realAnswer = num1 + num2;
  6.  
  7. if (userAnswer == realAnswer){
  8. cout << "Correct!";
  9. }
  10.  
  11. else
  12. {
  13. cout << "Wrong!";}
  14. }
  15. }
  16.  
In this case, you are still not initializing num1 and num2. Thus, realAnswer is going to have some garbage value, since you don't know what will be in num1 and num2. Here's what you should have (or something like this):

Expand|Select|Wrap|Line Numbers
  1. void problemType() {
  2.     int num1, num2;
  3.     num1 = calculateRandomNumber();
  4.     num2 = calculateRandomNumber();
  5.     int userChoice;
  6.     // Get user choice here: 1 = Add, 2 = Subtract, 3 = Multiply
  7.  
  8.     switch (userChoice) { // This is my preference, you can use an if...else ladder
  9.         case 1:
  10.             cout << num1 << " + " << num2 << " = ?" << endl;
  11.             int userGuess;
  12.             cin >> userGuess;
  13.             if (userGuess == num1 + num2) cout << "Correct!" << endl;
  14.             else cout << "Wrong!" << endl;
  15.             break;
  16.         case 2:
  17.             // Continue with subtraction, multiplication, etc.
  18.     }
  19. }
Jan 15 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.