Hey Guys so I'm trying to make some sort of encyclopedia of Pokemon (Pokedex) I'm in the early stages of it. I'm trying to prevent numbers being input instead of names. So heres what I have so far. so far i've put the int variable and some if statements but to no luck. Thanks!!
#include <iostream>
usingnamespace std;
int main()
{
int x =3 ;
string pokemon0fname;
cout<<"what is your favorite Pokemon?:" << endl;
cin>> pokemon0fname;
cin.ignore();
// I'm trying to say if you type in a name then you get "What a great Pokemon!"//
if (x>3)
cout<<"What a great Pokemon!";
/* I'm trying to say if you type in any number or symbol other than a name then you get
"You twat don't even try it or i'll kill you!"*/
if(x<=3){
cout<<" You twat don't even try it or i'll kill you!";
}
return 0;
}
Possibly the easiest way might be to input a string - which can contain any characters, including digits, and then test the individual characters of the string using the isdigit() function. http://www.cplusplus.com/reference/cctype/isdigit/
I tried to use the isdigit()function but to no avail because it doesn't really help with the problem. I'm trying to prevent people from putting numbers or other character instead of a name.
I'm trying to prevent people from putting numbers or other character instead of a name.
Well, you need to be very specific about what you mean by a "name". For example could it contain spaces or a hyphen? May it contain a numeric character or an apostrophe?
In the code below, I've simply checked that every character is a letter of the alphabet, but that may not be sufficient for Pokemon. A simpler approach would be to check only the very first letter. On the other hand a more advanced version might hold a complete list of all acceptable names, and check that the user's input was found in that list.
#include <iostream>
#include <string>
#include <cctype>
usingnamespace std;
int main()
{
string pokemon0fname;
cout << "what is your favorite Pokemon?:" << endl;
cin >> pokemon0fname;
// Check that every character is a letter of the alphabet
bool nameIsValid = true;
for (unsignedint i=0; i<pokemon0fname.size(); i++)
{
if (!isalpha(pokemon0fname[i]))
{
nameIsValid = false;
break;
}
}
if (nameIsValid)
cout<<"What a great Pokemon!" << endl;
else
cout << "that isn't a name" << endl;
return 0;
}
An easier resolution would be to test the user's input against a list of Pokémon names. In other words, if the user enters something other than a Pokémon name, your program should kill him.