Results 1 to 5 of 5

Thread: fflush (stdin): Another reason I dislike MS

  1. #1
    Join Date
    Nov 2004
    Location
    Kuala Lumpur, Malaysia
    Beans
    885
    Distro
    Xubuntu 14.04 Trusty Tahr

    fflush (stdin): Another reason I dislike MS

    ATTN: This is for newbies to programming. I guess the pros must have known this all along.

    I'm taking a subject called Programming Principles and Techniques. Basically it's an intro to programing class using C languange. The IDE installed at the computer lab and classroom is MS Visual C++. And after each lecture, I went back home at tried the lesson on Ubuntu using Anjuta. All went fine, until the lecturer teaches about fflush (stdin). Here's what I found out.

    This is the problem that my lecturer ask us to solve:
    Code:
    #include <stdio.h>
    
    int main()
    {
        int n;
        char string[80];
        for (n=0; n<5; n++)
            {
                printf ("Enter some words: ");
                scanf ("%s", string);
                printf ("The first word you entered is : %s\n", string);
            }
        return 0;
    }
    This is the suggested solution:
    Code:
    #include <stdio.h>
    
    int main()
    {
        int n;
        char string[80];
        for (n=0; n<5; n++)
            {
                printf ("Enter some words: ");
                scanf ("%s", string);
                printf ("The first word you entered is : %s\n", string);
                fflush (stdin);
            }
        return 0;
    }
    To my surprise, it only works in Windows. In Linux, it ran the same with and without fflush (stdin). After searching the net, I have found that:

    According to Things to Avoid in C/C++ -- fflush(stdin), it is best to avoid using scanf. But that's the only input method my lecturer has taught so far, and I wish not to confuse myself (and my classmates that I'm gonna have to share this with later) with other input methods.

    Further search on the net lead me to these solution that works both in GNU/Linux and Windows:
    Code:
    #include <stdio.h>
    
    int main()
    {
        int n;
        char string[80];
        for (n=0; n<5; n++)
            {
                printf ("Enter some words: ");
                scanf ("%s", string);
                printf ("The first word you entered is : %s\n", string);
                while (getchar() != '\n') continue;
            }
        return 0;
    }
    Code:
    #include <stdio.h>
    
    void clear_kb(void);
    
    int main()
    {
        int n;
        char string[80];
        for (n=0; n<5; n++)
            {
                printf ("Enter some words: ");
                scanf ("%s", string);
                printf ("The first word you entered is : %s\n", string);
                clear_kb();
            }
        return 0;
    }
    
    void clear_kb(void)
    {
        char junk[255];
        fgets (junk,255,stdin);
    }
    Why can't MS just use the standard (ANSI) C???
    Last edited by Buffalo Soldier; October 29th, 2005 at 03:54 PM.

  2. #2
    Join Date
    Aug 2005
    Beans
    169

    Re: fflush (stdin): Another reason I dislike MS

    Because they don't need to? MS are in the business of supporting Windows, not Linux. You may find that they have a compatibility mode, like gcc does. I know that Windows has a POSIX layer, for example.

  3. #3
    Join Date
    Jan 2013
    Beans
    1

    Re: fflush (stdin): Another reason I dislike MS

    thanks man

  4. #4
    Join Date
    Nov 2005
    Location
    Bordeaux, France
    Beans
    11,292
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: fflush (stdin): Another reason I dislike MS

    Just to set things straight:

    1) The fact that a vendor supports some things that are non-standard in itself is not an argument against that vendor, because all vendors do. Yes, Linux included.

    2) The C99 standard (and probably C89 as well) leaves unspecified the behaviour of fflush() on anything other than an output stream or a null pointer. Thus an implementation is free to do whatever it wants when fflush() is given any other kind of argument, and Microsoft is not breaking the standard by making its implementation behave differently from Linux's.

    3) It is not Microsoft who wrote the lecture and suggested to use fflush(stdin), so if anything, the blame should be on the instructor. And even then, if the course specifically targets Windows, the blame should be put on the student for enrolling in it and expecting something else.
    Last edited by Bachstelze; January 21st, 2013 at 06:09 PM.
    「明後日の夕方には帰ってるからね。」


  5. #5
    Join Date
    Jun 2010
    Beans
    92

    Re: fflush (stdin): Another reason I dislike MS

    Another reason I dislike MS

    Rather than the exhibiting the popular knee-jerk reaction when confronted with something like this, you should view it as an opportunity. This is the type of knowledge that separates the good programmers from the average, the in-demand professionals from the hobbyists.

    Intel's complicated segmented x86 architecture is another such opportunity. Many complain and whine about it, while others have successful careers because of it.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •