Я делаю функцию для ввода основной информации. Эта функция позже будет использоваться для получения информации и сохранения на диске. Я сделал две проверки. is_alpha и is_digit. Проблема с is_alpha заключается в том, что она возвращает «0», если она обнаруживает пробел (что не то, что я хочу). Я беру ввод для «Имя» и, очевидно, он может содержать пробелы! Не могли бы вы рассказать мне, как сделать метод, который проверяет мой массив символов, если это имя? (алфавиты и белые пространства)Как проверить массив символов только для букв (включая пробелы)?
class Bankaccount
{
protected:
int id;
char name[50];
char address[100];
char phone_no[50];
static int count;
public:
Bankaccount()
{
count++;
id = count;
}
bool is_number(const std::string& s)
{
std::string::const_iterator it = s.begin();
while (it != s.end() && std::isdigit(*it)) ++it;
return !s.empty() && it == s.end();
}
bool is_alpha(const std::string& s)
{
std::string::const_iterator it = s.begin();
while (it != s.end() && std::isdigit(*it)) ++it;
{cin.ignore(); return !s.empty() && it == s.end();}
}
void basics()
{system("cls");
cout << "Enter Name (Letters only): " << endl;
cin.ignore();
cin.getline(name,50);
{cout << "Enter Address: " << endl;
cin.ignore();
cin.getline(address,100);
cout << "Enter Phone Number (Digits only): " << endl;
cin.ignore();
cin.getline(phone_no,50);
//CHECK FOR DIGITS ONLY
bool temp=0;
temp=is_number(phone_no);
{while(temp!=1)
{cout << "ReEnter Phone Number: " << endl;
cin.ignore();
cin.getline(phone_no,50);
temp=is_number(phone_no);
}
}//while ends
}
{cout << "ReEnter Name: " << endl;
cin.ignore();
cin.getline(phone_no,50);
cout << "Enter Address: " << endl;
cin.ignore();
cin.getline(address,100);
cout << "Enter Phone Number (Digits only): " << endl;
cin.ignore();
cin.getline(phone_no,50);
//CHECK FOR DIGITS ONLY
bool temp;
temp=is_number(phone_no);
if (temp==1)
{}
else {cout << "ReEnter Phone Number: " << endl;
cin.ignore();
cin.getline(phone_no,50);
}
}
}
Вы можете использовать либо std :: isspace, либо std :: isblank вместе со std :: isalpha. В чем проблема? –
Как использовать isspace и isalpha вместе? Я новичок. Пожалуйста помоги! –
Имена могут также содержать дефисы и апострофы. Лучше сделайте функцию is_validnamecharacter(). – stefaanv