Thread: OK here's an example....

  1. #1
    Silent Wolf
    Guest

    Talking OK here's an example....

    Duh... I have an example of what i'm looking for. This is one that I did in class but i need to change it so that the computer picks the number and the user guesses (oh an Esp. that program didn't work x.x I think it's cuz our compiler is Microsoft Visual C++ 4.0) Anyways... here's the program we did
    *Note I need to change it so the computer picks the number*

    #include <iostream.h>

    void main ()
    {
    char high_low;
    int done = 0, current_guess = 50, highest = 100, lowest = 0;

    do{
    cout<<"Is "<<current_guess
    <<"too high, too low, or is it right? ('h', 'l', 'r')";
    cin>>(high_low)

    switch (high_low)
    {
    case 'l':
    if (current_guess > lowest);
    {
    lowest = current_guess;
    }
    current_guess = current_guess + (highest - current guess) /2;
    break;

    case 'h':
    if (current_guess <highest);
    {
    highest = current_guess;
    }
    current_guess = lowest + (current_guess - lowest) /2;
    break;
    default:
    cout<<high_low<<" is not a valid choice \n";
    break;
    }
    }while (!done);
    }


    Umm... yeah that's about it...but I need it done so that the computer 'thinks' of the number and the person guesses.... anyone that can help me with this?

  2. #2
    www.entropysink.com
    Join Date
    Feb 2002
    Posts
    603
    current_guess = current_guess + (highest - current guess) /2
    should read current_guess = current_guess + (highest - current_guess) /2

    Also no switch case for reply r.

    Other than that (and I think a missing ; somewhere) this compiles fine on DEVC++

    As for your other question, if you look back at the replies, it's already been answered.

    Easy isn't it.

    Take care,

    Rob.
    Visit entropysink.com - It's what your PC is made for!

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >i need to change it so that the computer picks the number and the user guesses
    Code:
    #include <iostream>
    #include <ctime>
    using namespace std;
    
    int main ( void )
    {
      int correct = 0,
          userGuess,
          compAnswer;
      srand ( time ( NULL ) );
      compAnswer = rand() % 100;
      while ( !correct ) {
        cout<<"Care to guess? ";
        cin>>userGuess;
        if ( userGuess > compAnswer )
          cout<< "Too high\n";
        else if ( userGuess < compAnswer )
          cout<<"Too low\n";
        else {
          cout<< userGuess <<" was correct!\n";
          correct = 1;
        }
      }
      return EXIT_SUCCESS;
    }
    >void main ()
    Don't use void main, it's wrong

    -Prelude
    My best code is written with the delete key.

  4. #4
    Silent Wolf
    Guest
    The microsoft visual isn't accepting <ctime> .... you also put <iostream> but it only takes <iostream.h>... could it be something like that with the ctime error?

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >you also put <iostream> but it only takes <iostream.h>
    You have an old compiler, the current C++ standard omits the .h extension and adds c to headers that are based from the C language. Simply change
    Code:
    #include <iostream>
    #include <ctime>
    using namespace std;
    to
    Code:
    #include <iostream.h>
    #include <time.h>
    -Prelude
    My best code is written with the delete key.

  6. #6
    Registered User biosx's Avatar
    Join Date
    Aug 2001
    Posts
    230
    Originally posted by Silent Wolf
    The microsoft visual isn't accepting <ctime> .... you also put <iostream> but it only takes <iostream.h>... could it be something like that with the ctime error?
    If you are using MSVC++ 4.0, then it probably won't recognize ANSI C++. Just use iostream.h and time.h instead.

    Code:
    #include <iostream.h>
    #include <time.h>
    
    #define TRUE 1
    #define FALSE 0
    
    int main ()
    {
      int correct = FALSE,
          userGuess,
          compAnswer;
    
      srand ( time ( NULL ) );   // Seed rand()
      
      compAnswer = rand() % 100;
      
      while ( !correct ) 
      {
        cout<<"Care to guess? ";
        cin>>userGuess;
        
        if ( userGuess > compAnswer )
          cout<< "Too high\n";
        else if ( userGuess < compAnswer )
          cout<<"Too low\n";
        else {
          cout<< userGuess <<" was correct!\n";
          correct = TRUE;
        }
      }
      return EXIT_SUCCESS;
    }

Popular pages Recent additions subscribe to a feed