我已经尝试了下面的功能,
string checkstring(int sublen){
bool quit = false;
string temp;
while(!quit){
cin>>temp;
if (temp.size()> sublen){
cout<<"\t"<<"Error , Only "<<sublen<<"characters are allowed "<<endl;
cin.clear();
getch();
}else{
return temp;
quit=true;
}
}
}
问题是,如果我在输入字符串中输入任何数字,它会跳过几行。
例如:- 输入您的姓氏:Mayer 23,
它跳过循环中的下一个问题
Question Marked with blue has skipped - Please view thus image | Cursor at Red Mark
抱歉我的英语不好!请帮忙!
最佳答案
当您使用 >>>
运算符读取文本时,它会在 white-space 上分隔。如果你想要整行使用 std::getline
相反。
关于c++ - 验证用户输入的文本大小的函数c++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38555575/