im trying to write a fairly simple program where the computer randomly chooses a number between 1-100, and the user trys to guess it in 7 go's. I can do it alright, but if i accidently entered a letter instead of a number for my guess, it went completly spastic. I need help trying to make sure thta the user does enter a number, and if they dont, i want to tell them so and ask them to try again.
I have tried this using arrays of characters, and checking each character for its decimal value... but it doesnt seem to work, and my guesses are always somehow made to equal some werid number, normally about 4950, even if i only input 3.
My code for this is here:
int g_input_guess()
{
char guess[4];
int a=0,b=0,c=0,z=0;
cin >> guess;
for(int i = 0;i<4;i++)
{
cout << "guess i is: " << guess; // this outputs some weird stuff... like nothing!! so im not sure, it appears
//that the guess doesnt actually get put in the array or something.
if(guess==113)
{ return -1; }
if(guess>58&&guess<48)
{ cout << "Please enter a real number!\n"; return -2; }
Apparently a single Reddit user posted a bunch of made up stuff about it being spyware and Steam users started review bombing it like mindless sheep. People have read the actual updated EULA and said that there's nothing notably different in it and nothing pointing to it being spyware. (replied to wrong post)
These gains are probably moot though if you use a Gecko based browser that can support adblocking on a much deeper level than Manifest V3 limits you to. The speed benefits from excellent blocking of careless ad networks are just ridiculous and instantly felt.
While these updates may improve loading performance by a few percents after a lot of hard engineering work, blocking ads on a deep level ahead of even loading them can instead easily cut several megabytes in one go.
Question
Ianbobisveck
im trying to write a fairly simple program where the computer randomly chooses a number between 1-100, and the user trys to guess it in 7 go's. I can do it alright, but if i accidently entered a letter instead of a number for my guess, it went completly spastic. I need help trying to make sure thta the user does enter a number, and if they dont, i want to tell them so and ask them to try again.
I have tried this using arrays of characters, and checking each character for its decimal value... but it doesnt seem to work, and my guesses are always somehow made to equal some werid number, normally about 4950, even if i only input 3.
My code for this is here:
int g_input_guess()
{
char guess[4];
int a=0,b=0,c=0,z=0;
cin >> guess;
for(int i = 0;i<4;i++)
{
cout << "guess i is: " << guess; // this outputs some weird stuff... like nothing!! so im not sure, it appears
//that the guess doesnt actually get put in the array or something.
if(guess==113)
{ return -1; }
if(guess>58&&guess<48)
{ cout << "Please enter a real number!\n"; return -2; }
}//end for
a = guess[0]*100;
b = guess[1]*10;
c = guess[2]*1;
z = a+b+c;
return z;
}
any help would be *very* appreciated. Thanks
Link to comment
https://www.neowin.net/forum/topic/475694-c-input-screening/Share on other sites
8 answers to this question
Recommended Posts