Original Post
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 [Edited by - Zahlman on March 27, 2010 12:06:43 PM]
// 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]