User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 397,813 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,571 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 4699 | Replies: 3
Reply
Join Date: May 2007
Posts: 2
Reputation: manny c++ is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
manny c++ manny c++ is offline Offline
Newbie Poster

Question check if the user enters a number instead of a letter

  #1  
May 15th, 2007


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
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Apr 2007
Posts: 102
Reputation: mariocatch is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 17
mariocatch mariocatch is offline Offline
Junior Poster

Re: check if the user enters a number instead of a letter

  #2  
May 15th, 2007
#include<iostream>
usingnamespace std;
#include<string>
 
int main()
{
char y; cout << "Enter a character or number: _\b"; cin >> y; if(!isdigit(y))
cout << "It's a Letter";
else
cout << "It's a Number";
cout << '\n'; return 0;
}
Reply With Quote  
Join Date: Dec 2006
Posts: 1,296
Reputation: Aia is a name known to all Aia is a name known to all Aia is a name known to all Aia is a name known to all Aia is a name known to all Aia is a name known to all 
Rep Power: 9
Solved Threads: 88
Aia's Avatar
Aia Aia is offline Offline
Nearly a Posting Virtuoso

Re: check if the user enters a number instead of a letter

  #3  
May 16th, 2007
Originally Posted by mariocatch View Post
#include<iostream>
usingnamespace std;
#include<string>
 
int main()
{
char y; cout << "Enter a character or number: _\b"; cin >> y; if(!isdigit(y))
cout << "It's a Letter";
else
cout << "It's a Number";
cout << '\n'; return 0;
}


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?.
Last edited by Aia : May 16th, 2007 at 12:03 am.
"No man's life, liberty, or property is safe while the legislature is in session." ~ Mark Twain
Reply With Quote  
Join Date: May 2007
Posts: 2
Reputation: manny c++ is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
manny c++ manny c++ is offline Offline
Newbie Poster

Re: check if the user enters a number instead of a letter

  #4  
May 16th, 2007
Originally Posted by Aia View Post
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;
}
 
Last edited by cscgal : May 16th, 2007 at 8:28 pm. Reason: Excessive color removed per forum policy
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C++ Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C++ Forum

All times are GMT -4. The time now is 6:31 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC