Hey all, I'm just playing around with code here getting a feel for C++...when i encounter this problem
1 2 3 4 5 6
do{
cout<<"Guess the frequency of the beep? (Between 180 and 500)";cin>>guess;
Sleep(1000);
if (guess == answer) cout<<"Well done! it was 440, Good Game i say."<<endl;
elseif (guess > answer) cout<<"Nope, lower than that"<<endl;
elseif (guess < answer) cout<<"Nope, higher than that"<<endl;} while (guess != answer);
It's a basic good loop when i'm putting numbers in however when i guess "a" or any other char it goes into crazy mode constantly repeating "nope, lower than that"
I know that this is because guess is an int..but is there a way for me to get out (return 0) if a char is entered?..ive had a go with at looking on the net and a few other things but no luck...Cheers Benny
cin object can be converted to a boolean that indicates whether an error occurred. After the error is encountered, if you want to use cin again, you have to call cin.clear() and probably cin.ignore(numeric_limist<streamsize>::max(), '\n') to clean all the rubbish in it.
example: http://www.cplusplus.com/forum/beginner/28223/#msg151683
#include <iostream>
#include <string>
#include <windows.h>
#include <stdlib.h>
#include <time.h>
#include <mmsystem.h>
#include <limits>
#define answer 440
usingnamespace std;
int main()
{
bool valid = true;
int guess;
char yesorno;
string myname;
cout << "Hello i'm Benny!, whats your name?" << endl;
getline (cin, myname);
cout<<"Hello "<<myname<<" nice to meet you."<<endl;
Sleep(1000);
cout<<"Want to play a game? "; cin>>yesorno;
if (yesorno == 'y'); else {cout<<"Bog off then"; return 0;}
cout<<"Ok then, 2 seconds"<<endl;
Sleep(3000);
Beep(440, 1000);
cout<<"Guess the frequency of the beep?..The value is Between 180 and 500"<<endl<<""<<endl;
Sleep(1000);
do {
do { cout<<"Guess ";cin>>guess;
if(cin>>guess);
else {cout<<"That was not even a number"<<endl; valid = false; cin.clear();cin.ignore(numeric_limits<streamsize>::max(), '\n');}
if (guess > 500 || guess < 180) {valid = false; cout<<"The value is between 180 and 500..i've just said that!"<<endl;}}
while (!valid);
Sleep(1000);
if (guess == answer) cout<<"Well done!, it was 440..good game old chap."<<endl;
elseif (guess > answer) cout<<"Nope, lower than that"<<endl;
elseif (guess < answer) cout<<"Nope, higher that that"<<endl;
} while (guess != answer);
system("pause");
return 0;
}
Problem ive got now is.. A:There are no compile errors (even though ive got a problem) and
B: When i run it, after the first bit of code (name,want to play ect..) it comes up with "Guess", then if i input "300" it should say "nope, higher than that" but instead the cursor goes to a new line and doesn't do anything but flash (its not fully crashing) and it's very strange.
You wouldn't know whats going on here would ya?..Cheers Benny
lines 32 and 33 both have cin>>guess in them, so you're asking yourself to input something twice. There is nothing wrong with having your statements inside an if(). They still work..