#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    38
    Rep Power
    2

    Arrow Read input, do something, read more input (simple question)


    I'm working on a program that takes standard input, makes a ordered linked list, then prints the list depending on the next user input (y/n).

    The problem I have however is taking more standard input after the first.

    Code:
    #include <stdio.h>
    
    int main ( void ) {
    	char c;
    	scanf("%c", &c);
    	printf("output: %c", c);
    	scanf("%c", &c);
    	printf("output: %c", c);
    	return 0;
    }
    When running the program, if I type a character (say y), it prints the character and stops the program before getting to the second scanf.

    This happens with both getchar and scanf.

    I know it's probably a simple answer but I can't work it out.

    Thanks for any help.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,841
    Rep Power
    480
    Did you really only type "y"? I'll bet you also pressed the Enter key. Please read the frequently asked questions.

    FAQ

    explains this. Also, did output: display twice? Observe carefully, report carefully. Thanks.
    Last edited by b49P23TIvg; January 8th, 2013 at 09:19 AM.
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    165
    Rep Power
    18
    Your code is lading a single input and reading the first two chars try this code

    Code:
    #include <stdio.h>
    
    char getChar(){
    char a;
    	scanf("%s", &a);
    return a;
    }
    
    int main ( void ) {
    	char a;
    	
    	printf("output: %c \n", getChar());
    	
    	printf("output: %c \n", getChar());
    	return 0;
    }
    Originally Posted by b49P23TIvg
    Did you really only type "y"? I'll bet you also pressed the Enter key. Please read the frequently asked questions.

    FAQ

    explains this. Also, did output: display twice? Observe carefully, report carefully. Thanks.

    Comments on this post

    • dwise1_aol disagrees : Clean up in aisle getChar! Buffer overflow!
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,145
    Rep Power
    2222
    EffX, did you understand b49P23TIvg's answer?

    The input functions of the Standard C Library date back to when you communicated with the main-frame or mini-computer through a terminal, most of where were line-oriented, so the input functions are line-oriented. You need to hit the Enter key before an input function such as getchar or scanf will even begin to start processing the input line. Therefore, an input line will contain the characters typed plus the newline character for the Enter key.. If you tell scanf to read in the next character in the input buffer, regardless of what kind of character it is, then it's going to read in the newline before it would ever think of waiting for you to enter in a second line.

    Newlines, spaces, tabs, and the like are called white space. You want scanf to ignore all the white space that precedes the next input character, so that is what you need to tell scanf. A space in the format string represents white space, so you need to insert a space before the character specifier, thus:
    scanf(" %c", &c);

    An alternative that would work with getchar as well would be to simply run a do-while loop in which you would read in all the characters and from which you would only exit if the character read in is a printable character ([A-Z a-z 0-9]). Consult the functions in ctype.h or else write your own.

    Comments on this post

    • portcitysoftwar agrees : Very nicely put! Thanks for correcting me
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2012
    Posts
    165
    Rep Power
    18
    Thanks for correcting me there. I guess it's been to long sense iv messed around with C and C++ been used to languages with built in protection against invalid input and buffer overflows.

    Originally Posted by dwise1_aol
    EffX, did you understand b49P23TIvg's answer?

    The input functions of the Standard C Library date back to when you communicated with the main-frame or mini-computer through a terminal, most of where were line-oriented, so the input functions are line-oriented. You need to hit the Enter key before an input function such as getchar or scanf will even begin to start processing the input line. Therefore, an input line will contain the characters typed plus the newline character for the Enter key.. If you tell scanf to read in the next character in the input buffer, regardless of what kind of character it is, then it's going to read in the newline before it would ever think of waiting for you to enter in a second line.

    Newlines, spaces, tabs, and the like are called white space. You want scanf to ignore all the white space that precedes the next input character, so that is what you need to tell scanf. A space in the format string represents white space, so you need to insert a space before the character specifier, thus:
    scanf(" %c", &c);

    An alternative that would work with getchar as well would be to simply run a do-while loop in which you would read in all the characters and from which you would only exit if the character read in is a printable character ([A-Z a-z 0-9]). Consult the functions in ctype.h or else write your own.

IMN logo majestic logo threadwatch logo seochat tools logo