我需要限制用户在输入字符时输入整数和字符串。我有一个整数方法,我只需要将它调整为一个字符。谁能帮我这个。
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;
}
我已经尝试过了,没有错误。但它不会工作。