c++ - 验证用户输入的文本大小的函数c++

标签 c++

我已经尝试了下面的功能,

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/

相关文章:

c++ - 将参数初始化到构造函数中,除了第一个参数

c++ - macOS 应用程序文件关联

c++ - 加载对象 mtl Direct3D 11

c++ - 字符串到字节数组

c++ - 如何(轻松)将 C++ 函数包装到易于安装的 R 'package'

c++ - 复制构造函数和析构函数八叉树c++

android - 从 pthread 初始化对象时为 "NoSuchMethodError"

c++ - boost::interprocess scoped_allocator 和不在共享内存中的容器的容器

c++ - 实例化模板化 std::function 时出现编译错误

c++ - fatal error : QTextStream: No such file or directory