Puzzle!

Discussion in 'C Programming' started by Tak, Jun 1, 2007.

  1. Tak

    Tak Guest

    Some days ago, I written a mini program like this:
    #include <stdio.h>

    int main()
    {
    char c;
    int n;

    scanf("%d",&n);
    c = getchar();

    return 0;
    }

    I want to assign n and c specific values,for example,give n and c
    values of 5 and 'x' in fact when I input 5 then "enter",the program
    is finish.I know when I press "Enter", I assigned '\n' to variable c,
    But how can I avoid this?
     
    Tak, Jun 1, 2007
    #1
    1. Advertising

  2. Tak

    Tak Guest

    On 6 2 , 1 15 , Tak <> wrote:
    > Some days ago, I written a mini program like this:
    > #include <stdio.h>
    >
    > int main()
    > {
    > char c;


    a mistake: "int c" not "char c"

    > int n;
    >
    > scanf("%d",&n);
    > c = getchar();
    >
    > return 0;
    >
    > }
     
    Tak, Jun 1, 2007
    #2
    1. Advertising

  3. In article <>,
    Tak <> wrote:
    >Some days ago, I written a mini program like this:
    >#include <stdio.h>


    >int main()
    >{
    > char c;
    > int n;
    > scanf("%d",&n);
    > c = getchar();
    > return 0;
    >}


    >I want to assign n and c specific values,for example,give n and c
    >values of 5 and 'x' in fact when I input 5 then "enter",the program
    >is finish.I know when I press "Enter", I assigned '\n' to variable c,
    >But how can I avoid this?


    while ( (c = getchar()) != EOF && c == '\n' ) { /* empty loop body */ }

    Afterwards, c will either be EOF or a character other than \n .
    This takes into account that the user might press return several times
    before entering the desired character.

    But to make this work, you will have to fix the problem the other poster
    pointed out: that c should be of type int, not of type char.

    --
    Prototypes are supertypes of their clones. -- maplesoft
     
    Walter Roberson, Jun 1, 2007
    #3
  4. Tak <> writes:
    > Some days ago, I written a mini program like this:
    > #include <stdio.h>
    >
    > int main()
    > {
    > char c;
    > int n;
    >
    > scanf("%d",&n);
    > c = getchar();
    >
    > return 0;
    > }
    >
    > I want to assign n and c specific values,for example,give n and c
    > values of 5 and 'x' in fact when I input 5 then "enter",the program
    > is finish.I know when I press "Enter", I assigned '\n' to variable c,
    > But how can I avoid this?


    (You've already mentioned in a followup that c needs to be an int.)

    If you type "5x" and then <enter>, the program will assign 5 to n and
    'x' to c.

    In a real program, that would be a very unfriendly input format. If
    you must use scanf, you should (a) check its return value, and (b) be
    aware of the state the input stream will be in after the call, and act
    accordingly; for example, you might want to read and ignore all
    characters up to and including a newline (and don't forget to check
    for EOF).

    A better way to handle input is to read a line at a time using
    fgets(), and then analyze the input line once you have it.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jun 1, 2007
    #4
  5. On Fri, 01 Jun 2007 17:15:12 -0000, in comp.lang.c , Tak
    <> wrote:

    >Some days ago, I written a mini program like this:
    >#include <stdio.h>
    >
    > scanf("%d",&n);
    > c = getchar();


    >I want to assign n and c specific values,for example,give n and c
    >values of 5 and 'x' in fact when I input 5 then "enter",the program
    >is finish.I know when I press "Enter", I assigned '\n' to variable c,
    >But how can I avoid this?


    Don't use scanf.
    --
    Mark McIntyre

    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
     
    Mark McIntyre, Jun 2, 2007
    #5
  6. Tak

    Tak Guest

    On 6 2 , 7 26 , Mark McIntyre <> wrote:
    > On Fri, 01 Jun 2007 17:15:12 -0000, in comp.lang.c , Tak
    >
    > <> wrote:
    > >Some days ago, I written a mini program like this:
    > >#include <stdio.h>

    >
    > > scanf("%d",&n);
    > > c = getchar();
    > >I want to assign n and c specific values,for example,give n and c
    > >values of 5 and 'x' in fact when I input 5 then "enter",the program
    > >is finish.I know when I press "Enter", I assigned '\n' to variable c,
    > >But how can I avoid this?

    >
    > Don't use scanf.


    Then use what?
     
    Tak, Jun 2, 2007
    #6
  7. Tak

    Flash Gordon Guest

    Tak wrote, On 02/06/07 04:31:
    > On 6 2 , 7 26 , Mark McIntyre <> wrote:
    >> On Fri, 01 Jun 2007 17:15:12 -0000, in comp.lang.c , Tak
    >>
    >> <> wrote:
    >>> Some days ago, I written a mini program like this:
    >>> #include <stdio.h>
    >>> scanf("%d",&n);
    >>> c = getchar();
    >>> I want to assign n and c specific values,for example,give n and c
    >>> values of 5 and 'x' in fact when I input 5 then "enter",the program
    >>> is finish.I know when I press "Enter", I assigned '\n' to variable c,
    >>> But how can I avoid this?

    >> Don't use scanf.

    >
    > Then use what?


    fgets (NOT gets, NEVER gets), getchar, getc, fgetc as appropriate. There
    are gotchas on these so you need to read the documentation and search
    the groups and the FAQ for further information.
    --
    Flash Gordon
     
    Flash Gordon, Jun 2, 2007
    #7
  8. Tak

    Army1987 Guest

    "Mark McIntyre" <> ha scritto nel messaggio
    news:...
    > On Fri, 01 Jun 2007 17:15:12 -0000, in comp.lang.c , Tak
    > <> wrote:
    >
    >>Some days ago, I written a mini program like this:
    >>#include <stdio.h>
    >>
    >> scanf("%d",&n);
    >> c = getchar();

    >
    >>I want to assign n and c specific values,for example,give n and c
    >>values of 5 and 'x' in fact when I input 5 then "enter",the program
    >>is finish.I know when I press "Enter", I assigned '\n' to variable c,
    >>But how can I avoid this?

    >
    > Don't use scanf.


    A little too extreme...
    For example:

    do {
    int k = scanf("%d", &n);
    switch (k) {
    case EOF:
    /* stdin is finished or there was something wrong. */
    /* React accordingly. */
    break;
    case 0:
    puts("Please enter an integer");
    /* maybe write that to stderr? */
    scanf("%*[^\n]%*c"); /*skip line*/
    continue; /* yes, here break and continue would do the */
    /* same thing... :) */
    case 1:
    break;
    } while (k < 0);

    Or to avoid strange problem if the number input is too large:

    #include <limits.h>
    #include <errno.h>
    do {
    long tmp;
    int k = scanf("%ld", &tmp);
    switch (k) {
    case EOF:
    /* stdin is finished or there was something wrong. */
    /* React accordingly. */
    break;
    case 0:
    puts("Please enter an integer");
    /* maybe write that to stderr? */
    scanf("%*[^\n]%*c"); /*skip line*/
    continue; /* yes, here break and continue would do the */
    /* same thing... :) */
    case 1:
    if (tmp < INT_MIN || tmp > INT_MAX) {
    k = tmp < 0 ? INT_MIN : INT_MAX;
    errno = ERANGE;
    } else
    k = tmp;
    break;
    } while (k < 0);
     
    Army1987, Jun 2, 2007
    #8
  9. Tak

    Tak Guest

    On 6 2 , 3 58 , "Army1987" <> wrote:
    > "Mark McIntyre" <> ha scritto nel messaggionews:...
    >

    <snip>
    > scanf("%*[^\n]%*c"); /*skip line*/


    I've learnt that statement /scanf("%[^.]", str); /is usually dangerous
    in some conditions.
     
    Tak, Jun 2, 2007
    #9
  10. Tak

    Tak Guest

    On 6 2 , 3 39 , Flash Gordon <> wrote:
    > Tak wrote, On 02/06/07 04:31:
    >
    > > On 6 2 , 7 26 , Mark McIntyre <> wrote:
    > >> On Fri, 01 Jun 2007 17:15:12 -0000, in comp.lang.c , Tak

    > > Then use what?

    >
    > fgets (NOT gets, NEVER gets), getchar, getc, fgetc as appropriate. There
    > are gotchas on these so you need to read the documentation and search
    > the groups and the FAQ for further information.



    I solve problems from ACM-ICPC, so fgets fgetc are not useful.
    usually I solved like this :
    int main()
    {
    int c;
    int n;

    scanf("%d",&n);
    c = getchar();
    c = getchar();

    return 0;

    }
     
    Tak, Jun 2, 2007
    #10
  11. Tak wrote:
    > On 6 2 , 3 58 , "Army1987" <> wrote:
    >> "Mark McIntyre" <> ha scritto nel
    >> messaggionews:...
    >>

    > <snip>
    >> scanf("%*[^\n]%*c"); /*skip line*/

    >
    > I've learnt that statement /scanf("%[^.]", str); /is usually dangerous
    > in some conditions.


    You've learnt correctly, but that's not what's happening here. The problem
    with scanf("%[^.]", str); is that you cannot know in advance whether enough
    memory is available to store the string in str. scanf("%*[^\n]%*c");
    discards everything it reads, instead of storing it.
     
    Harald van =?UTF-8?B?RMSzaw==?=, Jun 2, 2007
    #11
  12. Tak

    Army1987 Guest

    "Tak" <> ha scritto nel messaggio
    news:...

    > I solve problems from ACM-ICPC, so fgets fgetc are not useful.
    > usually I solved like this :
    > int main()
    > {
    > int c;
    > int n;
    >
    > scanf("%d",&n);
    > c = getchar();
    > c = getchar();
    >
    > return 0;
    >
    > }


    This is ok only as long as you know the only character between one number
    and the character to be read will be the newline.
    BTW, why do you assign c in both calls of getchar()? Isn't
    getchar(); c = getchar(); ok?
     
    Army1987, Jun 2, 2007
    #12
  13. Tak

    Tak Guest

    On 6 2 , 7 21 , "Army1987" <> wrote:
    > "Tak" <> ha scritto nel messaggionews:...
    >
    > > I solve problems from ACM-ICPC, so fgets fgetc are not useful.
    > > usually I solved like this :
    > > int main()
    > > {
    > > int c;
    > > int n;

    >
    > > scanf("%d",&n);
    > > c = getchar();
    > > c = getchar();

    >
    > > return 0;

    >
    > > }

    >
    > This is ok only as long as you know the only character between one number
    > and the character to be read will be the newline.
    > BTW, why do you assign c in both calls of getchar()? Isn't
    > getchar(); c = getchar(); ok?


    Yes , it seems cleaner.
     
    Tak, Jun 2, 2007
    #13
  14. In article <>,
    CBFalconer <> wrote:

    >I have no idea what ACM-ICPC is.


    Association for Computing Machinary, International C Programming Contest?
    --
    I was very young in those days, but I was also rather dim.
    -- Christopher Priest
     
    Walter Roberson, Jun 2, 2007
    #14
  15. CBFalconer said:

    > Flash Gordon wrote:
    >> Tak wrote, On 02/06/07 04:31:
    >>

    > ... snip ...
    >>
    >>> Then use what?

    >>
    >> fgets (NOT gets, NEVER gets), getchar, getc, fgetc as appropriate.
    >> There are gotchas on these so you need to read the documentation
    >> and search the groups and the FAQ for further information.

    >
    > Or ggets. I am pushing this with the idea of getting it
    > incorporated in the next version of the C standard.


    For so long as it was your pet function, okay, fine - but if you're
    going to try to get it into the C Standard, please at the very least
    make it take a size_t to specify the maximum buffer allocation you're
    prepared to tolerate. As it stands, it's wide open to a Denial of
    Memory attack.

    For the record, I'm not convinced that it would make a good addition to
    the Standard. I'm not even convinced that my own fgetline function
    (which is also freely available, and which I obviously prefer to ggets)
    would make a good addition.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at the above domain, - www.
     
    Richard Heathfield, Jun 2, 2007
    #15
  16. -cnrc.gc.ca (Walter Roberson) writes:
    > In article <>,
    > CBFalconer <> wrote:
    >>I have no idea what ACM-ICPC is.

    >
    > Association for Computing Machinary, International C Programming Contest?


    ACM International Collegiate Programming Contest.
    <http://icpc.baylor.edu/icpc/>

    I have no idea why this implies that one can't use fgets and fgetc.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jun 2, 2007
    #16
  17. "Army1987" <> writes:
    > "Mark McIntyre" <> ha scritto nel messaggio
    > news:...

    [...]
    >> Don't use scanf.

    >
    > A little too extreme...
    > For example:
    >
    > do {
    > int k = scanf("%d", &n);

    [snip]
    >
    > Or to avoid strange problem if the number input is too large:
    >
    > #include <limits.h>
    > #include <errno.h>
    > do {
    > long tmp;
    > int k = scanf("%ld", &tmp);

    [snip]

    C99 7.19.6.2p10:

    If this object does not have an appropriate type, or if the result
    of the conversion cannot be represented in the object, the
    behavior is undefined.

    This applies to fscanf, scanf, and sscanf. If I run your program and
    enter a number that won't fit in type long, I risk nasal demons.

    Using any of the *scanf functions with any of the numeric formats,
    unless you have complete control over what appears in the input, is
    dangerous. (IMHO, this is a flaw in the standard; this should have
    been treated as a matching failure.)

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Jun 2, 2007
    #17
  18. On Sat, 2 Jun 2007 09:58:26 +0200, in comp.lang.c , "Army1987"
    <> wrote:

    >
    >"Mark McIntyre" <> ha scritto nel messaggio
    >news:...
    >> On Fri, 01 Jun 2007 17:15:12 -0000, in comp.lang.c , Tak
    >> <> wrote:
    >>
    >>>Some days ago, I written a mini program like this:
    >>>#include <stdio.h>
    >>>
    >>> scanf("%d",&n);
    >>> c = getchar();

    >>
    >>>I want to assign n and c specific values,for example,give n and c
    >>>values of 5 and 'x' in fact when I input 5 then "enter",the program
    >>>is finish.I know when I press "Enter", I assigned '\n' to variable c,
    >>>But how can I avoid this?

    >>
    >> Don't use scanf.

    >
    >A little too extreme...


    okay, fuller version

    1) don't use scanf

    2) (experts only) use scanf with the care you would take to handle
    live scorpions while holding a sword point-first between your teeth.
    --
    Mark McIntyre

    "Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are,
    by definition, not smart enough to debug it."
    --Brian Kernighan
     
    Mark McIntyre, Jun 3, 2007
    #18
  19. Tak

    Tor Rustad Guest

    Keith Thompson wrote:
    >
    > C99 7.19.6.2p10:
    >
    > If this object does not have an appropriate type, or if the result
    > of the conversion cannot be represented in the object, the
    > behavior is undefined.
    >
    > This applies to fscanf, scanf, and sscanf. If I run your program and
    > enter a number that won't fit in type long, I risk nasal demons.


    Hmm... I can't say I agree with your interpretation Keith.

    How can the %li conversion in scanf overflow a long object?
    For %s and %c conversions, it's possible with such an overflow, but I
    don't see the same for "numbers".

    Of course, I assume there is no type mismatch... between conversion
    specifiers and object.

    --
    Tor <torust [at] online [dot] no>
     
    Tor Rustad, Jun 3, 2007
    #19
  20. Tak

    Tor Rustad Guest

    Mark McIntyre wrote:
    > On Sat, 2 Jun 2007 09:58:26 +0200, in comp.lang.c , "Army1987"
    > <> wrote:
    >
    >> "Mark McIntyre" <> ha scritto nel messaggio
    >> news:...
    >>> On Fri, 01 Jun 2007 17:15:12 -0000, in comp.lang.c , Tak
    >>> <> wrote:
    >>>
    >>>> Some days ago, I written a mini program like this:
    >>>> #include <stdio.h>
    >>>>
    >>>> scanf("%d",&n);
    >>>> c = getchar();
    >>>> I want to assign n and c specific values,for example,give n and c
    >>>> values of 5 and 'x' in fact when I input 5 then "enter",the program
    >>>> is finish.I know when I press "Enter", I assigned '\n' to variable c,
    >>>> But how can I avoid this?
    >>> Don't use scanf.

    >> A little too extreme...

    >
    > okay, fuller version
    >
    > 1) don't use scanf


    Very strange advice, the problem at hand, *might* have specified that OP
    don't even need to check for input errors.


    > 2) (experts only) use scanf with the care you would take to handle
    > live scorpions while holding a sword point-first between your teeth.


    You can check for errors with scanf() too, it does have a return value
    AND you do have the feof() and ferror() functions.

    --
    Tor <torust [at] online [dot] no>
     
    Tor Rustad, Jun 3, 2007
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Earl Teigrob
    Replies:
    3
    Views:
    6,652
    Nedu N
    Aug 6, 2003
  2. dwa

    Design Puzzle!

    dwa, Jun 10, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    370
    Cowboy \(Gregory A. Beamer\) [MVP]
    Jun 10, 2004
  3. Shankara Narayanan

    Booking puzzle....

    Shankara Narayanan, Jun 17, 2004, in forum: ASP .Net
    Replies:
    20
    Views:
    933
    bredal Jensen
    Jun 30, 2004
  4. VB Programmer
    Replies:
    2
    Views:
    430
    Alan Lambert
    Nov 4, 2004
  5. G. Stewart

    regex puzzle!

    G. Stewart, Nov 23, 2004, in forum: ASP .Net
    Replies:
    8
    Views:
    518
    G. Stewart
    Nov 25, 2004
Loading...

Share This Page