我只使用C#找到了有关此主题的信息,我正在使用C++.希望你能帮助我.
我的代码:
#include <iostream>
#include <fstream> // Librería para el manejo de archivos
#include <string> // Librería para el manejo de strings
#include <stdlib.h> // Librería para el uso de system("cls");
int main()
{
//Variables
string NombreJugador;
/* Content
------------------------------------------------------------------------*/
do
{
cout << "Your name: ";
cin >> NombreJugador;
} while(ValidarNombreJugador(NombreJugador));
return 0;
}
/* Function
------------------------------------------------------------------------*/
int ValidarNombreJugador(string NombreJugador)
{
int Numero;
Numero = atoi(NombreJugador.c_str());
if (Numero!=0)
{
cout << "No puede ingresar numeros, solo letras." << endl;
return 1;
}
else
{
cout << "Perfecto, tu nombre no tiene numeros." << endl;
return 0;
}
}
Run Code Online (Sandbox Code Playgroud)
我在Google上找到这种方式来验证该名称只有字母而不是数字.
问题是,如果输入"0",它会将其识别为字母,均值,则返回true.
我该怎么做才能正确验证字符串只有字母而不是数字?
顺便说一句,我是新用的.
没有数字:
if(std::none_of(str.begin(), str.end(), [](unsigned char c){return std::isdigit(c);})) {
// stuff
}
Run Code Online (Sandbox Code Playgroud)
所有字母:
if(std::all_of(str.begin(), str.end(), [](unsigned char c){return std::isalpha(c);})) {
// stuff
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1239 次 |
| 最近记录: |