I have some programming experience, but I'm still pretty new at C++. As one of my first programs I created a basic guessing game. I use #define to specify the bounds for the random number, but when I try and create the number it gives me a syntax error. I found out that it doesn't like it when I use UPPER_BOUND in the creation of the random number. If I create a new variable and set it to the same value, then it works, but I shouldn't have to create another variable when UPPER_BOUND is already defined.
Anyway, here's what I have. The code works, but I want to replace lines 26-27 with line 24. For some reason trying to use line 24 instead gives me a syntax error.
Any idea why I just can't use what I've already defined?
Code:#include <iostream> #include <ctime> // time() #include <cstdlib> // rand() and srand() #define UPPER_BOUND 100; #define LOWER_BOUND 1; #define 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; cout << " and " << UPPER_BOUND; cout << "." << endl; // Generate a random number; LOWER_BOUND < r < UPPER_BOUND srand(time(0)); // game.target = (rand() % UPPER_BOUND) + LOWER_BOUND; <-- THE PROBLEM LINE 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 guess: cout << "(" << game.guesses << " guesses 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; goto finished; } game.guesses--; if (game.guesses == 0) { // Ran out of guesses goto finished; } goto guess; finished: if (correct) { cout << "Congratulations! The answer was " << game.target << ".\n"; } else { cout << "Sorry, the correct answer was " << game.target << ".\n"; } }