Re: An interesting c program for beginners

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Niz

    Re: An interesting c program for beginners

    On 2008-05-05 12:23:26 +0100, apaticul@gmail. com said:
    >
    printf("First name: ");
    gets(first);
    printf("Last Name: ");
    gets(last);
    printf("Age: ");
    scanf("%d", age);
    printf("Social security number (no hyphens, just 3 figures): ");
    scanf("%d", ssn);
    printf("Salary: ");
    scanf("%f", salary);
    Use fgets() instead of gets() it is much safer.

  • Bill Leary

    #2
    Re: An interesting c program for beginners

    Small nit:

    "Niz" <niz@nicetry.co mwrote in message
    news:2008050513 182216807-niz@nicetrycom. ..
    On 2008-05-05 12:23:26 +0100, apaticul@gmail. com said:
    > printf("Social security number (no hyphens, just 3 figures): ");
    Three figures? All the SSN's I've seen are nine.

    Did I miss something earlier in the thread where you wanted only three digit
    portion of the SSN? If so, it seems the prompt should say so.

    Also, I thought it wasn't guaranteed (assured? assumed?) that any output
    would occur without a newline or an fflush. When I did stuff like this, I
    used:

    printf("Social security number (no hyphens, just 9 figures): ");
    fflush(stdout);
    scanf("%d", ssn);

    In cases where I forgot the fflush, for some platforms it worked as desired
    anyway. But on others, the prompts wouldn't appear until. In most of those
    cases the prompt appeared after I hit Enter for the input value, like this:

    123456789Social security number (no hyphens, just 9 figures
    []

    That [] represents the cursor at the beginning of the next line.

    But I also remember one system where NONE of the prompts appeared at all.
    Then when I did a printf, which had a newline, all the prompts showed up at
    the same time, followed by the printf text. The cause being how the
    c-library and/or operating system did output buffering.

    - Bill

    Comment

    Working...