Why doesn't this work?

This is a discussion on Why doesn't this work? within the C++ Programming forums, part of the General Programming Boards category; int m[3]; scanf( "%1d%1d%1d", &m[0], &m[1], &m[2] ); I would expect input "123" (without the quotes) to store values 1, ...

  1. #1
    Registered User
    Join Date
    Jul 2009
    Posts
    3

    Why doesn't this work?

    int m[3];

    scanf( "%1d%1d%1d", &m[0], &m[1], &m[2] );

    I would expect input "123" (without the quotes) to store values 1, 2 and 3 in the array.

    What am I doint wrong?

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    I would expect input "123" (without the quotes) to store values 1, 2 and 3 in the array.
    I would expect it to do the same. Can you post a complete program which demonstrates the error?

  3. #3
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    that's exactly what it does
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  4. #4
    Webhead Spidey's Avatar
    Join Date
    Jul 2009
    Posts
    285
    What error are you getting ?
    Spidey out!

  5. #5
    Registered User
    Join Date
    Jul 2009
    Posts
    3
    Okay, problem seems to be left over from previous scanf:

    int year, data[3];

    printf( "Year = " );
    scanf( "%d", &year );
    printf( "data = " );
    scanf( "%1d%1d%1d", &data[0], &data[1], &data[2] );

    Year reads into storage ok. Second scanf() doesn't hang up, but doesn't place any values in data[], either. Somehow the CR/LF needs to be handled when reading the year, but I can't figure out how to do it.

    Paul

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    > What am I doint wrong?
    a) not reading the manual
    b) assuming that printf and scanf are perfectly symmetrical in their format strings - they are not.

    scanf does NOT have field widths on input, so your attempt to read only 1 digit is broke.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Salem View Post
    > What am I doint wrong?
    a) not reading the manual
    b) assuming that printf and scanf are perfectly symmetrical in their format strings - they are not.

    scanf does NOT have field widths on input, so your attempt to read only 1 digit is broke.
    Somebody didn't read the manual, it is true:
    Quote Originally Posted by man scanf as posted
    In addition to these flags, there may be an optional maximum field
    width, expressed as a decimal integer, between the % and the conver-
    sion. If no width is given, a default of 'infinity' is used (with one
    exception, below); otherwise at most this many characters are scanned
    in processing the conversion. Before conversion begins, most conver-
    sions skip white space; this white space is not counted against the
    field width.
    The code you posted reads in 1, 2, and 3 into data[0], data[1], and data[2] respectively on everything I could be bothered to check it on.

  8. #8
    Registered User
    Join Date
    Jul 2009
    Posts
    3
    Thanks, folks, for trying to help. Here is the solution:

    int year, data[3];
    char dum;

    printf( "Year = " );
    scanf( "%d%c", &year, dum );
    printf( "data = " );
    scanf( "%1d%1d%1d%c", &data[0], &data[1], &data[2], dum );

    Apparently the %c takes care of the CR in the first scanf(), leaving the buffer ready for the next one.

    What the manual says about this (Harbison and Steele, pg 326) is "Trailing whitespace characters (such as the newline that terminates a line of input) will remain unread unless explicitly matched in the control string. However, doing this may be tricky ..." I had read that, but wasn't bright enough to understand what it meant until I had done a lot of experimentation. Finally got it right.

    Again, thanks to you all,

    Paul

  9. #9
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    You need to use &dum, if dum is a char variable.

    That will, I suppose, work, but it is overkill since what you had originally worked too.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    Don't forget [code]code tags[/code] in the future!
    << !! Posting Code? Read this First !! >>
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by paulrswan View Post

    int year, data[3];

    printf( "Year = " );
    scanf( "%d", &year );
    printf( "data = " );
    scanf( "%1d%1d%1d", &data[0], &data[1], &data[2] );
    this works perfectly
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM

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