Adding Random Numbers help

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mastern200
    New Member
    • Oct 2006
    • 16

    #1

    Adding Random Numbers help

    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?

    Code:
    #include <iostream>
    #include <cstdlib> 
    #include <ctime> 
    using namespace std;
    
    void displayHeader();
    void problemType();
    void calcRealAnswer();
    bool goAgain();
    
    int calculateRandomNumber();
    
    int choice = 0;
    int userAnswer = 0;
    int realAnswer = 0;
    float randomNumber = 0;
    
    
    int main(){
        srand((unsigned)time(0)); 
    
    do {
            displayHeader();
            problemType();
            }while(goAgain());
            system("cls");
    
    cin.get();
    cin.get();
    return 0;
    }
    
    void displayHeader()
    {
         cout<<"Welcome to my math game program. Sit back and enjoy!!"<<endl;
         cout<<endl;
    }
    
    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;
         }
         
    }
    
    bool goAgain()
    {
         char answer;
    
         cout << "Would you like to play again? ";
         cin >> answer;
    
         system("cls");
      while(answer != 'Y' && answer != 'N')
      {
      cout << "Invalid choice.  Would you like to go again? ";
      cin >> answer;
      }
    
      cout << endl;
      
    
      if(answer == 'Y')
      return true;
      else
      return false;
        }
    
    int calculateRandomNumber(){
        
        return (rand()%100) + 1;
    }
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    Originally posted by mastern200
    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 << calculateRandom Number();
    cout << " + ";
    cout << calculateRandom Number();
    cout << " = ";
    cin >> userAnswer;}

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

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

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

    }

    int calculateRandom Number(){

    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 calculateRandom Number 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 calculateRandom Number():

    Code:
    int num1, num2;
    num1 = calculateRandomNumber();
    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).

    Comment

    • mastern200
      New Member
      • Oct 2006
      • 16

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

      Code:
      int calcRealAnswer(){
      if (choice==1){
      int realAnswer;
      int num1, num2;
      realAnswer = num1 + num2;
      
      if (userAnswer == realAnswer){
      cout << "Correct!";
      }
      
      else
      {
      cout << "Wrong!";}
      }
      }

      Comment

      • mastern200
        New Member
        • Oct 2006
        • 16

        #4
        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?

        Comment

        • Ganon11
          Recognized Expert Specialist
          • Oct 2006
          • 3651

          #5
          Originally posted by mastern200
          OK i understand. But now, if i put the right answer, it says WRONG!. Even after i put num1 and num2.

          Code:
          int calcRealAnswer(){
          if (choice==1){
          int realAnswer;
          int num1, num2;
          realAnswer = num1 + num2;
          
          if (userAnswer == realAnswer){
          cout << "Correct!";
          }
          
          else
          {
          cout << "Wrong!";}
          }
          }
          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):

          Code:
          void problemType() {
              int num1, num2;
              num1 = calculateRandomNumber();
              num2 = calculateRandomNumber();
              int userChoice;
              // Get user choice here: 1 = Add, 2 = Subtract, 3 = Multiply
          
              switch (userChoice) { // This is my preference, you can use an if...else ladder
                  case 1:
                      cout << num1 << " + " << num2 << " = ?" << endl;
                      int userGuess;
                      cin >> userGuess;
                      if (userGuess == num1 + num2) cout << "Correct!" << endl;
                      else cout << "Wrong!" << endl;
                      break;
                  case 2:
                      // Continue with subtraction, multiplication, etc.
              }
          }

          Comment

          Working...