Thread: scanf problems

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    30

    scanf problems

    I'm trying to read a bunch of ints in one line until EOF, but the program doesnt end, it scans again.
    Code:
    printf("input: ");
            while (scanf("%d", &temp)!= EOF)
            {
    
                    printf("Values: %d", temp);
    
            }
    why?

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    There is nothing like EOF char. There is no any special character to represent end of file. It is used to indicate a end-of-file in a file and at the end of file. Use fgetc to red char. Perhaps you can break the look as soon as the user enter a return key. Like

    Code:
    int c;
    while( ( c = fgetc( stdin ) ) != '\n' ) { ... }
    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    30
    Well, that works if I was reading 1 char at a time.


    I'm trying to read in a bunch of ints,


    Example input:
    1 4 9 52 63 29

    And what I'm trying to get is
    Largest value: 63

    I have everything else I believe and it's just this input problem I am having.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    How are you terminating your input, and on what operating system are you running?

  5. #5
    Registered User
    Join Date
    May 2011
    Posts
    30
    for now, I'm terminating it by pressing ctrl+c. I'm using linux via putty

    #include <stdio.h>

    int static large;
    int static count;
    int largest(int *check)
    {
    large = check[0];
    int i = 0;
    for (i=0;i<count;i++)
    {
    if (large < check[i])
    {
    large = check[i];
    }
    }
    return large;
    }

    int main()
    {
    int list[20];
    int temp;
    int ans;
    printf("input: ");
    while ((scanf("%d", &temp))!= '\n')
    {
    list[count] = temp;

    count++;
    }

    ans = largest(list);
    printf("Largest value: %d\n", ans);
    return 0;
    }

    This is what I currently have. My biggest problem is the starting point.

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Ctrl-C will stop your program cold, unless the point is to trap signals. Otherwise you can read in a string, and then read out of the string using strtol (look that up in your manual, it is probably exactly what you want).

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Well your best bet would be to use read the input as a string convert that to int or long or whatever it is. Or your other options are just use for loop along the size of your array (20). Which could be saving as you don’t have boundary check with your current code. Which is very important to check you don’t cross your array boundary.

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  8. #8
    Registered User
    Join Date
    May 2011
    Posts
    30
    I'd need to try it without strtol.

    ssharish, but if I were to enter less than 20 inputs, wouldn't it loop until I have 20 inputs?

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by tabstop View Post
    Ctrl-C will stop your program cold, unless the point is to trap signals. Otherwise you can read in a string, and then read out of the string using strtol (look that up in your manual, it is probably exactly what you want).
    More specifically, CTRL C is not EOF. It's typically CTRL D or CTRL Z, depending on your OS.

    See also: End-of-file - Wikipedia, the free encyclopedia


    Quzah.
    Hope is the first step on the road to disappointment.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by bungkai View Post
    I'd need to try it without strtol.

    ssharish, but if I were to enter less than 20 inputs, wouldn't it loop until I have 20 inputs?
    Look at the loop:
    Code:
    while ((scanf("%d", &temp))!= '\n')
    Granted this code is broken, since scanf will never return '\n' -- you should change that to == 1 at the end.

  11. #11
    Registered User
    Join Date
    May 2011
    Posts
    30
    Same result with the == 1

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Same result as what? You need to type something that the computer will not recognize as a number -- any letter will do. If you only intend to just read one line of input, then this loop isn't for you.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by bungkai View Post
    for now, I'm terminating it by pressing ctrl+c. I'm using linux via putty
    Code:
    #include <stdio.h>
    
    int static large;
    int static count;
    int largest(int *check)
    {
            large = check[0];
            int i = 0;
            for (i=0;i<count;i++)
            {
                    if (large < check[i])
                    {
                            large = check[i];
                    }
            }
            return large;
    }
    
    int main()
    {
            int list[20];
            int temp;
            int ans;
            printf("input: ");
            while ((scanf("%d", &temp))!= '\n')
            {
                    list[count] = temp;
    
                    count++;
            }
    
            ans = largest(list);
            printf("Largest value: %d\n", ans);
            return 0;
    }
    This is what I currently have. My biggest problem is the starting point.
    You need to look up the scanf() function in your library documentation... It does not return what you seem to think it does.
    Your while loop needs to exit on count... not on scanf().

  14. #14
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Well this is getting no where, this is something which i propose. If this dosn't suite your requirnment, i would strongly suggest to give up with this idea and use char to terminate the loop.

    Code:
    void flush_buffer( void );
    
    int main ( void )
    {
        char intV[10];
        
        while( scanf( "%[0-9]", intV ) > 0 )
        {
             printf("%s\n", intV );
             /* convert read string to int using strtoul */
             flush_buffer();  
        }
        return 0;
    }
    
    void flush_buffer( void )
    {
         int ch;
         
         while( ( ch = getchar() ) != '\n' && ch != EOF );
    }
    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
    #define MAX 10
    
    void flush_buffer( void );
    
    int main ( void )
    {   int i = 0, count = MAX;
        int val[MAX];
        
        while( i++ < count)
          { if (scanf( " %d", &val) == 0)
              break;  
             printf"%d\n", val );
             flush_buffer();  
        }
        return 0;
    }
    
    void flush_buffer( void )
    {
         int ch;
         while( ( ch = getchar() ) != '\n' && ch != EOF );
    }
    Last edited by CommonTater; 06-16-2011 at 07:35 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems with scanf
    By alexkim123 in forum Tech Board
    Replies: 7
    Last Post: 07-08-2010, 05:19 PM
  2. scanf and integer problems
    By jericjones45 in forum C Programming
    Replies: 5
    Last Post: 02-27-2010, 07:12 AM
  3. problems with if() or scanf() i think...
    By nostrum in forum C Programming
    Replies: 5
    Last Post: 07-31-2004, 11:45 AM
  4. Problems with scanf
    By kinghajj in forum C Programming
    Replies: 2
    Last Post: 07-13-2003, 08:53 PM
  5. scanf problems
    By Unregistered in forum C Programming
    Replies: 4
    Last Post: 03-24-2002, 05:33 PM