/************************************************************ Name: Matt Bolduc Date: Jan 12 2005 Project: #2 Section: # Put your name, date, project, file name and section number here. Describe the overall object of the project. Picks a number from 1-100 for the user to guess. **********************************************************/ #include // needed for cin and cout operations #include // needed to create a random seed using namespace std; //selects a name space required to recognize cin and cout void main() { // declare all your variables srand(time(NULL)); // seed a random number based on time int inum2guess = 1 + (rand() %100);// gets the number to guess 1 to 100 // declare an integer variable to hold your guesses char CurrentGuess[5]; int CurrentGCount = 1; // declare an integer variable to hold the number of guesses bool valid = false; // output the instructions to the game: 0 to give up; 1-100 to guess cout << "I will pick a number from 1 to 100.\nYour goal is to guess what it is in as few trys possible.\nYou may quit at any time by typing in a 0\nor any letter for your guess.\n"; // cause the code in the braces to loop forever until a return is executed // the return will end the program while(true) // I like while loops more. ==>// cause the code in the braces to loop forever until a return is executed {// prompt for user to guess then recieve the guess cout << "\nEnter your *" << CurrentGCount<< "* guess: "; cin >> CurrentGuess; while (!valid) { if ((atoi(CurrentGuess) >= 0) && (atoi(CurrentGuess) <= 100)) break; cout << "Guess not valid, enter an integer: "; cin >> CurrentGuess; } if ( atoi(CurrentGuess) == 0) { cout << "\n\nYou chose to quit\n\n"; break; } else if (atoi(CurrentGuess) > inum2guess) // check to see if guess < 1 and if so, quit { cout << "The number is lower than " << atoi(CurrentGuess) << "\n"; // output a message } else if ( atoi(CurrentGuess) < inum2guess) // check to see if guess < 1 and if so, quit { cout << "The number is higher than " << atoi(CurrentGuess) << "\n";// output a message } else if ( atoi(CurrentGuess) == inum2guess) // check to see if guess < 1 and if so, quit { cout << "*****You got it in " << CurrentGCount << " tries*****\n\n";// output message break; // break loop } CurrentGCount++; }// end of forever loop }// end of main