Thread: Why fflush(stdin) not working??

  1. #1
    Registered User
    Join Date
    Nov 2017
    Posts
    34

    Unhappy Why fflush(stdin) not working??

    [read this]
    When the program asks the user for a number if by some how means
    I pass a string , scanf() discards the string and passes the string to the next scanf() and prints out on the screen.

    So I tried to use fflush(stdin) so that the string is not passed to the next scanf(). But here it is as to what happens... It still doesn't work.

    Can someone please explain??

    Code:
    //Trying to figure out the meaning of 
    //<assert.h>
    
    //void assert(int exp);
    
    #include <assert.h>
    #include <stdio.h>
    #include <stdlib.h>
    
    
    void main()
    {
        char str[50];
        int x;
        
        {    
            printf("\nEnter a number\n");
            scanf("%d" , &x);
        
            /*Once you enter a number which is not             greater than
            10 , you get an error message*/
        
            assert(x>10);
            printf("%d" , x);
            fflush(stdin);
        }
        
        
        
        {
            printf("\nEnter a string\n");
            scanf("%s",str);
        
            /*If the string is not entered then
            assert function will throw an error*/
        
            assert(str!=NULL);
    
            printf("\nString entered is %s",str);
        }
    }

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,736
    Last edited by GReaper; 11-17-2017 at 06:29 AM.
    Devoted my life to programming...

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    Then also read why void main is wrong as well.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Nov 2017
    Posts
    34

    I hope you are joking? You are seriously not helping Sir!!

  5. #5
    Registered User
    Join Date
    Nov 2017
    Posts
    34
    Quote Originally Posted by Salem View Post
    Then also read why void main is wrong as well.
    Please read the specification for C99 and the latest gcc 5.4 , void main usage is still acceptable. Seriously I asked a question for fflush(stdin) and you saying void main?? If you can't help why don't you just don't reply!

  6. #6
    Banned
    Join Date
    Aug 2017
    Posts
    861
    Quote Originally Posted by ranadas View Post
    Please read the specification for C99 and the latest gcc 5.4 , void main usage is still acceptable. Seriously I asked a question for fflush(stdin) and you saying void main?? If you can't help why don't you just don't reply!
    I know that feeling.... !!!

  7. #7
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,736
    Quote Originally Posted by ranadas View Post
    Please read the specification for C99 and the latest gcc 5.4 , void main usage is still acceptable.
    Tolerated, not acceptable. The only forms standard C supports are "int main(void)" and "int main(int argc, char* argv[])". GCC is a compiler and it chooses to support the broadest range of features. It still supports pre-standard function declarations for Christ's sake!
    Devoted my life to programming...

  8. #8
    Registered User
    Join Date
    May 2010
    Posts
    4,631
    Please read the specification for C99 and the latest gcc 5.4 , void main usage is still acceptable.
    Okay from the current C standard:

    5.1.2.2.1 Program startup

    1 The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters:

    int main(void) { /* ... */ }

    or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):

    int main(int argc, char *argv[]) { /* ... */ }

    or equivalent;10) or in some other implementation-defined manner.

    ...


    5.1.2.2.3 Program termination

    1 If the return type of the main function is a type compatible with int, a return from the
    initial call to the main function is equivalent to calling the exit function with the value
    returned by the main function as its argument;11) reaching the } that terminates the
    main function returns a value of 0. If the return type is not compatible with int, the
    termination status returned to the host environment is unspecified.
    Okay where does any of the above say that void main() is acceptable? The use of void main() has never been acceptable in the C standard, unless you are working on an non-hosted system (no operating system). If your compiler is accepting void main() then you need to insure that you increase the compiler warning levels so that the compiler can warn you about more potential issues with your code.

    By the way gcc-5.4 is fairly outdated you should consider getting a much more current version of that compiler, perhaps something like gcc-7.2.

  9. #9
    Registered User
    Join Date
    Nov 2017
    Posts
    34
    Thanx Man! But still no answer to my original question! Now can we change the topic from void main() to fflush(stdin)?

  10. #10
    Registered User
    Join Date
    May 2015
    Posts
    90
    Quote Originally Posted by ranadas View Post
    Thanx Man! But still no answer to my original question! Now can we change the topic from void main() to fflush(stdin)?
    Have you read this yet?

  11. #11
    Registered User
    Join Date
    May 2010
    Posts
    4,631
    But still no answer to my original question!
    Did you even read the links provide in post #2? They should answer your question.

    The fflush() function is only defined by the C standard to work with output streams, not input streams.

  12. #12
    Registered User
    Join Date
    Nov 2017
    Posts
    34
    Yes Sir I have. I am a novice C programmer . So still can't understand how to implement those two concepts in my above program! If someone can do the courtesy to implement th above program without the problem ,I will highly appreciate that. I am a college drop out and just pursuing CS from Open University (Distance) so no one is there to guide me. Just this beautiful forum and my desire to code. Thanx!

  13. #13
    Banned
    Join Date
    Aug 2017
    Posts
    861
    How to use assertions in C

    you should read that. and while this is not answering your question, I do agree you should make it a habit to write your main using a return of int.

    when I run your program in my compiler I get this,
    Code:
    asserting_matters.c:11:6: warning: return type of 'main' is not 'int' [-Wmain]
    Not everyone runs C99 when this code will work in other than C99

    what is wrong with it besides why you think fflush(stdin) is not working nor is it really needed anyways?
    Code:
    //Trying to figure out the meaning of 
    //<assert.h>
     
    //void assert(int exp);
     
    #include <assert.h>
    #include <stdio.h>
    #include <stdlib.h>
     
     
    int main(void)
    {
        char str[50];
        int x;
         
        {    
            printf("\nEnter a number\n");
            scanf("%d" , &x);
         
            /*Once you enter a number which is not greater than
            10 , you get an error message*/
         
            assert(x>10);
            printf("%d" , x);
         //   fflush(stdin);
        }
         
         
         
        {
            printf("\nEnter a string\n");
            scanf("%s",str);
         
            /*If the string is not entered then
            assert function will throw an error*/
         
            assert(str!=NULL);
     
            printf("\nString entered is %s",str);
        }
        return 0;
    }
    your assert in the first one,
    Code:
    userx@slackwhere:~/bin
    $ ./asserting_matters
    
    Enter a number
    3
    asserting_matters: asserting_matters.c:23: main: Assertion `x>10' failed.
    Aborted
    the second one scanf is trying to do what, if you enter a number scanf does what, and when you do nothing but hit enter then scanf does what? Maybe needing to rethink your logic on that one.
    Last edited by userxbw; 11-20-2017 at 09:34 AM.

  14. #14
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    1. Don't use assert() for checking for user input errors.
    In a debug build assert kills the program. Your users won't be happy bunnies if every single typo they make results in losing all their work.
    In a release build, asserts is typically compiled out, so you get no checking at all.

    Assert is for checking programming errors during development, say to enforce your API.
    Code:
    // Calculate square root on number known to be positive.
    double mysqrt ( double x ) {
      assert(x>=0);
      // still need to check even after assert
      if ( x >= 0 ) {
      } else {
      }
    }
    > /*If the string is not entered then
    > assert function will throw an error*/
    > assert(str!=NULL);
    Not in this case it won't.
    str is an array, and is therefore guaranteed to never be NULL.
    You need to actually check the contents of the array to validate user input.

    Actually, what you need to do first is check the return result of scanf before doing anything else.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 11-14-2017, 06:48 PM
  2. how to clear bufferi fflush(stdin) is not working
    By sujitkashyap in forum C Programming
    Replies: 3
    Last Post: 06-22-2012, 07:55 AM
  3. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 12:13 PM
  4. fflush(stdin)
    By kewell in forum C Programming
    Replies: 3
    Last Post: 07-20-2002, 04:27 PM
  5. ascii 10 in the stdin after fflush(stdin)??
    By Kev2 in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 06-03-2002, 03:53 PM

Tags for this Thread