-
a problem with cin......
this code works ok until i type say a letter. like if i typed 789 it works fine. if i type say the letter a or a word or anything with a char in it, it then tells me that the monster doesn't exist, which it should, but then repeatedly keeps looping through my program coming to this function and telling me that the monster doesn't exist. Surely there is a way to make sure a number is typed and not a letter?????
Thanks for your time..... :D
werdy666
Code:
void viewmonsterstat()
{
int answer=0;
cout << "Which monsters stats would you like to view?" << endl;
cout << "--------------------------------------------" << endl << endl;
cout << "1. " << bad[0].name << endl;
cout << "2. " << bad[1].name << endl;
cout << "3. " << bad[2].name << endl;
cout << "4. " << bad[3].name << endl;
cout << "5. " << bad[4].name << endl << endl << endl;
cin >> answer ;
switch(answer)
{
case 1:
viewcurrentmonster(answer-1);
return;
break;
case 2:
viewcurrentmonster(answer-1);
return;
break;
case 3:
viewcurrentmonster(answer-1);
return;
break;
case 4:
viewcurrentmonster(answer-1);
return;
break;
case 5:
viewcurrentmonster(answer-1);
return;
break;
case 0:
default:
cout << "That monster doesn't exist!" << endl;
Sleep(1000);
return;
break;
}// end switch
} // end function
-
do{
cin >>answer;
}while (answer>5);
-
No, thats not it.
cin is expecting an integer. you don't give it one. cin is sad.
The problem is, the unintelligible gibberish (your letter) is still in the hopper (stdin) and is waiting to be processed.
You are gonna want to cin.ignore() it, so that the next time cin looks to stdin, it isn't still waiting there to confound it again.
-
yeah the do loop doesn't work, i didn't think it would.
Could you please show me a quick example of how i was use cin.ignore() to make sure that if letters are typed that it wouldn't go through the loop in my function?
Where on the net can i find examples/documentation of things like cin.ignore() ??
Thanks for helping a newbie! :)
-
wouldn't it just read the character as its integer equivalent?
-
No.. because operator<<(istream&, int) is specially set up to accept integers. Characters just bork it.
As for cin.ignore(), basically it is this:
cin.ignore({number}, {delimiter});
Will dump the first {number} characters or dumb all characters until it hits a {delimiter}, whichever comes first. So a good implementation might be:
cin.ignore{255, '\n');
That would dump everything that is stuck in the buffer up until the newline.
-
By the way put that right after cin << answer;
-
-
To remove every character from the input stream:
while (std::cin.rdbuf()->in_avail() > 0) std::cin.get();
-
Try this.
Code:
void viewmonsterstat()
{
int answer=0;
do {
cout << "Which monsters stats would you like to view?" << endl;
cout << "--------------------------------------------" << endl << endl;
cout << "1. " << bad[0].name << endl;
cout << "2. " << bad[1].name << endl;
cout << "3. " << bad[2].name << endl;
cout << "4. " << bad[3].name << endl;
cout << "5. " << bad[4].name << endl << endl << endl;
cin >> answer ;
if (cin.fail())
{
cin.clear();
cin.ignore();
}
} while (answer == 0);
switch(answer)
{
case 1:
viewcurrentmonster(answer-1);
return;
break;
case 2:
viewcurrentmonster(answer-1);
return;
break;
case 3:
viewcurrentmonster(answer-1);
return;
break;
case 4:
viewcurrentmonster(answer-1);
return;
break;
case 5:
viewcurrentmonster(answer-1);
return;
break;
case 0:
default:
cout << "That monster doesn't exist!" << endl;
Sleep(1000);
return;
break;
}// end switch
} // end function
-
Thankyou swoopy! that works perfect!
:cool: :D
Werdy666