:cool:

Hi all

I have fingered out how to check the users input for their first and last name
This works fine.

If the user enters a number for their name they resave an error message.
But it only works for the first letter

How do I increment the check for each additional letter

int mainMenu::getName()
{
    int i;
 
    cout << "\tPlease enter your first name: ";
    cin.get(firstName, 20).get();
 
    while (isdigit(firstName[i]))
    {
        printf("\n\t\a%c[%d;%dmYou entered a Number ", 0x1B, BRIGHT, RED, BG_BLACK);
        printf("%c[%dm", 0x1B, 0);
        cout << firstName;
        cout << flush;
 
        printf(", %c[%d;%dmAs part of your first Name try agin.\n\n", 0x1B, BRIGHT, RED, BG_BLACK);
        printf("%c[%dm", 0x1B, 0);
 
        cout << "\tPlease enter your firstName: ";
        cin.get(firstName, 20).get();
        cout << endl;
    }
 
    cout << endl;
    cout << "\tPlease enter your lastName: ";
    cin.get(lastName, 20).get();
 
    while (isdigit(lastName[i]))
    {
        printf("\n\t\a%c[%d;%dmYou entered a Number ", 0x1B, BRIGHT, RED, BG_BLACK);
        printf("%c[%dm", 0x1B, 0);
        cout << lastName;
        cout << flush;
 
        printf(", %c[%d;%dmEnter your last name try agin.\n\n", 0x1B, BRIGHT, RED, BG_BLACK);
        printf("%c[%dm", 0x1B, 0);
 
        cout << "\tPlease enter your lastName: ";
        cin.get (lastName, 20).get();
    }
 
    firstName[0] = toupper (firstName[0]);
    lastName[0] = toupper (lastName[0]);
 
    cout << endl;
 
    return 0;
 
}

Thank you,
manny

Recommended Answers

All 4 Replies

#include<iostream>
usingnamespace std;
#include<string>
 
int main()
{
[INDENT]char y;
 
cout << "Enter a character or number: _\b";
cin >> y;
 
if(!isdigit(y))
[INDENT]cout << "It's a Letter";
[/INDENT]else
[INDENT]cout << "It's a Number";
[/INDENT] 
cout << '\n';
 
return 0;
[/INDENT] 
}
#include<iostream>
usingnamespace std;
#include<string>
 
int main()
{[INDENT]char y;
 
cout << "Enter a character or number: _\b";
cin >> y;
 
if(!isdigit(y))[INDENT]cout << "It's a Letter";
[/INDENT]else[INDENT]cout << "It's a Number";
[/INDENT]cout << '\n';
 
return 0;
[/INDENT]}

My friend, I think you are assuming too much of that if statement. What if the entered input is a space or a new line or " ?. Would that be considerate a letter?.

My friend, I think you are assuming too much of that if statement. What if the entered input is a space or a new line or " ?. Would that be considerate a letter?.

Hi

Thank you but I am asking the user to enter their name. And confirm that a letter has ban entered if not output a message I have the first part worked out

If the first letter is a digit the user resaves a message

I am trying to increment this processes for each character entered by one.

while (isdigit(firstName[i]))
{
    printf("\n\t\a%c[%d;%dmYou entered a Number ", 0x1B,    BRIGHT, RED, BG_BLACK);
    printf("%c[%dm", 0x1B, 0);
    cout << firstName;
    cout << flush;
 
    printf(", %c[%d;%dmAs part of your first Name try agin.\n\n",  0x1B, BRIGHT, RED, BG_BLACK);  
    printf("%c[%dm", 0x1B, 0);
 
    cout << "\tPlease enter your firstName: ";
    cin.get(firstName, 20).get();
    cout << endl;
}

while the above work for chars, is there anything similar for strings?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.