### Just need some feedback.

Hello World!

I recently (2 months ago) decided to learn programming and started with C++ and i would like to get some feedback on this little number guessing game i wrote. Are there any newbie mistakes in it that should be corrected or is there a better/more efficient way of writing this? (i mean things like this: before finding this forum the last line was system ("PAUSE"); :)

What the code does: generates a random number between 0 and 100 and the user has to guess it, i experimented with writing it "fool proof" so the program wont go mad if you imput apple instead of a number. If you find a way to crash it or make it go mad plz let me know (only what you did to get an error, not the solution THX)

How can i write it so that it wont wait for pressing enter after the 0 or 1 when it asks if you want to play again?

 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667`` ``````#include #include #include using namespace std; int main() { srand(time(NULL)); bool play_again=true; do { int user_error1=0; int user_error2=0; int player_guess; int secret_number=rand()%101; cout<<"Guess the number! It's between 0 and 100"<> player_guess)) { user_error1=1; cout<<"Please enter numbers only!"<100)) { user_error2=1; cout<<"Remember the number is between 0 and 100"<secret_number) {cout<<"Thats too high!"<>play_again)) { user_error1=1; cout<<"Please enter 1 if you wish to play again 0 if you don't!"<
Looks good.

Let's puzzle you.
1. What will happen when user enters "5Good" and random number is "5".
2. What will happen when user enters "7Number" and random number is "70"
3. What will happen when user enters "Number8" and random number is "8"

 How can i write it so that it wont wait for pressing enter after the 0 or 1 when it asks if you want to play again?

I don;t think there is any standard C++ function to do this, you can use plain C function
 ``12`` ``````#include play_again = getch();``````
A nice next step would possibly be moving the code into a class, so your main might look something like this:

 ``1234567`` ``````int main() { Game myGame; myGame.Run(); return 0; }``````

Santosh

i didnt test it with a mix of letters and numbers...

what puzzles me is line 26
correct me if im wrong: this line should check if the variable player_guess is an integer
if (!(cin >> player_guess))
but when it gets "67asd" as input it still considers player_guess to be in integer format
what am i missing here?

mutexe

i just finished reading the Compound Data Types chapter, so atm i'm not familiar with classes. (still trying to figure out pointers) I saw it a lot that people try to write their program the way you suggest. Why is this a better solution then writing the code inside the main function?
Last edited on
 if (!(cin >> player_guess))
This will return the istream (cin) itself, which can never be 0, so the if is always true.

The correct way would be to
 ``12345`` `````` cin >> player_guess; if(player_guess > 0) { ... }``````

 but when it gets "67asd" as input it still considers player_guess to be in integer format

this is the beauty of the >> operator
@Santosh cin can be true or false, depending on what happened during the input attempt.
I'm trying to write some code that will only accept numbers and numbers only as input, i believe i got this part right but i encountered a strange error when converting the string of characters to an integer.
I check every character entered and if one has a vale thats not in the 48-57 range (these are the character values of 0-9 in the ASCII table) it will throw an error message and wait for another input.
The bug is when i convert the string of characters to an integer (line 28) i will get the following outputs for x. If the character string is 3 chars long it gives back a wrong value otherwise it functions as intended. It probably has to do something with the pow function im using but cant figure out what the problem is

in out
7 7
75 75
756 755
7564 7564

 ``123456789101112131415161718192021222324252627282930313233`` ``````#include #include using namespace std; int main() { string input=""; bool user_error=0; int x=0; do { x=0; user_error=0; getline(cin,input); for (int i=0;i57)) { cout<<"Please enter numbers only!\n"; user_error=1; break; } else {int p=input.size()-i-1;x+=(c-48)*pow(10.0,p);} //cout<