Clear Buffer
May 16, 2019 at 12:54pm UTC
Hello,
I'm trying to prevent error from user input but when i check if the values are compatible into the if statement it always accept it as compatible types. How can I control the char input to only accept 'y'and 'n'? Thanks!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
char answer;
//wrong entry type prevention
if (cin >> answer){
cin.clear();
cin.ignore(10000, '\n' );
cerr << "Inform your option again (y/n) " ;
cin >> answer;
}
int numberOfNodes;
cout << "How many entries: " ;
//wrong entry type prevention
if (!(cin >> numberOfNodes)){
cin.clear();
cin.ignore(10000, '\n' );
cerr << "Say again the amount of entries: " ;
cin >> numberOfNodes;
}
May 16, 2019 at 1:16pm UTC
In your currently existing logic, how is the program supposed to know that 'y' and 'n' have different meanings than any other possible characters?
I would do something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
// Example program
#include <iostream>
#include <string>
#include <limits>
int main()
{
using namespace std;
char answer;
cin >> answer;
//wrong entry type prevention
while (!cin || (answer != 'y' && answer != 'n' )){
cin.clear();
cin.ignore(std::numeric_limits<streamsize>::max(), '\n' );
cerr << "Inform your option again (y/n) " ;
cin >> answer;
}
cout << "Your answer: " << answer << '\n' ;
int numberOfNodes;
cout << "How many entries: " ;
//wrong entry type prevention
while (!(cin >> numberOfNodes)){
cin.clear();
cin.ignore(10000, '\n' );
cerr << "Say again the amount of entries: " ;
cin >> numberOfNodes;
}
cout << "Number of entries: " << numberOfNodes << '\n' ;
}
Last edited on May 16, 2019 at 1:16pm UTC
May 16, 2019 at 1:16pm UTC
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include <iostream>
#include <cctype>
bool answer_is_yes_to( const char * question )
{
std::cout << question << "? [y/n]: " ;
char ans ;
std::cin >> ans ;
ans = std::tolower(ans) ; // convert ans to lower case
if ( ans == 'y' ) return true ;
if ( ans == 'n' ) return false ;
std::cout << "invalid input. try again\n" ;
std::cin.ignore( 10000, '\n' ) ;
return answer_is_yes_to(question) ;
}
int get_int( const char * prompt, int minv, int maxv )
{
std::cout << prompt << " [" << minv << ',' << maxv << "]: " ;
int number ;
if ( std::cin >> number )
{
if ( number >= minv && number <= maxv ) return number ;
else std::cout << "error: value " << number << " is out of range." ;
}
else std::cout << "error: non integer input." ;
std::cin.clear() ; // clear a possible failed state
std::cin.ignore( 1000, '\n' ) ; // discard this line of input
std::cout << " try again\n" ;
return get_int( prompt, minv, maxv ) ; // try again
}
int main()
{
const bool angry = answer_is_yes_to( "are you angry" ) ;
if (angry) std::cout << "cool down!\n" ;
const int num_entries = get_int( "number of entries" , 3, 8 ) ;
std::cout << "num_entries == " << num_entries << '\n' ;
}
Topic archived. No new replies allowed.