Input continuation loop

This is a discussion on Input continuation loop within the C Programming forums, part of the General Programming Boards category; Hello everbody, My first post. I'm working on a small project for school. Code: char answer = 'Y'; while (answer ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    3

    Input continuation loop

    Hello everbody, My first post.

    I'm working on a small project for school.

    Code:
    char answer = 'Y';
    
    while (answer == 'Y') {
                  //do the bulk of the work
    	printf("Do you want to continue? (Y/N)");
    	fflush(stdin);
    	scanf("%c", &answer);
    }
    This loop ran once then kicks me out after typing Y. My instructor told me to put the fflush(stdin). I did, and it worked great in my Visual Studio 2008. However, I'm to do this assignment on my school's unix account. I get the same problem again in which the loop runs, then kicks me out even after typing Y.
    What's wrong?

    I found this forum after searching for answer. I found a complicated answer involving the buffer (don't know anything abou these). Is writing this loop a nontrivial activity?

  2. #2
    Registered User
    Join Date
    Jan 2008
    Posts
    287
    Things to Avoid in C/C++ -- fflush(stdin), Part 2 - GIDNetwork

    Make sure to point this out to your instructor. They gave you terrible advice.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,256
    The problem is that your instructor gave you bad advice: fflush() is only defined for output and update (i.e., both input and output) streams, so fflush(stdin) results in undefined behaviour.

    One solution is to replace it with:
    Code:
    int c;
    while ((c = getchar()) != EOF && c != '\n');
    You may need to move the definition of c to the start of the while loop. The idea is to read and discard characters until the end of file is reached or the first newline character is reached (the newline would have been from the previous entering of input).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Mar 2009
    Posts
    3
    We routinely use fflush(stdin) followed by getchar() to prompt the user to type something before our little programs exit. :-(

  5. #5
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,269
    Quote Originally Posted by Sinosizer View Post
    We routinely use fflush(stdin) followed by getchar() to prompt the user to type something before our little programs exit. :-(
    Then you are relying on undefined behavior. IMHO, any decent C runtime should explode violently when attempting to flush an input buffer, since the very idea makes no sense. Unfortunately, some platforms will do what you want, leading you to believe your code is correct when it actually isn't.

    The problem is extra input in the buffer prior to the next newline character. The solution is to consume this input deliberately. It's not too hard:

    Code:
    void ignoreRestOfLine(FILE *fp)
    {
        int ch;
    
        while((ch = fgetc(fp)) != EOF && ch != '\n')
            ;
    }
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  6. #6
    Registered User
    Join Date
    Mar 2009
    Posts
    3
    I spoke to my instructor about this, and he suggested the code involving EOF and \n to be used on my Unix code. The fflush function call works somehow in Visual Studio 2008, even though it's not recommended.

    My question is, do I augment the EOF and \n portion with

    Code:
    && c == 'Y'

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Sinosizer View Post
    I spoke to my instructor about this, and he suggested the code involving EOF and \n to be used on my Unix code. The fflush function call works somehow in Visual Studio 2008, even though it's not recommended.

    My question is, do I augment the EOF and \n portion with

    Code:
    && c == 'Y'
    Speak to your instructor again. EOF and '\n' are perfectly valid in ALL platforms that support stdio.h - which if you exclude some of the smaller embedded plaforms is just about all computer platforms you will ever encounter (and of course, many programers never have the pleasure of working on a small embedded system that is designed for a specific task).

    --
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loop while waiting for input?
    By Terran in forum C++ Programming
    Replies: 6
    Last Post: 05-22-2008, 09:32 PM
  2. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 04:54 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 01:13 AM
  4. Input in timed loop
    By glo in forum Game Programming
    Replies: 12
    Last Post: 05-20-2006, 07:04 AM
  5. User input while loop is running
    By two31d in forum C++ Programming
    Replies: 7
    Last Post: 11-30-2005, 05:28 PM

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