validating a string name

the string validation doesnt seem to work. im not sure how to fix this.
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
  void customerdetails();
bool isValidName();

int main()
{

	customerdetails();

	
    system ("pause");
	return 0;
}

void customerdetails()
{
	cout << "Please enter customer name" << endl;
	getline(cin, name);
	isValidName();
	
	cout << "Please enter tel no" << endl;
	cin >> telno;

	cout << "Please enter group size" << endl;
	cin >> groupsize;
	
	cout << "Please enter rating" << endl;
	cin >> ratin;
}
bool isValidName(std::string name)
{
    return std::all_of(name.begin(), name.end(),
        [](char ch) { return (std::isalpha(ch)); });
}
Last edited on
1
2
3
4
5
bool isValidName(std::string name)
{
    return std::all_of(name.begin(), name.end(),
        [](char ch) { return (std::isalpha(ch)); });
}
hey thanks cire... I'm still getting a error message on my "std::isalpha" saying; "Error: no instance of function template "std::isalpha" matches the argument list."

There are a number of things wrong with your incomplete source code.

The major problems:
1) The declaration ("prototype") of isValidName() is incorrect.
2) You don't actually check the return value of isValidName() so it appears as if the function has no effect.

You should spend a little time learning how to indent your code, and stick to a style you choose:
http://en.wikipedia.org/wiki/Indent_style

Finally, since you're using elements of C++11 anyway, I suggest that you check out the regex library (it may not be implemented for GCC/MinGW compilers yet, but it is available for Visual Studio 2013).
http://www.cplusplus.com/reference/regex/

You'll have to validate telno and the other variables too, not just the name, and the regex library is exactly what you want.

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
38
39
40
41
42
43
44
45
46
47
48
#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std; // bad habit, unlearn doing this

void customerdetails();
bool isValidName(std::string);

int main()
{

    customerdetails();

    
    system ("pause");
    return 0;
}

std::string name;
int telno;
int groupsize;
int ratin;

void customerdetails()
{
    cout << "Please enter customer name" << endl;
    getline(cin, name);

    if (!isValidName(name))
        std::cerr << "*** Invalid name! ***\n";
    
    cout << "Please enter tel no" << endl;
    cin >> telno;

    cout << "Please enter group size" << endl;
    cin >> groupsize;
    
    cout << "Please enter rating" << endl;
    cin >> ratin;
}
bool isValidName(std::string name)
{
    return std::all_of(name.begin(), name.end(),
        [](char ch) { return (std::isalpha(ch)); });
}

al-right thanks Catfish666. i guess i was getting ahead of myself a bit there thanks for the help I'm looking into what you suggested... much appreciated.
Topic archived. No new replies allowed.