简单输入验证 - cin.ignore错误

问题描述

我在看教程时遇到了这个问题,并为自己制作了一个。 我拿不到这个cin.ignore()。我希望用户只输入字符,但如果他输入任何整数,它应该继续循环,直到他得到正确的输入。

这是我的代码以及提到的错误

#include <iostream>
#include <vector>
#include <climits>
using namespace std;

// simple io validation
int main(){
    char name{};
    char last{};
    int birthday {};
    cout << "Pls Enter your first name: ";
    cin>>name;   // if he type "alfred123 Philip" it should error
    while (!cin.good())  // or while cin.fail()
        {
        cout<<"Please enter character only"<<endl;
        cin.clear();
        cin.ignore(INT_MAX,"/n");  //error no instance of overloaded function "std::basic_istream<_CharT,_Traits>::ignore [with _CharT=char,_Traits=std::char_traits<char>]" matches the argument list
        cout<<"Enter Name: ";   // ask again the user
        cin >> name;

        }
    cout << endl;
    cout<<"Hello "<<name<<endl;
    return 0;
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)