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?
#include <cstdlib>
#include <iostream>
#include <time.h>
usingnamespace 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"<<endl;
int i=0;
//cout<<secret_number; //cheat line :)
do
{
i++;
cout<<"Whats your "<<i<<". guess?"<<endl;
do
{
if (!(cin >> player_guess))
{
user_error1=1;
cout<<"Please enter numbers only!"<<endl;
cin.clear();
cin.ignore(10000,'\n');
}
else {user_error1=0;}
if ((player_guess<0)||(player_guess>100))
{
user_error2=1;
cout<<"Remember the number is between 0 and 100"<<endl;
}
else {user_error2=0;}
} while ((user_error1==1)||(user_error2==1));
if (player_guess>secret_number) {cout<<"Thats too high!"<<endl;}
if (player_guess<secret_number) {cout<<"Thats too low!"<<endl;}
} while (player_guess!=secret_number);
if (i==1)
{
cout<<"Lucky guess! :)"<<'\n';
}
else {cout<<"Congratulations you guessed the secret number in "<<i<<" guesses!\n";}
cout<<"Do you wish to play again? Input 1 for yes 0 for no";
do
{
if (!(cin>>play_again))
{
user_error1=1;
cout<<"Please enter 1 if you wish to play again 0 if you don't!"<<endl;
cin.clear();
cin.ignore(10000,'\n');
}
else {user_error1=0;}
} while (user_error1==1);
} while (play_again==true);
cout<<"Thx for playing! Bye! Press enter to exit.";
cin.ignore(10000,'\n');
cin.get();
return 0;
}
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
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?
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