Thread: A beginner in trouble

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    2

    A beginner in trouble

    Hi all. I am a begginer, just started in school learning this. Well, I have a problem in one of my assingments:

    Code:
    // a game where the computer has to guess a number that the 
    // user is thinking of.  I am a beginnner, and I haven't learned 
    // all the cool stuff yet - like using random number generator
    // so I am trying to do this the simple way.  But this isn't working
    // properly, something about the numbers and the 
    // guessing in the do-while loop.
    
    
    #pragma argsused
    
    #pragma argsused
    #include <iostream.h>
    #include <conio.h>
    using namespace std;
    
    int main()
    
    {
            int highNumb = 100;
            int lowNumb = 1;
            int guess;
            char ans;
    
            cout << "Think of a number between 1 and 100 \n";
    
                    cout << endl;
                    guess =(highNumb+lowNumb)/2;
                    cout << "is the number " << guess << " ?\n";
                    cout << endl;
                    cout << "is the number correct or is it too high or too low? (c,h,l)\n";
                    cin >> ans;
    
            do
            {
                    lowNumb = ++guess;
                    guess = (highNumb + lowNumb)/2;
                    cout << "the number is: "<< guess <<endl;
                    cin >> ans;
                    if (lowNumb >= highNumb)
                            cout << "CHEATER!! " <<endl;
            }while (ans == 'H' || ans == 'h');
    
            do
            {
                    highNumb = --guess;
                    guess = (highNumb - lowNumb) ;
                    cout << "the number is: " << guess <<endl;
                    cin >> ans;
                    if (lowNumb >= highNumb)
                            cout << "CHEATER "<<endl;
            }while (ans == 'L' || ans == 'l');
    
            if (ans == 'R' || ans == 'r')
                    cout << "GENIOUS!!!"<<endl;
    
    
            getch ();
            return 0;
    }
    Last edited by Eldey; 09-13-2004 at 06:18 AM.

  2. #2
    Registered User
    Join Date
    Sep 2004
    Posts
    124
    Eldey,
    As it's homework I won't rewrite this, but you're not asking whether the guess is correct, too high, or too low within the loop. You need something along these lines:

    Code:
    ...
    ...
    do
    {
    	cout << endl;
    	guess =(highNumb+lowNumb)/2;
    	cout << "is the number " << guess << " ?\n";
    	cout << endl;
    	cout << "is the number correct or is it too high or too low? (c,h,l)\n";
    	cin >> ans;
    
    	if (ans == 'H' || ans == 'h')
    	{
    		highNumb = guess;
    	}
    	if (and == 'L' || and == 'l')
    	{
    		lowNumb = guess;
    	}
    } while (ans != 'L' && ans != 'l');
    ...
    ...
    and build in checks for cheating. This should get you started.

    Edit your post and remove your e-mail address. Spammers scour boards such as these looking for e-mail addresses to send their junk to.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    2
    Ok thanks, but what am I doing wrong when the numbers don't go higher or lower, I am doing something wrong in the calculation?

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    Eldy--relook and Angoid's post line by line and ask yourself the reason for each line. The answer to this question: "what am I doing wrong when the numbers don't go higher or lower, I am doing something wrong in the calculation?" will then become clear.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. // Trouble launching 2nd thread from MainWindow, beginner //
    By RickTrelles in forum Windows Programming
    Replies: 2
    Last Post: 07-03-2008, 09:43 AM
  2. Replies: 6
    Last Post: 01-03-2007, 03:02 PM
  3. Replies: 12
    Last Post: 10-23-2006, 07:45 AM
  4. Beginner in Trouble!
    By Sarina in forum C++ Programming
    Replies: 4
    Last Post: 10-23-2004, 02:35 PM
  5. trouble with a loop (beginner)
    By Procta in forum C++ Programming
    Replies: 2
    Last Post: 12-04-2003, 10:27 AM