Problems with character input

This is a discussion on Problems with character input within the C Programming forums, part of the General Programming Boards category; I need help with understanding how to recieve single character input, and use it the correct way. This book I'm ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    25

    Problems with character input

    I need help with understanding how to recieve single character input, and use it the correct way.

    This book I'm learning from is kind of shakey on this subject, and mainly goes into strings of input using strcmp, gets and puts, and what not.

    So strings are not my problem, just single characters.

    This is pretty much what I know now...

    Code:
    #include <stdio.h>
    
    
    void main()
    {
            char first;
            char second;
    
            printf("Enter a letter:");
            scanf("%c", &first);
            printf("Enter another letter:");
            scanf("%c", &second);
    
            printf("%c %c", first,second);
    }
    When i run it, it asks for the first letter, and i input it. But then it skips the next scanf, and sets the letter i put for the previous scanf, into that one.

    I'm seeing functions like "getchar" and "putchar", and other ones such as "toupper", "tolower". But the book doesn't describe how to use them.

    Please help, this is driving me insane.

  2. #2
    Registered User
    Join Date
    Oct 2002
    Posts
    385
    The next scanf is picking up 'Enter' as it's character. That's why you think it's skipping it. I can't remember right off hand how to alleviate this, maybe one of the other folks here can answer that.
    Wandering aimlessly through C.....

    http://dbrink.phpwebhosting.com

  3. #3
    .........
    Join Date
    Nov 2002
    Posts
    303
    The problem is that it is reading the newline with the second call to scanf(). Rememeber when you hit enter the '\n' character is put into the input stream. So say for the first scanf() you enter 'a' then press enter('\n') the '\n' is still in the input stream. So now the second scanf() immediately picks this up and does not give you a chance to enter your second character. A quick fix is to do something like....
    Code:
    #include <stdio.h>
    
    
    int main(void)
    {
            char first;
            char second;
    
            printf("Enter a letter:");
            scanf("%c", &first);
            printf("Enter another letter:");
            scanf(" %c", &second);
    
            printf("%c %c", first,second);
    }
    Notice the extra spacing in the second scanf(), this will make it so it skips whitespace, and the newline character '\n' is considered just that.

    About putchar(), getchar() and all that those are just other ways to work with input and output, and imo they are easier to work with than scanf().

    putchar() takes an integer as it's arguement and prints it to stdout, which is the terminal usually. Remember that integers and characters are basically interchangeable. A character in C is considered to be a "small integer" because internally it is represented by a number. So you could do something like
    Code:
    /* print the letter k */
    char letter = 'k';
    putchar(k);
    
    or...
    
    /* print a newline, 10 is the decimal value for the newline character */
    putchar(10);
    Both are valid and work, just think of them as being basically interchangeable.

    getchar() reads the next character from stdin and returns it. So you could do something like..
    Code:
    /* Get a character and print it */
    int c;
    c = getchar();
    putchar(c);
    Edit- woops just noticed, you were using void main() in your program hehe. Not good!
    Use int main(void) instead.

    Goodluck.
    Last edited by SourceCode; 06-18-2003 at 04:16 PM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by SourceCode

    getchar() reads the next character from stdin and returns it. So you could do something like..
    Code:
    /* Get a character and print it */
    int c;
    c = getchar();
    puts(c);
    Goodluck.
    Um, no.

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

  5. #5
    .........
    Join Date
    Nov 2002
    Posts
    303
    Woops yea meant putchar() sorry.

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    25
    Alright, I belive i understand now

    Code:
    #include <stdio.h>
    
    
    int main()
    {
            char first;
            char second;
    	char enter;		/* Enter key */
    
            printf("Enter a letter:");
            first = getchar();
            
    	enter = getchar();	/* Recieves \n (never used) */
    	
    	printf("Enter another letter:");
            second = getchar();       
            
    	printf("%c %c", first,second);
    	
    	return 0;
    }
    Works perfectly, thanks.

  7. #7
    Banned
    Join Date
    May 2003
    Posts
    124

    ...

    Well.. why you don't follow Source Code's code? That's very good.
    I don't find your code very elegant.... and you use another variable.

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    25
    Then i would like you to try making my code, and omit using another variable.

    And if you ask me, my code is perfectly fine. Because it does exactly as i want.

  9. #9
    Banned
    Join Date
    May 2003
    Posts
    124
    >Then i would like you to try making my code, and omit using another variable.
    Haha! I do the others code when:
    1) I want to do so, not when you want.
    2) There is a reason ( now there isn't any - Source Code's code explains everything ).

    >And if you ask me, my code is perfectly fine. Because it does exactly as i want.
    Well, it's like asking you to print the numbers between 1 and 100, and you write 100 printf statements. Then it will does exactly as you want, as you stated before, which is to output all the numbers. But it won't be perfectly fine, as you stated previously, for obvious reasons.
    Or it's like using ten variables instead of an array with 10 elements.

    I hope you get my point

  10. #10
    Banned
    Join Date
    May 2003
    Posts
    124
    getchar(); /* Recieves \n (never used) */
    You don't use extra variable now

  11. #11
    Registered User
    Join Date
    May 2003
    Posts
    25
    Oh I see what you mean by extra variable now.

    Thought you had to give getchar() a variable so it can be used.

    *oh powerful all knowing programmer, please forgive my noob like thinking*

  12. #12
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>/* Recieves \n (never used) */
    Of course, this does assume that the user has actually done what they are supposed to, ie keyed one character and pressed enter. If they key two characters and pressed enter, then what?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Hammer
    >>/* Recieves \n (never used) */
    Of course, this does assume that the user has actually done what they are supposed to, ie keyed one character and pressed enter. If they key two characters and pressed enter, then what?
    Well then you read the FAQ...

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

  14. #14
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    Originally posted by quzah
    Well then you read the FAQ...

    Quzah.
    Then post the question here because you didn't read it thoroughly
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

  15. #15
    Registered User
    Join Date
    May 2003
    Posts
    25
    Well if the user is stupid enough to regard "Enter a letter:" as, let me enter 2 or more...

    Then what can I say? Its simple intructions if you ask me.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 04:25 PM
  2. character input using fgetc() code???
    By lesrhac03 in forum C Programming
    Replies: 3
    Last Post: 03-27-2008, 11:55 PM
  3. Problems reading formatted input with sscanf
    By Nazgulled in forum C Programming
    Replies: 17
    Last Post: 05-10-2006, 01:46 AM
  4. Trouble with a lab
    By michael- in forum C Programming
    Replies: 18
    Last Post: 12-06-2005, 11:28 PM
  5. input output problems
    By gell10 in forum C++ Programming
    Replies: 3
    Last Post: 11-03-2003, 09:39 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21