Like Tree1Likes
  • 1 Post By AndrewHunter

scanf() doesn't execute in its 2nd time use

This is a discussion on scanf() doesn't execute in its 2nd time use within the C Programming forums, part of the General Programming Boards category; i am a beginner to c programming language... can anyone plz explain "why scanf() statement is not working after its ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    2

    Exclamation scanf() doesn't execute in its 2nd time use

    i am a beginner to c programming language...

    can anyone plz explain "why scanf() statement is not working after its use in the condition part of the while loop" as in the code is in attachment.
    Code:
    #include <stdio.h>
    
    int main()
    {
        int n[50], num;
        int i = 0, c = 0;
    
        printf("Enter numbers (At end enter any letter): ");
        while (scanf("%d", &n[i]))
        {
            i++;
        }
    
        printf("\n\tEnter a number: ");
        scanf("%d", &num);          //This statement doesn't execute
    
        printf("\n\tnum = %d", num);
    
        return 0;
    }
    Attached Files Attached Files
    • File Type: c test.c (342 Bytes, 38 views)

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,340
    Well if you entered
    1 2 3 a
    to make the first loop end, then the "a" will still be there, ready to fail on the next scanf as well.

    You need to remove the unwanted data from the input stream before trying to read another int.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    6
    I'm also a beginner to C, but from what I see you just need to put in a fflush. Here is a revised piece of code I made. I'm not entirely sure what your programs first line does or why a letter needs to be answered but here you go.

    Code:
    #include <stdio.h>
    
    int main()
    {
        int n[50], num;
        int i = 0, c = 0;
    
        printf("Enter numbers (At end enter any letter): ");
        while (scanf("%d", &n[i]))
        {
            i++;
        }
    
        printf("\n\tEnter a number: ");
        fflush(stdin);
        
        scanf("%d", &num);          //This statement doesn't execute
    
        printf("\n\tnum = %d", num);
        fflush(stdin);
        getchar();
    }

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    6
    Ok after reading a FAQ about it, I found away around it. Can someone please explain how this works and if it is "acceptable" to use it ?

    Code:
    #include <stdio.h>
    
    
     void myflush ( FILE *in )
    {
      int ch;
    
      do
        ch = fgetc ( in ); 
      while ( ch != EOF && ch != '\n' ); 
    
      clearerr ( in );
    }
    
    void mypause ( void ) 
    { 
      fflush ( stdout );
      getchar();
    }
    
    int main( void )
    {
        int n[50], num;
        int i = 0, c = 0;
    
        printf("Enter numbers (At end enter any letter): ");
        while (scanf("%d", &n[i]))
        {
            i++;
        }
    
        printf("\n\tEnter a number: ");
        myflush ( stdin );
        scanf("%d", &num);          //This statement doesn't execute
        
        printf("\n\tnum = %d", num);
        myflush ( stdin );
        mypause();
    }

  6. #6
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by mcpaddington View Post
    Ok after reading a FAQ about it, I found away around it. Can someone please explain how this works and if it is "acceptable" to use it ?
    This was recently discussed in this thread. Take a look through there and then ask if you have any additional questions.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  7. #7
    Registered User
    Join Date
    Aug 2011
    Posts
    6
    Quote Originally Posted by AndrewHunter View Post
    This was recently discussed in this thread. Take a look through there and then ask if you have any additional questions.
    Ok I get most of it now, just one question. What does 'EOF' stand for ?

    Edit: Nevermind I think I found it, does it mean End of File ?

  8. #8
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    End of File flag, it gets set when no more input can be read from a stream(which does include keyboard input). EOF - Wiki
    mcpaddington likes this.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  9. #9
    Registered User
    Join Date
    Aug 2011
    Posts
    6
    Quote Originally Posted by AndrewHunter View Post
    End of File flag, it gets set when no more input can be read from a stream(which does include keyboard input). EOF - Wiki
    Thank you for your help

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-18-2009, 12:04 PM
  2. Replies: 4
    Last Post: 04-26-2004, 06:31 PM
  3. Code before a line that Crashes doesn't execute
    By Diamonds in forum C++ Programming
    Replies: 2
    Last Post: 02-24-2003, 08:03 PM
  4. Replies: 2
    Last Post: 01-02-2002, 12:42 PM
  5. time a function takes to execute (up to the millisecond)
    By jszielenski in forum C Programming
    Replies: 4
    Last Post: 11-23-2001, 12:34 AM

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