Here's the new version. I don't really have any other questions to ask regarding the code, but if anyone has any other suggestions on how to make it better, I would like to know.
Code:
#include <iostream>
#include <ctime> // time()
#include <cstdlib> // rand() and srand()
const int UPPER_BOUND = 100;
const int LOWER_BOUND = 1;
const int INIT_GUESSES = 10;
using namespace std;
struct guessingGame {
int target;
int guesses;
} game;
int main () {
// Initial welcome message
cout << "I'm thinking of a number between " << LOWER_BOUND << " and " <<
UPPER_BOUND << ".\n";
// Generate a random number; LOWER_BOUND < r < UPPER_BOUND
srand(time(0));
game.target = (rand() % UPPER_BOUND) + LOWER_BOUND;
/*
int up = UPPER_BOUND; // I want to replace these two lines with the one above
game.target = (rand() % up) + LOWER_BOUND;
*/
game.guesses = INIT_GUESSES;
// Initialize some variables
int guess;
bool correct = false;
// The guessing section
do {
if (game.guesses != 1)
cout << "(" << game.guesses << " guesses left): ";
else
cout << "(1 guess left): ";
cin >> guess;
if (guess < game.target) {
cout << "Higher." << endl;
} else if (guess > game.target) {
cout << "Lower." << endl;
} else {
// The guess was correct
correct = true;
break;
}
game.guesses--;
} while (game.guesses > 0);
if (correct)
cout << "Congratulations! The answer was " << game.target << ".\n";
else
cout << "Sorry, the correct answer was " << game.target << ".\n";
return 0;
}