Skip to main content
gamedev.net
🔒 Locked

code help please

Started by macken04 Mar 27, 2010 at 9:57 AM 1 replies 400+ views
Original Post
macken04
macken04
hi i have been looking through the forms and have gotten some help (thanks too many here for other code posted). i bought beginning c++ 2nd edition. the 2nd chapter has a walk through of a random number guessing game. i am doing the third question where i must input a number and the computer has to guess it, cutting the possibilities down by higher and lower. i have gotten about as far as i can. it runs, however after i input my selection the computer make a guess not within my parameters (1-100). then shuts down. here is my code, any and all help you's can give is greatly appreciated as i am quite stuck

// Guess my number
// The classic number quessing game

#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;

int main()
{
    
    
    // declaring variables
    int highRange = 100, lowRange = 0; // variables for the ranges
    int tries = 0;// coutn amount of tries
    int range = (highRange - lowRange) + 1;// this will allow me to change the variables the computer generates from, when one is chaged for beeing to low or high
    int theNumber = rand() % range + lowRange; // using the forula above, into the random function 
    int myNumber;// variable to store my number
    cout <<"\nHi im going to guess your number\n ";
   
      //user enters a number and the computer will produce a number
         cout<< "\nGive me you number:";
         cin >> myNumber;
    
  
    do
    {
       
         cout <<"\nMy guess is" << theNumber <<
         tries++;
         
         if (theNumber == myNumber)
         {
                       cout << "\nI think i got it woho";
                       cout << "\nit took me" << tries++ <<"goes";  
                       break;
         }
         
         if (theNumber > myNumber)
         {
                     
                       theNumber = lowRange;// here the lowest possible number feed into the random generator is now made the computer 1st selection
                       continue;//allows the do loop to repeat
         }             
         
         
         
         if (theNumber < myNumber)
         {
                       theNumber = highRange;// again the variable theNumber is applied to highRange so i cant make a number higher than that
                       continue;
         }
         
         
    }while (theNumber =!myNumber);// loop must run once, this allows that and makes sure it is ok to run again
    
    system ("PAUSE");
    return 0;


}
[/sorce]


[Edited by - Zahlman on March 27, 2010 12:06:43 PM]
Waaayoff
Waaayoff
There you go :) Study it carefuly (i didn't change the comment lines, but if there's something you didn't get, let me know.)

By the way, i replaced the headers and signs( >, <, >>, <<) to get them to work on my compiler.

Also, your code doesn't keep track of guesses so it sometimes repeats a guess more than once. You should look into that

// Guess my number// The classic number quessing game#include <iostream>#include <cstdlib>#include <ctime>using namespace std;int main(){       // declaring variables    int highRange = 100, lowRange = 0; // variables for the ranges    int tries = 0;// coutn amount of tries    // No need for the range variable, i removed it    int theNumber = rand() % (highRange - lowRange + 1) + lowRange; // using the forula above, into the random function     int myNumber;// variable to store my number    cout << "\nHi im going to guess your number\n ";         //user enters a number and the computer will produce a number         cout << "\nGive me you number:";         cin >> myNumber;      do    {              theNumber = rand() % (highRange - lowRange) + lowRange;          cout << "\nMy guess is" << theNumber;         tries++;                  if (theNumber == myNumber)         {                       cout << "\nI think i got it woho";                       cout << "\nit took me" << tries++ << "goes";                         break;         }                  else if (theNumber < myNumber)			lowRange = theNumber;// here the lowest possible number feed into the random generator is now made the computer 1st selection                                               else if (theNumber > myNumber)			 highRange = theNumber;// again the variable theNumber is applied to highRange so i cant make a number higher than that                      }while (theNumber != myNumber);// loop must run once, this allows that and makes sure it is ok to run again        system ("PAUSE");    return 0;}


Don't forget to rate me :)
"Spending your life waiting for the messiah to come save the world is like waiting around for the straight piece to come in Tetris...even if it comes, by that time you've accumulated a mountain of shit so high that you're fucked no matter what you do. "
macken04
macken04
thank you, im going to open the book again 2mo. thanks for the help. made sure i gave u a rating

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.