Thread: Adding Random Numbers help

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    3

    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;
    }

  2. #2
    Registered User SKeane's Avatar
    Join Date
    Sep 2006
    Location
    England
    Posts
    234
    You are not doing anything with the answer the user inputs (other than storing it).

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    3
    Ok. But if i want to send the random numbers calculated , have it checked to see if it is right or wrong, and send it back, how would i do that? i have this so far:

    Code:
    int calcRealAnswer(){
    int realAnswer;
    if (realAnswer == calculateRandomNumber + calculateRandomNumber){
    cout << "Correct!";
    }
    else{
    cout << "Wrong!";}
    BTW i am a noobie, so no burning please
    Last edited by mastern200; 01-15-2007 at 11:30 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    If you do this

    cout << calculateRandomNumber();
    cout << " + ";
    cout << calculateRandomNumber();
    realAnswer = calculateRandomNumber() * calculateRandomNumber();

    Then sure you're going to get different answers.

    You need something like
    a = calculateRandomNumber();
    b = calculateRandomNumber();
    cout << a << "+" << b;
    realanswer = a + b;

    etc
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    3
    OK i understand and i fixed it. One more thing though. Is there a way so that after 3 guesses, it disables the user from entering anything else and it displays the real answer?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Just have a counter of the number of guesses.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    Something like
    Code:
    extern const int num_tries;
    int tries = 0, response;
    do
    {
       if(tries >= num_tries) {
            std::cout << "It's okay; with hard work, learning disabilities can be largely overcome. The correct answer is " << answer << '.' << std::endl;
            break;
       }
       else if(!tries) std::cout << "What is " << a << ' ' << op << ' ' << b << " ?" << std::endl;
       else std::cout << "Sorry, that's not the right answer. Try again. (" << (num_tries - tries) << " tries left)" << std::endl;
       std::cin >> response;
       if(response == answer) {
            std::cout << "Correct!" << std::endl;
            break;
       }
    }while(++tries);
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. questions....so many questions about random numbers....
    By face_master in forum C++ Programming
    Replies: 2
    Last Post: 07-30-2009, 08:47 AM
  2. Generating a sequence of numbers in a random order
    By mirbogat in forum C Programming
    Replies: 15
    Last Post: 08-12-2008, 02:01 PM
  3. Question about random numbers
    By Kempelen in forum C Programming
    Replies: 2
    Last Post: 07-02-2008, 06:28 AM
  4. random numbers limit
    By HAssan in forum C Programming
    Replies: 9
    Last Post: 12-06-2005, 07:51 PM
  5. Generating 100k to 1 million unique random numbers
    By Ariod in forum C Programming
    Replies: 4
    Last Post: 08-26-2005, 12:59 PM