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)); });
}
|