samedi 24 juillet 2021

Simple Input validation - cin.ignore (error)

I have this problem while watching tutorials and make one for myself. I can't get a hold of this cin.ignore(). I want the user to type characters only but if he types any integer, it should continue looping until he gets the correct input.

This is my code along with the error mentioned.

#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;
}

Aucun commentaire:

Enregistrer un commentaire