Thread: Number or Alphabet

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    18

    Number or Alphabet

    Hi All . I am new here and also new to C++
    I need help with this program , I have no clue how to make it
    I am using Turbo C++

    Write a program that prompts the user to enter the initial of his name. your program should check whether the entered character is a number or alphabet. If the entered value is a number than print an error message on the screen otherwise print: “YOUR NAME STARTS FROM : ”INITIAL CHARACTER.

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    Look at isalpha function.

  3. #3
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by Ahmed29 View Post
    I am using Turbo C++
    With all the good free compilers out there, why do so many people use garbage like Turbo C++?
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  4. #4
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by Ahmed29 View Post
    Hi All . I am new here and also new to C++
    I need help with this program , I have no clue how to make it
    I am using Turbo C++

    Write a program that prompts the user to enter the initial of his name. your program should check whether the entered character is a number or alphabet. If the entered value is a number than print an error message on the screen otherwise print: “YOUR NAME STARTS FROM : ”INITIAL CHARACTER.
    And if it neither a number nor a alphabet, like an underscore? Then what? Probably you should just check if it is a number. If not give an error message.
    Another way, except using isalpha() is do something like this:
    Code:
    char initial;
    //get character from user
    if (initial < 'a' || initial > 'z' || initial > 'A' || initial < 'Z')
       printf("ERROR\n");
    Generally in the ASCII you will have 'a', 'b', 'c', ... , 'z' so everything between 'a' and 'z' is a letter. On another point in the table you have 'A', 'B', ..., 'Z' so everything between 'A' and 'Z' is a capital letter. The same goes for numbers.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by C_ntua View Post
    And if it neither a number nor a alphabet, like an underscore? Then what? Probably you should just check if it is a number. If not give an error message.
    Another way, except using isalpha() is do something like this:
    Code:
    char initial;
    //get character from user
    if (initial < 'a' || initial > 'z' || initial > 'A' || initial < 'Z')
       printf("ERROR\n");
    Generally in the ASCII you will have 'a', 'b', 'c', ... , 'z' so everything between 'a' and 'z' is a letter. On another point in the table you have 'A', 'B', ..., 'Z' so everything between 'A' and 'Z' is a capital letter. The same goes for numbers.
    Don't do that. It makes the assumption that your code is running on an ASCII system. If you tried to run it on a mainframe, you'd be in trouble. That's what isalpha() was made for, so you don't need to care what platform you're on.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  6. #6
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    Alternatively he could use isdigit() to just check if the user entered a number. Albeit either method will have the same outcome.

  7. #7
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by MeTh0Dz View Post
    Alternatively he could use isdigit() to just check if the user entered a number. Albeit either method will have the same outcome.
    No, isdigit() would tell you if it's a digit. But if isdigit() returns false, you still can't be sure that it's a letter.
    isalpha() is the right function to use here, since you only want the user to enter letters.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  8. #8
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    Quote Originally Posted by Ahmed29 View Post
    Hi All . I am new here and also new to C++
    I need help with this program , I have no clue how to make it
    I am using Turbo C++

    Write a program that prompts the user to enter the initial of his name. your program should check whether the entered character is a number or alphabet. If the entered value is a number than print an error message on the screen otherwise print: “YOUR NAME STARTS FROM : ”INITIAL CHARACTER.
    cpjust maybe you should read what he initially wrote.

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by MeTh0Dz View Post
    cpjust maybe you should read what he initially wrote.
    Ah, well in that case, I'd say there is a problem with the requirements. The user is supposed to enter their initials (which are obviously only letters), so it should say "if the user entered anything other than a letter, print an error message".

    Otherwise I could enter my initials as "#%" and the program wouldn't give me any errors.
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  10. #10
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    Agreed (I was simply going by what the original poster prescribed).

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    18
    Thanks all problem solved i used ctype so it worked

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help getting program print out the digit in words
    By cosmiccomputing in forum C Programming
    Replies: 26
    Last Post: 04-24-2008, 08:28 AM
  2. Need help with this compiler error
    By Evangeline in forum C Programming
    Replies: 7
    Last Post: 04-05-2008, 09:27 AM
  3. Calculating next prime number
    By anilemon in forum C Programming
    Replies: 8
    Last Post: 04-17-2006, 10:38 AM
  4. Prime number program problem
    By Guti14 in forum C Programming
    Replies: 11
    Last Post: 08-06-2004, 04:25 AM
  5. Random Number problem in number guessing game...
    By -leech- in forum Windows Programming
    Replies: 8
    Last Post: 01-15-2002, 05:00 PM