Issue w/ Guess My Number Program

This is a discussion on Issue w/ Guess My Number Program within the C++ Programming forums, part of the General Programming Boards category; Hi guys; After abandoning C++ for around a year I have decided to try and return and relearn all the ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    62

    Issue w/ Guess My Number Program

    Hi guys;

    After abandoning C++ for around a year I have decided to try and return and relearn all the stuff I forgot and couldn't understand to begin with. So I started reading a few C++ eBooks and am looking at one on game programming, this isn't like RPG programming, or really anything graphics related, it just uses the mask of games like tic tac toe and guess my number to teach about arrays and whatnot.

    Anyway I have run into a problem while wanting to update my Guess My Number program so that there are two modes, the first where you guess the computer's number, and the second where the computer guesses yours. The second mode is the one where I am having an issue.

    First off, it works fine with numbers between 1 and 10, but when I try to do numbers above that, like between 1 and 50 it doesn't work as I want it to. I will set the number to 30; It will guess a number like 32, be told it's too high, then guess 9, be told it's too low, then guess 38, disregarding that if the number 32 is too low then it must guess lower; I tried a few things and it gets it to where the number is eventually guessed, but I was wondering if there was a way I could program it so that if it guesses a number that's too high, it won't guess a higher number. My code is below so you can see the method that I was shown in the book. My code is sort of a mess right now, but it was nice before I added the second mode crap.

    Below there is just the code that is in question, and beneath that is the entire file

    Any help would be greatly appreciated, this is really annoying me

    CODE IN QUESTION
    Code:
    if (mode == 2)
        {
            printf("Enter a number between 1 and 50: ");
            cin >> number;
            compMax = 50;
            compMin = 1;
            srand(time(0));
            guessNum = (rand() % compMax) + 1;
            guessCount = 1;
            while (play != 'n')
            {
                printf("\nGuess #%d: %d\n",guessCount,guessNum);
                if (guessCount >= 25)
                {
                    printf("Well your number couldn't be guessed");
                    system("PAUSE");
                    break;
                }
                if (guessNum < number)
                {
                    printf("Too Low!\n");
                    compMin = guessNum;
                    compVar = (50 - compMin);
                    guessNum = (rand() % 50) + compVar;
                    if (guessNum <= compMin || guessNum >= 50)
                    {
                        guessNum = (rand() % 50) + compVar;
                        if (guessNum <= compMin || guessNum >= 50)
                        {
                            guessNum = (rand() % 50) + compVar;
                        }
                    }
                    guessCount++;
                    continue;
                }
                if (guessNum > number)
                {
                    printf("Too High!\n");
                    compMax = guessNum;
                    guessNum = (rand() % compMax) + 1;
                    if (guessNum <= compMin || guessNum >= 50)
                    {
                        guessNum = (rand() % compMax) + 1;
                        if (guessNum <= compMin || guessNum >= 50)
                        {
                            guessNum = (rand() % compMax) + 1;
                        }
                    }
                    guessCount++;
                    continue;
                }
                if (guessNum = number)
                {
                    printf("Well it appears your number was guessed on guess %d\n",guessCount);
                    system("PAUSE");
                    break;
                }
            }
        }
    ALL CODE

    Code:
    /*
        GuessMyNumber.cpp
        Sums up everything from the second chapter via the classic number game ^_^
    */
    #include <iostream>
    #include <cstdlib>
    #include <ctime>
    
    using namespace std;
    
    int main()
    {
        int number,guessCount,guessNum,difficulty,mode,compVar,compMax,compMin;
        printf("\tWelcome to the Guess My Number Game!\n");
        char play = 'y';
        printf("\n\tModes\n1) You guess the number\n2) The computer guesses your number\n\nEnter your selection: ");
        cin >> mode;
        if (mode == 1)
        {
            system("CLS");
            printf("\tWelcome to the Guess My Number Game!\nWhat difficulty would you like?\n1) Easy (Numbers 1-5)\n2) Normal (Numbers 1-10)\
            \n3) Hard (Numbers 1-100)\nEnter the number of your selection: ");
            cin >> difficulty;
            switch (difficulty)
            {
            case 1:
                 srand(time(0));
                 number = (rand() % 5) + 1;
                 printf("You chose Easy\nGuess a number between 1 and 5.");
                 break;
            case 2:
                 srand(time(0));
                 number = (rand() % 10) + 1;
                 printf("You chose Normal\nGuess a number between 1 and 10.");
                 break;
            case 3:
                 srand(time(0));
                 number = (rand() % 100) + 1;
                 printf("You chose Hard\nGuess a number between 1 and 100.");
                 break;
            default:
                    printf("I don't know what you did, but it caused an error");
                    play == 'n';
                    break;
            }
                 
            guessCount = 0;
            while (play != 'n')
            {
                  guessCount++;
                  printf("\nGuess %d: ",guessCount);
                  cin >> guessNum;
                  if (guessNum == number)
                  {
                       printf("%d is right! You got it on try number %d\n",guessNum,guessCount);
                       printf("Play again? (y/n): ");
                       cin >> play;
                       if (play == 'y')
                       {
                          system("cls"); // Clears Screen
                          main();        // Starts main() over
                       }
                       else
                       {
                          play = 'n'; // Exits the loop, instead of using a break; since that only exits
                       }              // this IF statement, NOT the while loop
                  }
                  else if (guessNum > number)
                  {
                       printf("%d is too high",guessNum);
                       if (guessCount >= 10)
                       {
                            printf("\nKeep playing? (y/n): ");
                            cin >> play;
                       }
                       else
                       {
                           continue;
                       }
                  }
                  else if (guessNum < number)
                  {
                       printf("%d is too low",guessNum);
                       if (guessCount >= 10)
                       {
                            printf("\nKeep playing? (y/n): ");
                            cin >> play;
                       }
                       else
                       {
                           continue;
                       }
                  }
                  else
                  {
                      printf("I don't know what the ........ you did but it made an error");
                      break;
                  }
            }
        }
        if (mode == 2)
        {
            printf("Enter a number between 1 and 50: ");
            cin >> number;
            compMax = 50;
            compMin = 1;
            srand(time(0));
            guessNum = (rand() % compMax) + 1;
            guessCount = 1;
            while (play != 'n')
            {
                printf("\nGuess #%d: %d\n",guessCount,guessNum);
                if (guessCount >= 25)
                {
                    printf("Well your number couldn't be guessed");
                    system("PAUSE");
                    break;
                }
                if (guessNum < number)
                {
                    printf("Too Low!\n");
                    compMin = guessNum;
                    compVar = (50 - compMin);
                    guessNum = (rand() % 50) + compVar;
                    if (guessNum <= compMin || guessNum >= 50)
                    {
                        guessNum = (rand() % 50) + compVar;
                        if (guessNum <= compMin || guessNum >= 50)
                        {
                            guessNum = (rand() % 50) + compVar;
                        }
                    }
                    guessCount++;
                    continue;
                }
                if (guessNum > number)
                {
                    printf("Too High!\n");
                    compMax = guessNum;
                    guessNum = (rand() % compMax) + 1;
                    if (guessNum <= compMin || guessNum >= 50)
                    {
                        guessNum = (rand() % compMax) + 1;
                        if (guessNum <= compMin || guessNum >= 50)
                        {
                            guessNum = (rand() % compMax) + 1;
                        }
                    }
                    guessCount++;
                    continue;
                }
                if (guessNum = number)
                {
                    printf("Well it appears your number was guessed on guess %d\n",guessCount);
                    system("PAUSE");
                    break;
                }
            }
        }
        
        cin.ignore();
        system("cls");
        cout << "Hit \"Enter\" to END";
        cin.ignore();
        return 0;   
    }

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Learn how to divide your code into functions. It will help with this kind of stuff.

    Anyway, I've written this type of program out a few times. The idea behind it is that the CPU player needs to have a high limit and a low limit. The limits start at some predetermined numbers. After that, they move dynamically depending upon the guess. If you guess a number, say 10, and it's too low, then you know that the number in question must be greater than 10. So assign 10 to your low limit. Simply put: low = guess; Same concept with the high limit.

    It appears that you do this to some degree, but your guess doesn't appear to be based upon your limits:

    Code:
    compMin = guessNum;
    compVar = (50 - compMin);
    guessNum = (rand() &#37; 50) + compVar;
    Code:
    compMax = guessNum;
    guessNum = (rand() % compMax) + 1;
    Why are you not using your max and min limits in your rand() call properly?
    Last edited by MacGyver; 08-22-2007 at 03:52 AM.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    62
    Why are you not using your max and min limits in your rand() call properly?
    That's the thing, I'm not sure how to construct that...That's why I feel really stupid about this;

    What would the proper way to do it be? Have I got it backwards or something?

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    If you don't know how to do it, get thee to a search engine or something and check up on how rand() works.

    The idea is you should make the random number no larger than your max limit and no smaller than your min limit. You already have those variables. Just put them into your computation of your random number when you assign it to guessNum.

    This is why you should have separate functions. You could write it out in one function and get it right once.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    62
    Okay, after some research on google I found how to properly structure the damn thing;

    Here is what the final code looked like, at least the chunk that was bothering me

    Code:
    srand(time(0));
    guessNum = (rand() &#37; compMin) + (compMax - compMin) + 1;
    I realized the crucial part I was leaving out once I saw this page

    Thanks for your help MacGyver ^_^ Or at least for pointing me in the right direction, this had kept me up all the night the other day...er...night ^_^

  6. #6
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Congrats.

    BTW, remember to only call srand() once per program (once per thread on Windows I believe).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Guessing program Hm. Help
    By snugy in forum C Programming
    Replies: 3
    Last Post: 11-17-2008, 10:09 AM
  2. Random number issue
    By swgh in forum C++ Programming
    Replies: 15
    Last Post: 11-14-2008, 09:12 AM
  3. Issue with program that's calling a function and has a loop
    By tigerfansince84 in forum C++ Programming
    Replies: 9
    Last Post: 11-12-2008, 12:38 PM
  4. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  5. problem with my prime number program
    By datainjector in forum C Programming
    Replies: 4
    Last Post: 07-12-2002, 12:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21