In String only letters?

Hello everyone,

i got a, maybe silly, question:

how can i only read letters in?
I want to start a query, where the user have to input a name, but he/she shouldn't be able to write enter some values.

My first try was to solve it like that:

1
2
3
4
5
6
7
8
9
  int main() try {


    string name{""};

    cout << "Please enter your name: ";
    cin >> name;

    if (!cin) throw std::invalid_argument("Your input isn't a string");
If you don't want name to contain particular characters, you need to parse/validate it yourself.

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
31
32
33
34
#include <iostream>
#include <string>
#include <cctype> // isalpha

bool valid_name(const std::string& name)
{
    if (name.length() == 0)
        return false;

    for (char ch : name)
    {
        if (!std::isalpha(ch))
            return false;
    }
    return true;
}

int main()
{
    using namespace std;
    
    string name;
    cout << "Please enter your name: ";
    cin >> name;
    
    if (!valid_name(name))
    {
        cout << "Your input isn't a string!\n";
    }
    else
    {
        cout << "Hello, " << name << "!\n";   
    }
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <locale> //isalpha
using namespace std;

int main (){
	string s="it's a Line";
	for(auto x:s){
		if(isalpha(x)) cout << x << " true\n";
		else cout << x << " false\n";		
	} 
}
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
31
32
33
34
35
36
37
#include <iostream>
#include <string>
#include <cctype>
#include <algorithm>

int main()
{
    {
        // generate an error if non-alpha characters are entered
        std::string name ;
        std::cout << "enter your first name (only alpha characters please): " ;
        std::cin >> name ;

        // https://en.cppreference.com/w/cpp/algorithm/all_any_none_of
        // http://www.stroustrup.com/C++11FAQ.html#lambda
        if( std::any_of( name.begin(), name.end(), []( char c ) { return !std::isalpha(c) ; } ) )
            std::cout << "error: name contains invalid characters\n" ;
        else
            std::cout << "fine. name: " << name << '\n' ;
    }

    {
        // ignore non-alpha characters that are entered
        std::string name ;
        std::cout << "enter your first name (only alpha characters please): " ;

        char c ;
        while( std::cin.get(c) && std::isspace(c) ) ; // skip leading white space

        do
            if( std::isalpha(c) ) name += c ; // append only alpha characters to name
        while( std::cin.get(c) && !std::isspace(c) ) ; // till a white space is encountered

        if( name.empty() ) std::cout << "error: name can't be empty\n" ;
        else std::cout << "fine. name: " << name << '\n' ;
    }
}
Thank you guys!!

I did it like @Ganado said and it works well!

Indeed i don't rly understand how this works:

1
2
3
4
5
6
7
8
9
10
11
12
bool valid_name(const std::string& name)
{
    if (name.length() == 0)
        return false;

    for (char ch : name)
    {
        if (!std::isalpha(ch))
            return false;
    }
    return true;
}


Could you @Ganado explain me what exactly is being checked here?
>
what exactly is being checked here?

it will be true if the char is between a-z or A-Z. otherwise false. eg.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include<iostream>
#include<cctype>
using namespace std;

int main(){
	char c='k';
	if(isalpha(c)) cout<< "ok1\n";
	c=' ';
	if(isalpha(c)) cout<< "ok2\n";
	c='5';
	if(isalpha(c)) cout<< "ok3\n";
	c='M';
	if(isalpha(c)) cout<< "ok4\n";
return 0;	
}
Line 3-4: If the length of the string is 0, it's invalid. This could be the result if cin fails for whatever reason.

Line 6-10: This is a for loop that is iterating over every character (ch) in the name. If the character is not "alpha" (a letter from A to Z), it returns false.

Line 11: Otherwise, if it has looped through every character and finds no non-alpha characters, the name is valid.
Last edited on
Thanks a lot! I understand everything very well.
Topic archived. No new replies allowed.