: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

Dani AI

Generated

The code from only ever tests one character because the index variable is never initialized and the loop never actually walks the whole name. ’s single-char isdigit check is fine for a single input, and is right to flag spaces and punctuation — those need explicit handling. For full names use std::string + std::getline and validate every character (or use std::all_of / std::any_of) so digits anywhere in the string get caught. : yes — the same string-based approach works for multi-character input.

#include <iostream>
#include <string>
#include <algorithm>
#include <cctype>

bool valid_name(const std::string &s) {
    if (s.empty()) return false;
    return std::all_of(s.begin(), s.end(), [](char c){
        unsigned char uc = static_cast<unsigned char>(c);
        return std::isalpha(uc) || uc == '-' || uc == '\'' || uc == ' ';
    });
}

int main() {
    std::string first;
    do {
        std::cout << "Please enter your first name: ";
        std::getline(std::cin, first);
        if (!valid_name(first))
            std::cout << "Invalid input — only letters, hyphen, apostrophe and spaces allowed.\n";
    } while (!valid_name(first));
    if (!first.empty()) first[0] = static_cast<char>(std::toupper(static_cast<unsigned char>(first[0])));
    // repeat for last name...
}

Notes and troubleshooting: always cast characters to unsigned char before calling std::isalpha/std::isdigit/std::toupper to avoid undefined behavior on negative char values. If staying with C-style arrays, iterate with a for loop from 0 until the terminating '\0' instead of using an uninitialised index. Decide policy for hyphens/apostrophes/spaces (some real names include them). For full Unicode/name validation, consider a proper Unicode-aware library; std::isalpha only handles the C locale/ASCII by default.

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.