Validating string for invalid characters

Today I did some research on how to check string input for invalid characters such as numbers, unfortunately, without success. I am trying to check the string to get the client name and check if there are any numbers.

#include "stdafx.h"                                             
#include <string>               
#include <iostream>
#include <conio.h>
#include <algorithm>
#include <cctype>

using namespace std;

string validateName(string name[], int i)
{
    while(find_if(name[i].begin(), name[i].end(), std::isdigit) != name[i].end()){   
        cout << "No digits are allowed in name." << endl;
        cout << "Please re-enter customer name:" << endl;
        cin.clear();                                            
        cin.ignore(20, '\n');
    }

    return name[i];    
}

int main()
{ 
    string name[10];
    int i=0;
    char newentry='n';

    do{
        cout << "Plase enter customer name: " << endl;                                                        
        getline(cin, name[i]);  
        name[i]=validateName(name, i);

        i++

        cout << "Would you like to enter another questionare? Enter either 'y' or 'n': " << endl;
        cin >> newentry;

    } while((newentry =='y') || (newentry=='Y'));

The function seems to work just fine, but only with the first input. For example, when I run the program and enter the number 3, an error message is displayed, and the user will be prompted to enter the name again. After the user enters the correct name, the program, however, continues to request a new entry with the same error messages, even if no digits or special characters are used.

+3
source share
2 answers

, , , , , , :

#include <string>               
#include <iostream>
#include <conio.h>
#include <algorithm>
#include <cctype>

using namespace std;

void validateName(string &name) //Pass by reference and edit given string not a copy, so there is no need to return it
{
    cout << "Plase enter customer name: " << endl;
    cin.clear();
    cin.sync();
    getline(cin, name);
    while (name.find_first_of("0123456789") != -1)
    {
        cout << "No digits are allowed in name." << endl;
        cout << "Please re-enter customer name:" << endl;
        cin.clear();
        cin.sync();
        getline(cin, name);
    }
}

int main()
{
    string name[10];
    int i = 0;
    char newentry = 'n';

    do{
        validateName(name[i++]);

        if (i >= 10)
            break;

        cout << "Would you like to enter another questionare? Enter either 'y' or 'n': " << endl;

        do{
            cin.clear();
            cin.sync();
            cin >> newentry;
        } while ((newentry != 'y') && (newentry != 'Y') && (newentry != 'n') && (newentry != 'N'));

    } while ((newentry == 'y') || (newentry == 'Y'));
}
+3

validateName , .

, getline , .

cin.ignore(20, '\n');

getline(cin, name[i]);  

do-while.

cin.ignore( std::numeric_limits<std::streamsize>::max() );

,

cin >> newentry;

, getline .

0

All Articles