#define problem

This is a discussion on #define problem within the C++ Programming forums, part of the General Programming Boards category; I have some programming experience, but I'm still pretty new at C++. As one of my first programs I created ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    8

    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";
    	}
    }
    Last edited by reaperman; 10-28-2008 at 10:48 AM.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Lose the semicolons.
    Code:
    #define UPPER_BOUND 100
    #define LOWER_BOUND 1
    #define INIT_GUESSES 10
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    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
    Last edited by anon; 10-27-2008 at 04:29 PM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    8
    I feel stupid.

    Thanks.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    8
    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;
    }
    Last edited by reaperman; 10-27-2008 at 04:36 PM.

  6. #6
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    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.

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    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.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    8
    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;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help with Bitmap Display
    By The Brain in forum Windows Programming
    Replies: 7
    Last Post: 03-23-2009, 05:33 AM
  2. Help me with function call
    By NeMewSys in forum C++ Programming
    Replies: 16
    Last Post: 05-22-2008, 01:53 PM
  3. C problem with legacy code
    By andy_baptiste in forum C Programming
    Replies: 4
    Last Post: 05-19-2008, 06:14 AM
  4. LISP (DrScheme) any one?
    By Jeremy G in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 03-31-2004, 11:52 AM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 05:05 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21