Why should i use the system

This is a discussion on Why should i use the system within the C Programming forums, part of the General Programming Boards category; Just wondering why this code doesn't display the last print statement. In place of getchar i have to use a ...

  1. #1
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751

    Why should i use the system

    Just wondering why this code doesn't display the last print statement. In place of getchar i have to use a system call.
    Code:
    system("pause")
    .

    here it is. What is the logic i'm missing.
    Code:
    /* scanf used properly */
    
    #include <stdio.h>
    
    int main()
    
    {
    
        int sum;
        char letter;
        float money;
    
        printf("Please enter an integer ");
        scanf("%d", &sum);
    
        printf("Please enter a character ");
        /* leading space before the %c ignores whitespace in input */
        scanf(" %c", &letter);
    
        printf("Please enter a float variable ");
        scanf("%f", &money);
    
        printf("\nThe values you entered were\n");
        printf("Sum = %d, letter = %c, money = %.2f\n", sum, letter, money);
    
        getchar();
        return 0;
    
    }
    Last edited by caroundw5h; 12-11-2003 at 07:34 AM.

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Because scanf() leave the newline in the input buffer, so when the program gets to getchar() it reads the newline from the input buffer and doesn't stop.

    also
    Code:
    scanf("%d", ?);
    I take it this is an error?

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    It prints the last statement, you just can't blink or you may miss it. Change this:
    Code:
    scanf("%f", &money);
    to this:
    Code:
    scanf(" %f", &money);
    >/* scanf used properly */
    Not really. For even remotely proper scanf usage, you need to check the return value. Basically anything that takes outside input that has a chance of failure (interactive input meets both of these requirements), you should check for errors.
    My best code is written with the delete key.

  4. #4
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Not too sure what you were saying thantos. Can you let me know again.

    also prelude:
    Code:
    scanf("  %f", &money)
    doesn't that ignores leading whitespace. I haven't tested to see if it works but curious about what else it does that i am not aware of.


    in regards to
    Code:
    scanf("%d", x)
    x being that math symbol i can't find on my keyboard - i swear to god i tried to edit the code, but it look fine like so
    Code:
    scanf("%d", &sum) /* this is not how it is supposed to look */
    wonder what i did?

    okay this is freaky. I just previed my reply and when i do the ampersand and then sum it prints out this &sum. If i leave out a space it prints out this
    Code:
    & sum
    . WTF?

    btw prelude just tested your code, and it still flashes w/o me being able to see the results. Also yeah, i'm aware the comments from the code is a lie. I really should have validated the user input with the stdlib and ctypes.h I did this mainly to test the arguments and errors scanf would return. I don't understand why the iso/ainsi committee even kept this. What is its purpose other than to get the user to start accepting user input right away?
    A point that is echoed seemingly here.
    Last edited by caroundw5h; 12-11-2003 at 07:56 AM.

  5. #5
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >doesn't that ignores leading whitespace
    Yes.

    >and it still flashes w/o me being able to see the results
    You have an exceptionally annoying implementation of scanf then. You'll need to either clear out the stream somehow, or double up your getchar calls if you're sure that there's only a newline left in stdin. Really all you have to remember is that the only input function that works well with scanf is scanf. Anything else requires you to jump through hoops to get it working.

    >I don't understand why the iso/ainsi committee even kept this.
    To keep old code from breaking. Pre-standard programs used scanf religiously, so the ISO standard had little choice but to keep it. Also consider that stdin can be redirected to a file stream. In those cases scanf is correct, safe, and slightly more convenient than fscanf.

    >WTF?
    These forums act funny with &<some specific character sequence>.
    My best code is written with the delete key.

  6. #6
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    Ok I'll try to re-explain.

    When the user types something it firsts goes to the input buffer. From there any program that is requesting data from the input buffer retrieves it.

    So lets take the case the user inputed:
    ABCDEFG<ENTER>

    scanf has a nasty habit of grabbing ABCDEFG but it won't grab the <ENTER>. So this <ENTER> is still sitting in the buffer waiting to be used. Now we get to the another scanf and the user inputs:
    HIJKLMN<ENTER>. So now the input buffer is <ENTER>HIJKLMN<ENTER>. Scanf gets the first <ENTER> and ignores it knowing that it wants some data. So then it grabs HIJKLMN and leaves the second <ENTER> is the buffer. Repeat through all the scanfs. At the end of the last scanf there is still a <ENTER> in the buffer. So when the programs gets to getchar it sees that there is something in the buffer and gets the <ENTER>. Now getchar is happy with an <ENTER> all by itself so its done doing its job. Then it goes to the return 0 and the program executes.

    Hoped that cleared it up somewhat? I was trying to keep it simple so if you feel I insulted your intelligence, sorry.

  7. #7
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    as for the &sum; thing, your code is:
    Code:
    scanf("%d", &amp;sum);
    and in HTML:
    Code:
    &amp;sum; == &sum;
    Got it now?

    To solve the problem, use &amp;amp; for the &.

  8. #8
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Thanks guys. You all cleared up a lot.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File System Implementation
    By dodgeviper in forum C Programming
    Replies: 9
    Last Post: 11-16-2007, 12:04 PM
  2. Using system icons
    By @nthony in forum Windows Programming
    Replies: 1
    Last Post: 01-13-2007, 06:56 PM
  3. Linux database system needed
    By BobS0327 in forum Tech Board
    Replies: 7
    Last Post: 06-11-2006, 03:56 PM
  4. BIOS system and memory allocation problem
    By beely in forum Tech Board
    Replies: 9
    Last Post: 11-25-2003, 06:12 AM
  5. Number system base M, print numbers N digits wide...
    By biterman in forum C Programming
    Replies: 12
    Last Post: 11-19-2001, 03:31 AM

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