Thread: Input continuation loop

  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
    290
    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
    28,413
    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).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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, 08:32 PM
  2. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. Input in timed loop
    By glo in forum Game Programming
    Replies: 12
    Last Post: 05-20-2006, 06: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