Thread: Checking user input [int/char] (Help)

  1. #1
    Registered User
    Join Date
    Dec 2012
    Posts
    16

    Checking user input [int/char] (Help)

    I was wondering if I can make a program which would print "its a number" when the user enters a number and prints "Its an alphabet" when an user enters an alphabet.
    Can you make a simple C program which can do that?

    I am a newbie in programming so please explain what you write.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,530
    Announcements - C Programming
    Presumably, you've got as far as being able to read input, and print what was read.
    If so, then post this much to at least prove you've been paying attention in class.

    If you can't do that, then re-read your material and make some kind of effort.

    This forum is NOT a place to dump your assignment and expect a complete answer on a plate.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2012
    Posts
    16
    Quote Originally Posted by Salem View Post
    Announcements - C Programming
    Presumably, you've got as far as being able to read input, and print what was read.
    If so, then post this much to at least prove you've been paying attention in class.

    If you can't do that, then re-read your material and make some kind of effort.

    This forum is NOT a place to dump your assignment and expect a complete answer on a plate.
    As much I can make is
    Code:
    #include<stdio.h>
    #include<conio.h>
    void main()
    int i;
    printf("Enter something");
    i=getchare();
    if(i>=1)
    puts("You have entered a number");
    else if((i=='a')||(i=='b')) // and so on to Z.
    puts("You have entered an alphabet");
    else
    puts("Invalid input");
    }
    The problem is in the declaration
    Code:
    int i;
    Because if i is an int then it cant store a char.....
    If a user enters a char then it will convert it to its ASCII value i.e. a number value so in both the cases i will be an integer.
    so the output will always be You have entered a number.
    So How can I fix that.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,530
    Look in ctype.h for isascii() etc.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Dec 2012
    Posts
    16
    Quote Originally Posted by Salem View Post
    Look in ctype.h for isascii() etc.
    Can you explain

  6. #6
    Registered User
    Join Date
    Dec 2012
    Posts
    16

    Lightbulb Didn't find anything

    Quote Originally Posted by Salem View Post
    Look in ctype.h for isascii() etc.
    isascii

    short isascii (short c);
    Checks whether a character is an ASCII character.
    isascii returns nonzero if c is in the range 0 to 127 (0x00-0x7F), otherwise it returns zero. It is a simple macro.

    I dont think that it will help. because it only checks if the number is an ASCII number.

    Thats not my solution.

    Plz read my conditions and program and then post a helpful reply.

    You can take time, I am not in hurry Its not my homework, its only for general knowledge.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,235
    Quote Originally Posted by Hritik
    I dont think that it will help. because it only checks if the number is an ASCII number.

    Thats not my solution.
    You missed the "etc". What other functions can you find in <ctype.h> that might help?

    Quote Originally Posted by Hritik
    Plz read my conditions and program and then post a helpful reply.
    One of your two conditions is probably wrong: I guess that by "alphabet", you mean "letter" in the English alphabet.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Dec 2012
    Posts
    16
    Quote Originally Posted by laserlight View Post
    One of your two conditions is probably wrong: I guess that by "alphabet", you mean "letter" in the English alphabet.
    Yeah , its character/letter.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,235
    Right. So what else did you find from <ctype.h>?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Dec 2012
    Posts
    16

    Post All I found

    Quote Originally Posted by laserlight View Post
    Right. So what else did you find from <ctype.h>?
    All I found in it was:

    _tolowerTranslates uppercase characters to lowercase._toupperTranslates uppercase characters to lowercase.isalnumChecks whether a character is an alphanumeric.isalphaChecks whether a character is a letter.isasciiChecks whether a character is an ASCII character.iscntrlChecks whether a character is a control character.isdigitChecks whether a character is a digit.isextalnumChecks whether a character is an extended alphanumeric.isextlowerChecks whether a character is a lowercase, including foreign ones.isextpunctChecks whether a character is an extended punctuation character.isextupperChecks whether a character is an uppercase, including foreign ones.isfrgnChecks whether a character is a foreign letter.isfrgnalnumChecks whether a character is a foreign letter which is valid in a variable name.isfrgnlowerChecks whether a character is a foreign lowercase.isfrgnupperChecks whether a character is a foreign uppercase.isgraphChecks whether a character is a graph character.isGreekChecks whether a character is a Greek letter.islowerChecks whether a character is a lowercase.isprintChecks whether a character is a printing character.ispunctChecks whether a character is a punctuation character.isspaceChecks whether a character is a white space.isupperChecks whether a character is an uppercase.isxdigitChecks whether a character is a hex digit.toasciiTranslates characters to ASCII format.toextlowerTranslates characters to lowercase, including foreign ones.toextupperTranslates characters to uppercase, including foreign ones.tolowerTranslates characters to lowercase.toupperTranslates characters to uppercase.
    <a href="http://google.com">Just testing HTML</a>

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,509
    You're getting warmer. Do any of those seem like they might be useful for your program?

  12. #12
    Registered User
    Join Date
    Dec 2012
    Posts
    16
    Quote Originally Posted by Matticus View Post
    Do any of those seem like they might be useful for your program?
    I don't think that anyone of this would help

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,235
    I think you're looking at a poor source because in the standard, pretty much all those functions take an int parameter, not a short parameter, and return an int, not a short.

    EDIT:
    Quote Originally Posted by Hritik
    I don't think that anyone of this would help
    "when the user enters a number" -> isdigit checks whether a character is a digit
    "when an user enters (a character/letter)" -> isalpha checks whether a character is a letter
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    Registered User
    Join Date
    Dec 2012
    Posts
    16

    Smile Work done !

    Didn't ever think of that! Sorry for this long thread for this easy purpose.in other words It Worked!!
    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<ctype.h>
    int main()
    {
    int i;
    i=getche();
    if(isalpha(i))
    puts("\nIts a letter");
    else if(isdigit(i))
    puts("\nIts a digit");
    else
    puts("\nEnter a valid input, you idiot!");
    getch();
    return 0;
    }
    Last edited by Hritik; 12-26-2012 at 10:22 AM.

  15. #15
    Registered User
    Join Date
    Dec 2012
    Posts
    3
    This's not use lib , like this:
    Code:
    #include<stdio.h>
    int main(int argc, char *argv[])
    {
        int i;
        printf("Enter something: ");
        i=getchar();
        if(i >= '0' && i <= '9')
            puts("You have entered a number");
        else if((i >='a' && i <= 'z') || (i >='A' && i <= 'Z')) // 
            puts("You have entered an alphabet");
        else
            puts("Invalid input");
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking User Input
    By Dusterdoo in forum C Programming
    Replies: 9
    Last Post: 06-25-2011, 03:04 PM
  2. Checking the user input.
    By omnificient in forum C Programming
    Replies: 2
    Last Post: 05-13-2008, 10:03 PM
  3. Checking user input?
    By hayai32 in forum C Programming
    Replies: 9
    Last Post: 04-03-2005, 05:33 PM
  4. Checking user input
    By Cmuppet in forum C Programming
    Replies: 5
    Last Post: 08-05-2004, 10:32 AM
  5. Error checking user input
    By theharbinger in forum C++ Programming
    Replies: 5
    Last Post: 07-22-2003, 09:57 AM

Tags for this Thread