Guessing Program

This is a discussion on Guessing Program within the C++ Programming forums, part of the General Programming Boards category; The program listed below lets a user input a state, and the program tells the user if that is the ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    36

    Guessing Program

    The program listed below lets a user input a state, and the program tells the user if that is the state that the program randomly picked (from the enumeration block)...but I'm having a hard time getting to work. Any ideas?

    Code:
    #include <iostream>
    #include <string>
    #include <stdlib.h>
    #include <time.h>
    
    using namespace std;
    
    enum block {ohio, texas, maine};
    void guess(string& input, int& computer_number);
    block states;
    
    int main() {
        srand(time(0));
        int computer_number = rand()%3;
    	static_cast<block>(computer_number);
    	string input;
    
    	cout << "Enter the state: ";
    	cin >> input;
    
    	guess(input, computer_number);
    
    	return 0;
    }
    
    void guess(string& input, int& computer_number) {
        if (input == computer_number) {
    		cout << "You guessed the state!";
    	} else {
    		cout << "You did not.";
    	}
    }

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    A few things about your program.

    The line

    Code:
    static_cast<block>(computer_number);
    doesn't actually do anything since you don't return the value of the cast. It is a pointless cast anyway. Also, in your guess function you can't compare an integer to a string. It doesn't make sense. You need to have something like this below your enumeration

    Code:
    static const string blockNames[] = { "ohio", "texas", "maine" };
    Then in your guess function you can index into that array based on the random integer you generate like this:

    Code:
    if(input == blockNames[computer_number])
    You might want to think about converting the users string to lower-case because "Ohio" wouldn't compare equally with "ohio".

    Any other questions let us know.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    I re-wrote the program like this and it's working a lot better than the previous one. Instead of states, I used colors. The only problem now is that you can enter every color, but none of them will be the right one. Also, at times, if you enter "blue" as your first input, it immediately tells you that you won, and the program ends.

    Code:
    #include <iostream>
    #include <string>
    #include <stdlib.h>
    #include <time.h>
    
    using namespace std;
    
    enum colors {red, yellow, blue, green, orange, purple};
    colors compGuess;
    colors guess;
    
    int main() {
    	int count = 0;
        srand(time(0));
        int computer_number = rand()%6;
    	static_cast<colors>(computer_number);
    	bool boolean = false;
    	string input;
    
    	cout << "Enter the color: ";
    	cin >> input;
    
    	if (input == "red") {
    		guess = red;
    	}
    	if (input == "yellow") {
    		guess = yellow;
    	}
    	if (input == "blue") {   
    		guess = blue;
    	}
    	if (input == "green") {
    		guess = green;
    	}
    	if (input == "orange") {
    		guess = orange;
    	}
    	if (input == "purple") {
    		guess = purple;
    	}
    
    	while (boolean == false) {
    		if (guess == computer_number) {
    			cout << "You guessed it!" << endl;
    			cout << "It took you " << count << " times." << endl;
    			boolean = true;
    		} else {
    			cout << "Wrong color.  Try again..." << endl;
    			cout << "Color: ";
    			count++;
    			cin >> input;
    		}
    	}
    
    
    	return 0;
    }
    Any other ideas would be great. Thanks for the help.

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    First you should be using if-else style comparisons rather than a bunch of if's. Because if you can't have two colors the user wants, just one.

    But that aside, it looks like it would work except when the user gets it wrong. You store their input in 'input' but don't recalculate the 'guess' variable by running through the if's again. So it will always be wrong.

    If you had set it up the way I had mentioned it would be much simpler.
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  5. #5
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    Ah, okay it works now. Having the if statements the way they are aren't going to cause bugs in the program, will they?

    How can I do some checking for the first character being capitalized or not? So if the user enters Red, it'll count as red. I tried adding some more if's and using the input[0], but once I did that, the program wouldn't work anymore.

    Thanks for your help.

  6. #6
    Registered User
    Join Date
    Oct 2004
    Posts
    32
    Quote Originally Posted by webren
    How can I do some checking for the first character being capitalized or not? So if the user enters Red, it'll count as red. I tried adding some more if's and using the input[0], but once I did that, the program wouldn't work anymore.
    Instead of checking the first character, just convert the input to either upper or lower case and then check that against the answer. That way it will be easier for you to control the test.

  7. #7
    Registered User
    Join Date
    Apr 2005
    Posts
    36
    Ah, good call. Thanks for the advice, tripper.

    Also, thanks for all your help, Mr. Wizard.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 10:17 PM
  2. Can someome help me with a program please?
    By WinterInChicago in forum C++ Programming
    Replies: 3
    Last Post: 09-21-2006, 10:58 PM
  3. program won't run standalone
    By richard in forum C++ Programming
    Replies: 2
    Last Post: 07-20-2002, 06:21 PM
  4. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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