C++ 我如何让字符串仅验证正在按下的ENTER键? void游戏::验证(字符串和str1) { bool exit=false; int strLength=str1.length(); while(exit==false) { 对于(int i=0;i>?我
我如何让字符串仅验证正在按下的ENTER键?C++ 我如何让字符串仅验证正在按下的ENTER键? void游戏::验证(字符串和str1) { bool exit=false; int strLength=str1.length(); while(exit==false) { 对于(int i=0;i>?我,c++,c++11,visual-c++,C++,C++11,Visual C++,我如何让字符串仅验证正在按下的ENTER键? void游戏::验证(字符串和str1) { bool exit=false; int strLength=str1.length(); while(exit==false) { 对于(int i=0;i>?我认为cin忽略了空格,那么它是一个空字符串。因此您有:exit==false,因此while循环运行。I
void游戏::验证(字符串和str1)
{
bool exit=false;
int strLength=str1.length();
while(exit==false)
{
对于(int i=0;i 在开始检查之前,请尝试打印出字符串,您可能会
请确保字符串不是空的,即使enter键是唯一的输入。
在用户输入之前,请尝试刷新流。
请尝试std::coutHow您读取字符串吗?getline
不会追加换行符。名为validate
的函数不应该提示输入是的,我正在使用getline..so cin>>?我认为cin忽略了空格,那么它是一个空字符串。因此您有:exit==false,因此while循环运行。Ivoid Game::validate(string& str1)
{
bool exit = false;
int strLength = str1.length();
while (exit == false)
{
for (int i = 0; i < strLength; i++)
{
if(!isalpha(str1[i]) || isspace(str1[i])) //|| str1.empty())//?
{
cout << "Not a valid name, please try again: " << endl;
getline(cin, str1);
}
else if(isalpha(str1[i]))
{
exit = true;
}
}
}