Thread: variations on guess my number

  1. #1
    Registered User
    Join Date
    Feb 2010

    Post variations on guess my number

    Here is a simple looping version I made of the "guess my number" program.

    So I have some questions:
    What are some other/better ways to write it, and how do you make the number the user must guess random for each run of the program? How would you specify that it's an integer between one and ten, for example?

    In this the number you have to guess is always the same. It's boring but it works. Written in Dev-C++.

    #include <iostream>
    using namespace std;
    int main ()
        int guess;
        cout<<"I am thinking of an integer between 1 and 10. Try to guess what it is!\n";
        while (guess!=5)
         if (guess==1 || guess==2 || guess==9 || guess==10){cout<<"Guess again!\n";}
         if (guess==3){cout<<"A little higher.\n";}
         if (guess==4||guess==6){cout<<"So close!\n";}
         if (guess==8){cout<<"Too high.\n";}
         if (guess==7){cout<<"A little lower.\n";}
         cout<<"What's your next guess?\n"; 
         if (guess==5){cout<<"You got it!\n";}
    return 0;

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Look up rand() from <cstdlib>.
    The simplest way to specify the range is to use modulus.

    I also suggest trying to generalize your code. You shouldn't need a test for each possible guess. Maybe try using the difference between the answer and the guess?
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 02-19-2009, 07:19 PM
  2. Random number + guessing game trouble
    By Ravens'sWrath in forum C Programming
    Replies: 16
    Last Post: 05-08-2007, 03:33 AM
  3. small problem
    By ucfmustang in forum C Programming
    Replies: 1
    Last Post: 02-10-2003, 04:55 PM
  4. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM
  5. Newton-Raphson number squaring method/pointers
    By inakappeh in forum C Programming
    Replies: 2
    Last Post: 08-29-2001, 11:04 AM

Tags for this Thread