Thread: Character handling help

  1. #1
    Registered User
    Join Date
    Oct 2003
    Posts
    14

    Character handling help

    I've been workin on the following program and could use a little help. The program should input a character from the keyboard and using isdigit, return wheter the character is a digit or not. The program compiles fine, but will not function. I get a program error and the window is closed. I also tried to input the character into the isdigit function as a pointer to character and that also would not work.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main()
    {
       int character;  /* Initializes character  */
     
       printf( "Enter a character: ");    /* Asks for character and inputs it */     
       scanf( "%d", character );
    
               /* This should determine if the character is a digit or not */
       printf( "%d%s", isdigit( character ) ?  "The character is a " : "The character is not a ", "digit." );   
    
       return 0;
    }
    Thanks!

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    You're missing the number. According to the first argument you're passing to printf, it expects a number and a string. You're just passing a string.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,380
    Code:
    printf( "%d%s", isdigit( character ) ?  "The character is a " : "The character is not a ", "digit." );
    Here printf() expects an integer and a string. You are passing two strings. If character is a digit, then the first string is "The character is a", else the first string is "The character is not a". the second string is "digit.".

    Also you expect a character, so variable character could be of type char and you should read a character.

    Code:
       char character;
    
       scanf( "%c", &character);

  4. #4
    Registered User
    Join Date
    Oct 2003
    Posts
    14
    Thanks for the help.
    Last edited by vandalay; 11-23-2003 at 02:06 AM.

  5. #5
    Registered User
    Join Date
    Oct 2003
    Posts
    14
    Any ideas on how I can get the program to take whitespace characters and test them as well? I'm able to enter and check single characters, but it needs to check short strings also.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Generally speaking, you should always use fgets() to read the input into a buffer.

    Then you have all the freedom to use as many buffer oriented functions (including sscanf()) to extract information from that buffer.
    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.

  7. #7
    Registered User
    Join Date
    Oct 2003
    Posts
    14
    Originally posted by Salem
    Generally speaking, you should always use fgets() to read the input into a buffer.

    Then you have all the freedom to use as many buffer oriented functions (including sscanf()) to extract information from that buffer.
    I havent quite worked my way up to that yet. Any suggestions using arrays or pointers?

  8. #8
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,164
    Originally posted by vandalay
    I havent quite worked my way up to that yet. Any suggestions using arrays or pointers?
    scanf() is so full of problems when dealing with mixed input (numbers, strings, whitespace) that you will spend a LOT of time trying to figure it out, then when you learn how to use fgets() you will see a much better way to handle input. YOU have the control instead of cobbling together unreadable format specifiers. If you must, learn scanf() fully. But I'd recommend either waiting for fgets() and learn how to parse a string then, or look into fgets() now... Beat the rush.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  9. #9
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Originally posted by vandalay
    Any ideas on how I can get the program to take whitespace characters and test them as well? I'm able to enter and check single characters, but it needs to check short strings also.
    I am assuming that because you know of isdigit() you also are aware of isspace()?

    ~/

  10. #10
    Registered User
    Join Date
    Oct 2003
    Posts
    14
    Originally posted by kermit
    I am assuming that because you know of isdigit() you also are aware of isspace()?

    ~/
    Thats the part of the program I'm having trouble with. I'm unable to enter a string such as \n or \t to test to see if its a whitespace character. I'm only able to enter a single character. When \n is entered, only \ is tested.

  11. #11
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Is there a reason why you could not use the getchar() function? It works nicely for such things.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main( void )
    {
       int *character;  /* Initializes character  */
     
       printf( "Enter a character: ");    /* Asks for character and inputs it */     
       *character = getchar();
    
               /* This should determine if the character is a digit or not */
       printf( "\n%s%s", isdigit( *character ) ?  "The character is a " : "The character is not a ", "digit.\n\n" );   
    
       return 0;
    }
    [edit]
    Sorry, I should have paid better attention - the digit thing was solved, you need to know about spaces now. You can do that with getchar too, but using fgets() might be your bets bet, as was previously mentioned, as the stuff is already nicely in a buffer for you, and you can do any test on the contents of the buffer you desire.
    ~/
    Last edited by kermit; 11-23-2003 at 04:31 PM.

  12. #12
    Registered User
    Join Date
    Oct 2003
    Posts
    14
    Originally posted by kermit
    Is there a reason why you could not use the getchar() function? It works nicely for such things.

    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main( void )
    {
       int *character;  /* Initializes character  */
     
       printf( "Enter a character: ");    /* Asks for character and inputs it */     
       *character = getchar();
    
               /* This should determine if the character is a digit or not */
       printf( "\n%s%s", isdigit( *character ) ?  "The character is a " : "The character is not a ", "digit.\n\n" );   
    
       return 0;
    }
    [edit]
    Sorry, I should have paid better attention - the digit thing was solved, you need to know about spaces now. You can do that with getchar too, but using fgets() might be your bets bet, as was previously mentioned, as the stuff is already nicely in a buffer for you, and you can do any test on the contents of the buffer you desire.
    ~/
    Thanks Kermit. I tried your suggestion and still only get the \ of \n or \t to test against isspace. Any other recommendations?

  13. #13
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    Could you possibly give a little context as to what you want to do with this input, and why you are testing for a space? It might be helpful in order to figure out which direction to take, as there are many ways of doing things in C a lot of the time it seems.

  14. #14
    Registered User
    Join Date
    Oct 2003
    Posts
    14
    Sorry about that. I have to input a character from the keyboard and test it against the functions in the character handling library. I have all of the functions working, except the ones requiring a string (\n, \t, \r, and such). In those, the only thing recognized is \. I've tried everything I can think of, which isnt a whole lot. Thanks for your patience and help.

  15. #15
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    int main( void )
    {
       int *character;  /* Initializes character  */
     
       printf( "Enter a character: ");    /* Asks for character and inputs it */     
       *character = getchar();
    This is entirely wrong. You should not be using a pointer here. getchar returns a character, not a pointer to a character. Furthermore, since you are using a pointer, and you aren't actually making it point to something, it points to some random space in memory.

    What happens is that as soon as you call getchar the way you are, it overwrites that random spot in memory with whatever getchar returns. This is a BadThing(TM).

    Don't use a pointer. Nothing here requires a pointer.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Character literals incorrectly interpreted
    By DL1 in forum C Programming
    Replies: 11
    Last Post: 04-05-2009, 05:35 PM
  2. Using a character array in a switch question.
    By bajanElf in forum C Programming
    Replies: 10
    Last Post: 11-08-2008, 08:06 AM
  3. Replies: 11
    Last Post: 10-07-2008, 06:19 PM
  4. wide character (unicode) and multi-byte character
    By George2 in forum Windows Programming
    Replies: 6
    Last Post: 05-05-2007, 12:46 AM
  5. <string> to LPCSTR? Also, character encoding: UNICODE vs ?
    By Kurisu33 in forum C++ Programming
    Replies: 7
    Last Post: 10-09-2006, 12:48 AM