Thread: reading 2 ints and a char

  1. #16
    Registered User sagar474's Avatar
    Join Date
    Jun 2011
    Location
    Kakinada
    Posts
    56
    scanf ("%c", val2); ---> scanf ("\n%c", val2); on line 25

    add \n before %c

    because you press enter after entering previous number. you are also giving a new line char "\n" hence the scanf(); is taking "\n" as input.
    Last edited by sagar474; 11-08-2011 at 04:39 PM.

  2. #17
    Registered User sagar474's Avatar
    Join Date
    Jun 2011
    Location
    Kakinada
    Posts
    56
    scanf ("%c", val2); ---> scanf ("\n%c", val2); on line 25

    add \n before %c

    because you press enter after entering previous number. you are also giving a new line char "\n" hence the scanf(); is taking "\n" as input.

  3. #18
    Registered User oluwasegun's Avatar
    Join Date
    Nov 2011
    Location
    lagos nigeria
    Posts
    3

    Thumbs up

    line 15 of your code is wrong :
    printf ("read values: %d %c %d", &val1, &val2, &val3);

    the address operator (&) cannot be used with an output function such as printf

    change it to :
    printf ("read values: %d %c %d", val1, val2, val3);

    this will print out the correct value

  4. #19
    Registered User sagar474's Avatar
    Join Date
    Jun 2011
    Location
    Kakinada
    Posts
    56
    Quote Originally Posted by oluwasegun View Post
    line 15 of your code is wrong :
    printf ("read values: %d %c %d", &val1, &val2, &val3);

    the address operator (&) cannot be used with an output function such as printf

    change it to :
    printf ("read values: %d %c %d", val1, val2, val3);

    this will print out the correct value
    he already changed it.
    his latest code is in post 15

  5. #20
    Registered User
    Join Date
    Oct 2011
    Posts
    11
    sagar
    This worked when I used your suggestion. But to understand this better, why is the scanf() taking the \n as input even before displaying the text - Enter value 2:
    Also in the statement scanf (val3); it seems to work without the \n. Is this because now an int is being read rather than a char?

    Thanks

  6. #21
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    When you enter your first digit, 2, you follow it with a newline. The stdin buffer then has '\n'2 waiting.
    %d tells scanf() to skip whitespace, read digits from buffer and remove from buffer until non-digit is encountered. So waiting in the buffer is that '\n' which is a valid character for scanf("%c"...) to read.

    Specifying "\n%c"... works but I don't recommend using specific text input. What if a user enters 2 space tab enter ?

    Just put a leading space after the open quote and before the %c and All leading whitespace will be ignored.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Push ints into a char vector
    By Prediluted in forum C++ Programming
    Replies: 10
    Last Post: 06-11-2011, 01:36 PM
  2. Dealing with ints that are in char array
    By Wiretron in forum C Programming
    Replies: 4
    Last Post: 05-15-2006, 04:06 PM
  3. reading 3 ints from one line, then 3 from another
    By Tokay in forum C++ Programming
    Replies: 10
    Last Post: 11-13-2005, 09:42 PM
  4. need help with char and ints in an array
    By satory in forum C Programming
    Replies: 1
    Last Post: 12-05-2004, 01:41 PM
  5. Reading an unknown # of ints into array
    By jds in forum C Programming
    Replies: 3
    Last Post: 10-10-2003, 04:16 PM