How to prevent numbers from being an input

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!!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
  #include <iostream>

using namespace 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/
@Chervil

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <string>
#include <cctype>

    using namespace 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 (unsigned int 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;
}
Last edited on
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.
Well, I already mentioned that idea, but described it as "advanced" rather than "easier". But no problem, there is room for more than one opinion.
Yes @Chervil and @Josue

but there are over 600 Pokemon names. Do you guys know a shortcut around this?

and @Chervil : It has to be the exact Pokemon name so yes you are absolutely correct!
Here you go: https://db.tt/B1C2EmNP
It might be tricky for the user to type the exact name, such as "Nidoran♀" or "Nidoran♂".
Topic archived. No new replies allowed.