Step Twelve Guess The Number
Design a program called guess.cpp. This program will create a guessing game by picking
a random number from 1 to 100, and then allow the user to guess until they discover the
correct answer. The computer should respond with something like "Too Low", "Too High" or
"You got it!"
#include <iostream>
using namespace std;
int random(int low, int high) {
return ...;
}
int main(void) {
int count = 0; //keep track of how many guesses it takes the user
srand(time(0)); //seed the random number generator
//have the computer pick a random # from 1 to 100
int magic_number=random(1,100);
cout << "GUESS A NUMBER (1-100)" << endl;
int guess = -1000;
while (guess != magic_number) {
cout << "Enter a guess: " << endl;
cin >> guess;
if ... cout << "Too small!" << endl;
else if ... cout << "Too big!" << endl;
else ... cout << "You got it in " << count << " guesses!" << endl;
}
return 0;
}
Advance to Step Thirteen
Return to the Table of Contents