i need to restrict the user from inputting an integer and string when inputting a char. I have a method for an integer i just need to adapt it for a char. Can anyone help me with this.
char getChar()
{
char myChar;
std::cout << "Enter a single char: ";
while (!(std::cin >> myChar))
{
// reset the status of the stream
std::cin.clear();
// ignore remaining characters in the stream
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
// ^^^ This line needs to be changed.
std::cout <<
"Enter an *CHAR*: ";
}
std::cout << "You entered: " << myChar << std::endl;
return myChar;
}
char getChar()
{
char myChar;
std::cout << "Enter an Char: ";
while (!(cin >> myChar))
{
// reset the status of the stream
cin.clear();
// ignore remaining characters in the stream
cin.ignore(std::numeric_limits<char>::max() << '\n');
cout << "Enter an *CHAR*: ";
}
std::cout << "You entered: " << myChar << std::endl;
return myChar;
}