Why do I need this line of code?

This is a discussion on Why do I need this line of code? within the C Programming forums, part of the General Programming Boards category; this program works, but if I take out the while loop the program will end immediately after the last scanf(); ...

  1. #1
    Registered User
    Join Date
    Aug 2008
    Posts
    55

    Why do I need this line of code?

    this program works, but if I take out the while loop the program will end immediately after the last scanf(); I'm not sure why and would appreciate if someone could explain exactly why.
    Code:
    #include <stdio.h>
    
    #define STORE_NAME "Sierra Sporting Goods"
    
    
    int main(void){
        int intNumber, intType, intQuantity;
        double dblCost, dblPrice;
        
        printf("%s\n", STORE_NAME);
        printf("Enter the product number: ");
        scanf("%d", &intNumber);
        printf("\nEnter the product type: ");
        scanf("%d", &intType);
        printf("\nEnter the quantity: ");
        scanf("%d", &intQuantity);
        printf("\nEnter the cost: ");
        scanf("%lf", &dblCost);
        printf("\nEnter the price: ");
        scanf("%lf", &dblPrice);
    
    
        printf("\n\nThe product number is %d", intNumber);
        printf("\nThe product type is %d", intType);
        printf("\nThe quantity type is %d", intQuantity);
        printf("\nThe cost is %lf", dblCost);
        printf("\nThe price is %lf", dblPrice);
        
        printf("\nPress enter to continue\n");
        
        while(getchar() != '\n');              /*clear the buffer*/
        
        getchar();
      
       
     return 0;
        
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    The program will not end immediately after the scanf -- it will end at the line "return 0;" at the bottom. The key to remember is that scanf does not eat enter keys after input -- so when you get to "press enter to continue", you've already pressed enter before (when you typed in the price), so the program doesn't stop. The while loop specifically eats an enter key (the \n symbol) so that you "see" the results and the getchar() after it.

    Edit: Note that if you run your program the way nature intended -- at the command line -- you wouldn't see any problems.

  3. #3
    Registered User
    Join Date
    Aug 2008
    Posts
    55
    Quote Originally Posted by tabstop View Post
    The program will not end immediately after the scanf -- it will end at the line "return 0;" at the bottom. The key to remember is that scanf does not eat enter keys after input -- so when you get to "press enter to continue", you've already pressed enter before (when you typed in the price), so the program doesn't stop. The while loop specifically eats an enter key (the \n symbol) so that you "see" the results and the getchar() after it.

    Edit: Note that if you run your program the way nature intended -- at the command line -- you wouldn't see any problems.
    Thanks for response. So without the while loop, the getchar() would get the \n instead of pausing like I had intended. I get it.

    About your last sentence- Are you saying that if I ran it from the command line I wouldn't have to use the while loop?

  4. #4
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,189
    Thats correct.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    More specifically, while your program would end right away even on the command line, it wouldn't matter because you'd still be able to see the output, which I assume is your goal.

  6. #6
    Registered User
    Join Date
    Aug 2008
    Posts
    55
    Ohhh right. I see. Thanks.

  7. #7
    Registered User
    Join Date
    Aug 2008
    Posts
    46
    whatever you enter from keyboard will be stored in stdin buffer.. by using fflush(stdin) also u can perform the same thing that u did using while loop and getchar.. if fflush donot work then __fpurge(stdin) will definitely work.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,604
    Flushing stdin is undefined.
    __fpurge is some non-standard crap.
    Don't use either.
    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.

  9. #9
    Registered User
    Join Date
    Aug 2008
    Posts
    46
    what are the problems of using fflush() and __fpurge() functions. why cant we use them?
    Last edited by vapanchamukhi; 09-05-2008 at 04:34 AM.

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by vapanchamukhi View Post
    what are the delimitation of using fflush() and __fpurge() functions. why cant we use them?
    fflush is perfectly fine for output files, but for input files, fflush is officially undefined, so using it can cause ANYTHING to happen, it could "write 'Bang! You die!' on the console'", or it could crash your application, and it is DEFINITELY not in any way guaranteed to flush pending input.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by vapanchamukhi View Post
    what are the problems of using fflush() and __fpurge() functions. why cant we use them?
    __fpurge() is a non-standard function, meaning that the code is not portable. If I took that file with the __fpurge() call I wouldn't be able to compile it.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C code line, pointer declaration
    By Dedalus in forum C Programming
    Replies: 2
    Last Post: 06-10-2009, 04:34 AM
  2. help again with scrolling without wrapping
    By Dukefrukem in forum C Programming
    Replies: 8
    Last Post: 09-21-2007, 12:48 PM
  3. Can't figure out a line of code...
    By bamera in forum C++ Programming
    Replies: 1
    Last Post: 10-15-2005, 07:11 PM
  4. SSCANF help
    By mattz in forum C Programming
    Replies: 7
    Last Post: 12-10-2001, 03:53 PM

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