Yes, first time I wrote this program :
Code:
#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
char name[50];
int i = 0;
while (true) {
cout << "Please enter your name.\n";
cin >> name;
if (isalpha(name[i])) {
i = 0;
i++;
cout << "Nice to meet you.\n";
break;
}
cout << "No, that's wrong.Enter your name again:\n";
}
return 0;
}
For a name like Sonny2 it displays the message 'nice to meet you' but this is not a valid name.
After this, I wrote the following program using the boolean function.I thinks this is better but it keeps displaying the message 'No,that's wrong.Please enter your name ' even when it shouldn't.
This is it:
Code:
#include <iostream>
#include <cstring>
#include <cctype>
using namespace std;
bool is_name(string);
int main()
{
char name[50] = {'\0'};
int i = 0;
do {
cout << "Please enter your name.\n";
cin >> name;
if (is_name(name) == true) {
cout << "Nice to meet you.\n";
break;
}
cout << "No, that's wrong.\n";
} while (is_name(name) == false);
return 0;
}
bool is_name(string)
{
int i = 0;
char name[50];
for (i = 0; i < strlen(name) ;i++ ){
if(isalpha(name[i])) {
return true;
}
}
return false;
}
I 'll appreciate if you tell me why it doesn't work and how I should fix it.
By the way, how can I check the presence of the @ in a string ? If I'm not wrong it's a function for points (.) I want the user to input his email address.