jeudi 25 juin 2020

C++ empty() and all_of() for checking string is empty or have only digit

I'm creating an IO console application and at the inputs i got an 'while' loop with two condition empty() and all_of(), the function all_of() seems to work properly but when i press enter the empty() function not working and just let me to input the next thing in the 'struct'. I'm not sure am i doing it correct..There is the part of the code

    cout << "Enter age: ";
    getline(cin, age_str);  
            
    while(!age_str.empty() && !all_of(age_str.begin(), age_str.end(), ::isdigit)){
        
            cout << "--Please Enter an integer-- " << endl;
            cin.clear();
            getline(cin, age_str);          
    
    }
    stringstream(age_str) >> person_arr[n].age;

Aucun commentaire:

Enregistrer un commentaire