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.
source
share