Thread: question

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    7

    question

    I am to to write a program that generates a number from 1-9 & allow the user to guess the number. I have two questions. 1) Why am I getting the same number every game? 2) Why when the guess isnt correct does all my cout's re appear? I had this happen b4 in a C program & trapped the menu from showing again. This is why I created a char c. Not working this time. If anyone can point me in the right direction, I appreciate it.

    Code:
    #include<ctime>  //rand() uses same number; srand uses different number for new game
    #include<iostream>
    #include<cstdlib>  //required to use rand() function
    
    using namespace std;
    
    int main()
    {
        srand((unsigned)time(NULL));
        int number;
        number = rand()%10;
        int guess;
        char c; //used to trap extra menu
    
    
    
        do
        {
            cout<<"Let's play a game!"<<endl;
            cout<<"I'm thinking of a number from 1 to 9"<<endl;
            cout<<"Let's see if you can guess my number!"<<endl;
            cout<<"What number am I thinking? "<<endl;
            cin>>guess;
            cout<<c<<endl;
            if (guess == number)
            {
                cout<<"Lucky guess!!"<<endl;
            }
            else if ((guess-2 == number) || (guess+2 == number))
            {
                cout<<"Oh, so close! You are within two of the number."<<endl;
                cout<<"Try again! "<<endl;
                cout<<c<<endl;
            }
            else
            {
                cout<<"Not even close! Your are more than two off the number."<<endl;
                cout<<"Try again! "<<endl;
                cout<<c<<endl;
            }
    
        }while(guess != number);
        system("PAUSE"); //prevents console window from closing
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    The answer to number 2 is obvious: because that text is inside the loop!

  3. #3
    Registered User
    Join Date
    May 2010
    Posts
    7
    Yep! had to tweak couple things but u r right, pretty obvious! needed a good laugh, i did! Now all of a sudden the numbers are different. Not sure what's up with that, but it looks good now except my IF statements if I'm within 2. Thanks!!

  4. #4
    Bored Programmer
    Join Date
    Jul 2009
    Location
    Tomball, TX
    Posts
    428
    Quote Originally Posted by Buddha View Post
    except my IF statements if I'm within 2.
    You said what you wanted to do, but didn't do it. If the number was 7 and you guessed six. Do the math on this code

    Code:
    else if ((guess-2 == number) || (guess+2 == number))
    6-2 = 4
    6+2 = 8

  5. #5
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    Erase the following:
    Code:
    char c; 
    
    //and
    
    cout<<c<<endl;
    and replace the second with:

    Code:
    cin.ignore();
    cin.get();
    I believe you intended to halt the program there, but the program doesn't because it coutputs the c character immediately!!
    Devoted my life to programming...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question bout my work
    By SirTalksAlots in forum C Programming
    Replies: 4
    Last Post: 07-18-2010, 03:23 PM
  2. A question about a question
    By hausburn in forum C++ Programming
    Replies: 3
    Last Post: 04-25-2010, 05:24 AM
  3. Alice....
    By Lurker in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 06-20-2005, 02:51 PM
  4. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  5. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM