Thread: #define problem

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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.

  2. #2
    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.

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, 12:52 PM
  5. My graphics library
    By stupid_mutt in forum C Programming
    Replies: 3
    Last Post: 11-26-2001, 06:05 PM