Thread: A noob in need of help!

  1. #1
    Registered User
    Join Date
    Nov 2003
    Posts
    9

    Unhappy A noob in need of help!

    HI! Im trying to make a C++ console app. where you give the computer a number and it tries to guess it by process of elimination.
    You put in a number for it to guess,
    then it sees if it is bigger or smaller than 5000 and goes from there....

    Code:
    while (Guess != SecretNumber)
    	{
    		Sleep(32);  //slow it down
    
    		if(Guess > SecretNumber)
    		{
    			cout << "Ok, so it's bigger than "<< Guess <<"\n\n";
    			Guess = Guess /2;
    			cout << "Um... Is it... " <<Guess<<"??\n";
    		}
    		else if(Guess < SecretNumber)
    		{
    			cout << "Ok, so it's smaller than "<<Guess<<"\n\n";
    			Guess = Guess - (Guess/2);
    			cout << "Um... Is it... " <<Guess<<"??\n";
    		}
    		else if (SecretNumber == Guess)
    		{
    			cout << "yay\n";
    		}
    Could please tell me where I am going wrong and maybe what I need to do [/plea]
    ;D
    Thanks

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787

    Re: A noob in need of help!

    I'm tempted to say this is a generally bad algorithm, but here goes...
    Code:
    Guess = Guess - (Guess/2);
    should be something like:
    Code:
    Guess+=(Guess/2);
    //or
    Guess = Guess + (Guess/2);
    you can also run into this problem... Guess comes to 3, and their number is two.

    Code:
    guess>number
      guess/2
    guess<number
      guess+=guess/2
    this is how it looks to a computer (kinda)
    Code:
    3>2
      3/2=1
    1<2
       1/2=0; 0+1=1;
    then it goes into an infinite loop...
    perhaps you should go with the more conventional version of this program: the program picks a random number and the user guesses it.
    Last edited by major_small; 11-20-2003 at 08:47 PM.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  3. #3
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Or
    Code:
    Guess *= 1.5
    and
    Code:
    Guess *= 0.5
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Binary search anyone?
    Code:
    #include <iostream>
    #include <stdlib.h>
    #include <windows.h>
    
    using namespace std;
    
    int main()
    {
      int number;
      int l, h, m;
    
      cout<<"Enter a number between 0 and 5000: ";
      if ( !( cin>> number || number < 0 || number > 5000 ) ) {
        cerr<<"I said a number between 0 and 5000...";
        return EXIT_FAILURE;
      }
      l = 0;
      h = 5000;
      cout<<"Hmm";
      while ( true ) {
        m = ( l + h ) / 2;
        cout<<'.';
        if ( number < m )
          h = m - 1;
        else if ( number > m )
          l = m + 1;
        else
          break;
        Sleep ( 500 );
      }
      cout<<"It's "<< m <<", I knew it!"<<endl;
    }
    My best code is written with the delete key.

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Prelude! How could you!?!?!!

    #include <stdlib.h>

    #include <cstdlib>

    Sheesh. I expected more from you.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Registered User
    Join Date
    Nov 2003
    Posts
    9

    Talking

    Cool thanx!
    I'll get to work!
    If I have any more troubles, now I know where to go!

    P.S. The programme I made prior to this one was one where the computer makes a random number and the user guesses it. [it actually worked!]

    P.P.S. Think I might have a crack at making pong after this... these forums are full of pong programmes!!

  7. #7
    Registered User Dante Shamest's Avatar
    Join Date
    Apr 2003
    Posts
    970
    > Prelude! How could you!?!?!!

    I bet she was using Dev-C++, which automatically inserts <stdlib.h> instead of <cstdlib>.

  8. #8
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    i completely forgot that Dev adds that stuff in... whenever I start a new project, I always erase everything it throws in there... prewritten code just kinda annoys me...
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Sheesh. I expected more from you.
    Hmm? What do you mean? (I actually know what you mean, but I want to hear your reasoning before I give you mine )

    >I bet she was using Dev-C++, which automatically inserts <stdlib.h> instead of <cstdlib>.
    Nope, I wrote that in MSVC++ 6. And the choice of header was quite deliberate.
    My best code is written with the delete key.

  10. #10
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    My reasoning is that since it is a C++ program, it should use C++ headers. Since there is a C++ conversion of <stdlib.h> (<cstdlib>), you should use it. I figured that you would at least conform to the standards.

    Now let's hear your reasoning.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Since there is a C++ conversion of <stdlib.h> (<cstdlib> ), you should use it.
    The C++ headers wrap everything in the std namespace. This makes things like printf calls look funny. Also, my version of MSVC++ doesn't do this, so if I forget the std:: prefix, I'll be programming outside of the standard unless I use a using directive on std, which I prefer not to do unless I'm exceptionally lazy.

    >I figured that you would at least conform to the standards.
    The C++ standard gives us the c* headers, but it also requires conformance with standard C. This means that the C .h headers must be supported in every C++ implementation. So I've kept to the standards.

    >Now let's hear your reasoning.
    1 part laziness, 1 part sadistic intentions, and 2 parts obscure standards interpretation. I do it because I don't like to deal with VC++6's problems while compiling with multiple compilers, and I like to shoot down arguments against a seemingly non-standard style.
    My best code is written with the delete key.

  12. #12
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    >>This makes things like printf calls look funny.

    Well printf calls already look funny in my opinion...cout is prettier

    I guess I can't argue with you though...
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Well printf calls already look funny in my opinion...cout is prettier
    A matter of opinion to be sure. But I don't usually use C streams in C++ for the sake of good style and because they mesh awkwardly with std::strings.
    My best code is written with the delete key.

  14. #14
    Registered User
    Join Date
    Nov 2003
    Posts
    9
    Thanks for your help:
    I've almost got my programme (btw is it programme or program??) working (haven't had much time to work on it :@)

    P.S:
    ...I bet she was using Dev-C++.....
    'She' = me
    Me = Wiramu
    Wiramu = a 'he'
    confused?
    Good! 'Cos I'm wiramu, and I'm a dude!
    o_0

    PPS: Im using MS VS version7
    ;D

  15. #15
    Registered User
    Join Date
    May 2003
    Posts
    1,619
    Originally posted by Prelude
    The C++ standard gives us the c* headers, but it also requires conformance with standard C. This means that the C .h headers must be supported in every C++ implementation. So I've kept to the standards.
    All the C .h headers are officially listed as deprecated, so they apparently aren't going to be with us forever.

    Also, C++ has never premitted all standard C code to compile; even before C99, C++ and C had different rules in a number of areas. One that comes to mind is C's automatic conversion of a void * to any other pointer type which is forbidden in C++. So I think the implication that standard C is standard C++ as well is a little misleading.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Noob to programming need help with a project
    By Wheelsonbus in forum C Programming
    Replies: 6
    Last Post: 02-25-2009, 03:46 AM
  2. Noob in need of help.
    By gec5741 in forum C++ Programming
    Replies: 18
    Last Post: 01-23-2009, 03:25 PM
  3. Noob printf question
    By lolguy in forum C Programming
    Replies: 3
    Last Post: 12-14-2008, 08:08 PM
  4. noob needs help!!!!:(
    By tykenfitz in forum C Programming
    Replies: 1
    Last Post: 07-10-2005, 08:49 AM
  5. Just a little request from a noob...
    By Lee134 in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 03-18-2005, 05:45 AM