Thread: random integer addition

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    2

    random integer addition

    hi, im fairly horrible at c++ programming and we just received a difficult (to me) final project to do...it involves taking random integers, adding them and having the user guess there total..like this:
    random number + random number = usersguess

    if the guess is wrong they are supposed to guess again...
    im not sure how to set this up..any help would be much appreciated..thank you

  2. #2
    Registered User axon's Avatar
    Join Date
    Feb 2003
    Posts
    2,572
    well, you can have a function that generates a rando number. If you want that number to be trully different seed srand to time...if you need more info about this, do a search on the board.

    add the two rand numbers, get user input, and compare....based on the comparison result go further.
    Code:
    int get_rand()
    {
        //get random nuber here;
        return randNum;
    }
    
    int main()
    {
    
        int number1 = get_rand();
        int number2 = get_rand();
        int sum = number1 + number2;
        //main prog loop
        while( guessing )
        {
                     //get user input compare that to sum using if statement
                    //if TRUE display it and break out of the loop, if not continue
        }
    }
    this should get you sarted.....another hint...when you are testing and debugging your program seed rand to a constant value.

    some entropy with that sink? entropysink.com

    there are two cardinal sins from which all others spring: Impatience and Laziness. - franz kafka

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    2

    ???

    Code:
    /*Ryan Poremba*/
    #include <iostream.h>
    
    cout<<"Enter the number for the problem type desired."<<endl;
    cout<<"  1. Addition"<<endl;
    cout<<"  2. Subtraction"<<endl;
    cout<<"  3. Multiplication"<<endl;
    cout<<endl;
    cout<<"Enter choice: ";
    cin>>choice;
    if  (choice==1){
    void addition()
    int get_rand()
    {
      for (int i=1; i<=10;i++);
        return randNum;
    }
    int main()
    {
      int number1 = get_rand();
        int number2 = get_rand();
        int sum = number1 + number2;
        //main prog loop
        while( guess==0 )
        {
    cout<<number1<<"+"<<number2<<"=";
    cin<<guess<<endl;
    if (guess==sum){
    cout<<"CORRECT"<<endl;
    correct++;
    else
    wrong++;
    break;
    }
        }
    }
    
    return(0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 06-01-2008, 07:47 AM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  3. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  4. how to generate a random integer number between 0 and 9
    By dongkhoi in forum C Programming
    Replies: 2
    Last Post: 11-07-2006, 01:49 PM
  5. Random Integer
    By MrDoomMaster in forum C++ Programming
    Replies: 15
    Last Post: 11-19-2003, 04:47 PM