Thread: Another do-while enigma..for me

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    9

    Another do-while enigma..for me

    hey guys, i'm rather new to c prog so any advice will be appreciated.
    this prob may be rather elmentary but i'm still having probs, pls help!
    ok, so the prob goes like this:
    Write a loop to repeatedly perform a computation to add 2 integers.
    The users will be prompted with "do u wanna con't?"and if he enters a y or Y, the prog will start again.if the user enters anything else, the prog terminates..

    ok, my version goes like this:

    #include <stdio.h>
    main ()
    {
    char ans;

    do
    {
    int num1, num2, num3;
    printf ("enter 2 integers");
    scanf ("%d%d", &num1, &num2);

    num3 = num1 +num2;
    printf ("The ans is %d", num3");

    printf ("wanna play again?");
    scanf ("%c", &ans);
    }
    while (ans == 'y' || ans == 'Y');

    return 0;
    }

    it does not work tho=(
    it computes the answer perfectly, but it does not seem to let the user key in a y/n answer...it just prints the "wanna play again" and then the whole prog terminates...
    Help please!!why does this happen?

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    65
    add fflush(stdin); after the scanfs.

    #include <stdio.h>
    main ()
    {
    char ans;

    do
    {
    int num1, num2, num3;
    printf ("enter 2 integers");
    scanf ("%d%d", &num1, &num2);
    fflush(stdin); <-------------------

    num3 = num1 +num2;
    printf ("The ans is %d", num3);

    printf ("wanna play again?");
    scanf ("%c", &ans);
    fflush(stdin); <--------------------

    }
    while (ans == 'y' || ans == 'Y');

    return 0;
    }
    The experimenter who does not know what he is looking for will not understand what he finds.
    - Claude Bernard

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > add fflush(stdin); after the scanfs.
    No!
    fflush on input streams is undefined - see http://www.eskimo.com/~scs/C-faq/q11.33.html

    If you really want to tidy up the input stream, then do this
    Code:
    while ( getchar() != '\n' );  /* read input until newline */
    Like so
    Code:
    #include <stdio.h> 
    main () 
    { 
    char ans; 
    
    do 
    { 
    int num1, num2, num3; 
    printf ("enter 2 integers"); 
    scanf ("%d%d", &num1, &num2); 
    
    num3 = num1 +num2; 
    printf ("The ans is %d", num3); 
    
    printf ("wanna play again?"); 
    while ( getchar() != '\n' );  /* read input until newline */
    scanf ("%c", &ans); 
    
    } 
    while (ans == 'y' || ans == 'Y'); 
    
    return 0; 
    }
    The whole problem goes away if you use fgets and sscanf, as discussed many times before...

  4. #4
    Registered User
    Join Date
    Nov 2001
    Posts
    65
    I am not sure whether fflush(stdin) is defined or undefined but I have used this hundreds of times in various compilers (with exception of gcc) and it is working. Is being undefined (if really)more important than being working?
    The experimenter who does not know what he is looking for will not understand what he finds.
    - Claude Bernard

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    9
    hey salem!!!
    thanks it works...hey but care to explain wat the get char() command does?

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    26
    Is being undefined (if really)more important than being working?
    assuming fflush works everytime is bad. Even if it only fails once it's one time to many, because your data is corrupted.
    Use the while(getchar() != '\n') approach.
    Or as Salem suggested if you use fgets and sscanf you don't have a problem at all.
    Code:
    char temp[10];
    int num1, num2;
    
    printf("\nEnter 2 numbers ");
    fgets(temp, sizeof(temp), stdin);
    sscanf(temp, "%d %d", &num1, &num2);

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > I am not sure whether fflush(stdin) is defined or undefined
    Read the standard then
    http://anubis.dkuug.dk/JTC1/SC22/WG14/www/docs/n869/

    Which says
    -----
    7.19.5.2 The fflush function
    Synopsis
    1 #include <stdio.h>
    int fflush(FILE *stream);
    Description
    2 If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.
    3 If stream is a null pointer, the fflush function performs this flushing action on all streams for which the behavior is defined above.
    Returns
    4 The fflush function sets the error indicator for the stream and returns EOF if a write error occurs, otherwise it returns zero.
    -----

    > but I have used this hundreds of times in various compilers (with exception of gcc) and it is working.
    Maybe so, but undefined does include apparently doing what you expect. It also included doing nothing and reformatting your hard disk.
    The point is, not what appears to work for you, given your current circumstances, its a matter of making your programs as correct as possible, so that what you learn out of habit will mean that programs you write in future will work just as well (no matter which compiler you end up working with), and you don't have to go round learning (and un-learning) over and over again lots of silly compiler specific tricks.

    > Is being undefined (if really)more important than being working?
    The most often example of this is using an uninitialised pointer - everyone knows its wrong, but it still happens, and programs sometimes quite happily work despite this. Then all of a sudden, some minor edit in a totally unrelated part of the code causes all hell to break loose, and its all memory corruption and segfaults.

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    9
    hey i agree that we shld get the basics right...learn a standard code..
    hey but i still dun uderstand wat the getchar() != '\n' does...
    anyone care to explain?

  9. #9
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    getchar() gets a single character from stdin. putting it inside a while loop like salem has done effectively emptys the input stream to tidy up the crap that can be left behind by scanf()

    >> /* read input until newline */

    did you not understand that?!
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  10. #10
    Registered User
    Join Date
    Jan 2002
    Posts
    9
    ha, so sorry, i'm kinda new to C so sorry....
    ok, now i kinda get it...thanks a lot guys!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enigma execpvp
    By simpatico_qa in forum C Programming
    Replies: 10
    Last Post: 05-11-2009, 03:04 PM
  2. Win32 Battleship game enigma?
    By Finchie_88 in forum Windows Programming
    Replies: 7
    Last Post: 03-20-2005, 07:17 AM
  3. a new enigma
    By caroundw5h in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 05-17-2004, 07:14 PM
  4. Help About The ENIGMA Algoritm
    By JohnnyAtomic in forum C Programming
    Replies: 4
    Last Post: 02-22-2002, 10:05 AM