# #define problem

• 10-27-2008
reaperman
Guessing Game (formerly #define problem)
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";         } }```
• 10-27-2008
Dave_Sinkula
Lose the semicolons.
Code:

```#define UPPER_BOUND 100 #define LOWER_BOUND 1 #define INIT_GUESSES 10```
• 10-27-2008
anon
The defines shouldn't end with a semicolon. A define is just textual replacement, so after the macros are expanded the problem line looks like this:

Code:

`game.target = (rand() &#37; 100;) + 1;;`
Incidentally, it might be better simply to use constants, so that the code you have typed in is actually the same as the compiler is compiling.
Code:

```const int upper_bound = 100; const int lower_bound = 1;```
It is also somewhat strange that you'd know about structs but use several goto labels for a perfectly simple loop:
Code:

```//game loop do {     ...     --game.guesses; } while (game.guesses); //after the loop```
Also, I believe your actual formula for random numbers in a range is wrong, it just so happens to work when LOWER_BOUND is one and not in any other case. It should look more like:
Code:

```random = rand() % (upper_bound - lower_bound) + lower_bound; //upper_bound not included //or random = rand() % (upper_bound - lower_bound + 1) + lower_bound; //to include upper_bound```
• 10-27-2008
reaperman
I feel stupid.

Thanks.
• 10-27-2008
reaperman
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() &#37; 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; }```
• 10-27-2008
C_ntua
OK, maybe is stupid to fix a comment but I ll mention this anyway.
Code:

`// Generate a random number; LOWER_BOUND <= r <= UPPER_BOUND`
But, your only mistake is the way you get input.
cin >> guess;
will get one int and leave a '\n' behind! The next cin >> guess will try to read the '\n'. To avoid this add a cin.ignore(). This will ignore the remaining '\n'. I strongly advise to check what happens if any character is entered and if only a '\n' is entered to see if your program handles those data correctly. See also what happens if you have a something <space> something entered.
• 10-27-2008
CornedBee
Code:

`game.target = (rand() &#37; UPPER_BOUND) + LOWER_BOUND;`
Your logic is broken here. Think carefully about the possible result if we say, for example
Code:

```const int LOWER_BOUND = 5; const int UPPER_BOUND = 10;```
Or just try it out a few times and then think about what went wrong.
• 10-27-2008
reaperman
I borrowed the code to generate the number from a site online. It may not be exactly accurate, but I'm going to focus first on making sure I get all other errors out, then I'll focus on making the number more accurate.

Anyway, I found a method online to deal with having a character entered and not a number, but it's not perfect. Anyone have a better way to do it, preferably with an example to demonstrate?

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): ";                 bool numberChosen = false;                 while (!(cin >> guess)) {                         cin.clear();                         while (cin.get() != '\n') {}                         cout << "Try again: ";                 }                 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; }```