#### Archived

This topic is now archived and is closed to further replies.

# Anyone care to show me where the heck I went wrong here?

This topic is 5528 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

OK, I''ve been working on a small practice program. It''s nothing much at all, it picks a random number (between 1 and 3 for testing purpose), then prompts the user to select a number in the range given. If they are right, it tells them and ends.. If they are wrong, it tells them and ends.. If they choose a number outside of the range (1-3 in the sample), they are prompted to enter another number. That is where my problem is...I broke everything down into seperate functions..well, most in any case. Here is the code.. // Pick-a-number.cpp // #include <iostream> // required for ''cout'' #include <conio.h> // required for ''getch'' #include <time.h> #include <stdlib.h> using namespace std; // activates the standard namespace int SetNumber(); int GetGuess(); void DisplayOutput(int guess, int number); int main() { int number = SetNumber(); int guess = GetGuess(); do { if (guess == 0) { cout << endl << "OK, quitting now." << endl; break; } else if (guess == number) { cout << endl << "Whoa! You got it!" << endl; break; } else if ((guess > 3) || (guess < 1)) { cout << endl << "I said a number from 1 to 3, try again." << endl; GetGuess(); } else { DisplayOutput(guess, number); break; } } while ((guess!=0) && (guess>0&&guess<4) && (guess!=number)); cout << endl << "Press any key" << endl; getch(); return 0; } int SetNumber() { srand( (unsigned)time( NULL ) ); int number = ((rand() % 3) +1); return number; } int GetGuess() { cout << endl << "Choose a number from 1 to 3. Choose 0 to stop playing." << endl; cout << endl << "What is your choice? : "; int guess; cin >> guess; return guess; } void DisplayOutput(int guess, int number) { cout << "You chose " << guess << " as your number. It was in fact, " << number << "." << endl; } Everything works perfectly except when they initially choose an invalid number, like 4, they are prompted to choose again (Which is correct), but no matter what they select they fall out of the code and end. Anyone have any ideas where I went wrong? I''m a bit stumped. If they choose a wrong number, they should be redirected back to the GetGuess() function so they can choose a new number, but it gets messed up. Elp!?!?! Thanks! ~~ Trakkur Never try to argue with an idiot, they''ll only pull you down to their level...and beat you with experience.

##### Share on other sites
else if ((guess > 3) || (guess < 1)){cout << endl << "I said a number from 1 to 3, try again." << endl;GetGuess();}

GetGuess() doesn't do anything here. It only returns the guessed number, it does not change the variable "guess" in main()'s scope.
guess = GetGuess(); should work.

[edited by - micepick on October 22, 2002 3:15:48 PM]

##### Share on other sites
Hmm, tried that...no change at all.

Never try to argue with an idiot, they''ll only pull you down to their level...and beat you with experience.

##### Share on other sites
To tell you the truth, I'm not sure what's going on but it looks like your main loop needs some re-doing.

1) Change that ugly if-structure to a switch statement, unless you change your loop around significantly.
2) Make the do...while loop a while(1) loop. Or better yet, while(guess). The way it is, you are performing a lot of unnecessary tests that are also probably the cause of your problem.
3) I would test for an out of bounds guess inside the GetGuess function.
4) You definitely don't need a separate function to display output.

[edited by - micepick on October 22, 2002 3:44:52 PM]

##### Share on other sites

    int main(){    int number = SetNumber();    int guess;    do{         guess = GetGuess();        if(guess == 0)                cout << endl << "OK, quitting now." << endl;        else if (guess == number)                cout << endl << "Whoa! You got it!" << endl;        else if ((guess > 3) || (guess < 1))                cout << endl << "I said a number from 1 to 3, try again." << endl;        else{                DisplayOutput(guess, number);                break;        }    }while ((guess!=0) && (guess!=number));    cout << endl << "Press any key" << endl;    getch();    return 0;}

The problem with your original version was that:
1) The user makes a guess
2) The guess was "out of range" (x < 1 || x > 3)
3) The user is asked to try again
4) User enters another value, but the input is ignored
5) The while-condition will be false, because "guess>0&&guess<4" is false. Program exits

Micepick's original idea will also fail because:
1-3) The same as above
4) User enters another value into guess
5) While-condition will only become true if the users new guess is a valid, and INcorrect number.

[edited by - mbulow on October 22, 2002 4:11:48 PM]

##### Share on other sites
Aha!

Thanks mubulow...I was SOOO close.

Thanks much, it was making me NUTS!

Trakkur
--------------------------

Never try to argue with an idiot, they''ll only pull you down to their level...and beat you with experience.

##### Share on other sites
Now to add a fixed number of attempts.

Later!

Trakkur

Never try to argue with an idiot, they''ll only pull you down to their level...and beat you with experience.

##### Share on other sites
I always miss the most obvious things. I didn't even look to see if he was getting new input. I hang my head in shame.

[edited by - micepick on October 22, 2002 4:36:55 PM]

• ### Forum Statistics

• Total Topics
628634
• Total Posts
2983964

• 10
• 18
• 20
• 13
• 9