Thread: Problem in reading inputs

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    113

    Problem in reading inputs

    Hi,

    The following code is not working since fgets read the newline character when enter is pressed......

    I have used fflush but this not working , can anybody let me know the correct way ?

    Code:
    
    #include<stdio.h>
    #include<string.h>
    
    struct student
    {
        int roll_no;
        char name[25];
        char city[25];
        char course[10];
    };
    
    int main()
    { 
         int number, i;
    
         struct student s[22];
    
         printf("\n STUDENT RECORD PROGRAME");
         printf("\nEnter The No. Of Records to Enter");
         scanf("&#37;d",&number);
         printf("\nEnter The Data Of students");
    
         for(i= 1;i<=number; i++) {
         printf("\nEnter Name:"); 
         fflush(stdin);
         fgets(s[i].name, sizeof(s[i].name), stdin);
         
         printf("\nEnter Roll No.:");
         scanf("%d",&s[i].roll_no);
    
         printf("\nEnter City:");
         fgets(s[i].city, sizeof(s[i].city), stdin);
    
         printf("\nEnter Course:");
         fgets(s[i].course, sizeof(s[i].course), stdin);
         }
    
         printf( "\n The Details Of Student Are:\n");
    
         for(i= 1;i<=number; i++)  {
    
         printf("\n Enter Name: %s",s[i].name);
         printf("\nRoll No.:%d",s[i].roll_no);
         printf("\n  City:%s",s[i].city);
         printf("\n Course:%s",s[i].course);
    
         }
    
    }
    Thanks

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Flushing the input buffer is undefined, so it never works reliably.
    You will need another way of flushing the input buffer. See link.
    Or you can always use fgets and convert the data with strtol or similar.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Let me guess that you are using Linux, where fflush(stdin) is not "working".

    You should not use fflush(stdin), because fflush(stdin) [or any other input file] is undefined by the ISO/ANSI C standard. It happens to work in certain compilers on certain OS's, but in other situations it may even crash your program. Instead, use a function to flush the input buffer after scanf(), e.g.
    Code:
    void clearinput(FILE *fin)
    {
       int ch;
       while((ch = fgetc(fin)) != '\n' && ch != EOF) ;
    }

    --
    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.

  4. #4
    Registered User
    Join Date
    Jan 2007
    Posts
    113
    Thanks matsp,

    It's working fine but now I have to press enter 2 times.... may be due to the
    fgetc(fin)
    in function .
    Is there any way that I can directly check for newline without using fgetc() ??

    Thanks

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, there isn't.
    You can avoid the issue altogether by using fgets all the time.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Bargi View Post
    Thanks matsp,

    It's working fine but now I have to press enter 2 times.... may be due to the in function .
    Is there any way that I can directly check for newline without using fgetc() ??

    Thanks
    As Elysia says, there's no way to check if there's anything in the input buffer or not.

    Perhaps that's because your fflush() in the original code is in the wrong place, which I presume is where you put your clearinput() call?

    --
    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.

  7. #7
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Don't know if this is correct (probably not?) or what you want:
    Code:
    FILE * chk_eol (FILE *fin, int *found)
    {
       char c;
       FILE *save_fin = fin;
       
       c = fgetc(fin);
       if (c == '\n')
          *found = 1;
       else
          *found = 0;
       
       return save_fin;
    }
    so you ll use fgetc() but you ll have the previous value of the pointer restored, so no harm done I suppose.
    and you call by
    fin = chk_eol(fin, &found);
    and found says if the next character is '\n' or not

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    chk_eol is non-standard.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by C_ntua View Post
    Don't know if this is correct (probably not?) or what you want:
    Code:
    FILE * chk_eol (FILE *fin, int *found)
    {
       char c;
       FILE *save_fin = fin;
       
       c = fgetc(fin);
       if (c == '\n')
          *found = 1;
       else
          *found = 0;
       
       return save_fin;
    }
    so you ll use fgetc() but you ll have the previous value of the pointer restored, so no harm done I suppose.
    and you call by
    fin = chk_eol(fin, &found);
    and found says if the next character is '\n' or not
    Copying the file-pointer won't retain where in the input buffer the current input is, which I presume is the purpose you are using "save_fin" for.

    It also won't work, as fgetc() will read from the input (and block until you press enter) if there isn't anything in the input buffer.

    So, it won't actually do what you want. If you KNOW that there is more input available in the input buffer, you could "peek" at it by calling fgetc() and then fungetc() - but that still fails in the same way as the code I posted if you don't have any pending data in the input buffer.

    --
    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.

  10. #10
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by Elysia View Post
    chk_eol is non-standard.
    what do you mean?

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by C_ntua View Post
    what do you mean?
    It means some compilers may have it, and some not. In other words, it's not part of the standard, it's a compiler extension, because I just checked MSDN, and there's no such function.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Elysia View Post
    It means some compilers may have it, and some not. In other words, it's not part of the standard, it's a compiler extension, because I just checked MSDN, and there's no such function.
    c_ntua posted code that implements chk_eol, not suggesting that it's a standard function at all. However, it is my opinion that the posted code doesn't at all work.

    --
    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.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I'm slow >_<
    But reading with fgetc is bad. If there is nothing in the input buffer, it will wait for input.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Quote Originally Posted by Elysia View Post
    It means some compilers may have it, and some not. In other words, it's not part of the standard, it's a compiler extension, because I just checked MSDN, and there's no such function.
    Well, it is my function if that is what you mean. Just made it up. That's why I said I don't know if it is correct.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by C_ntua View Post
    Well, it is my function if that is what you mean. Just made it up. That's why I said I don't know if it is correct.
    I missed that >_<
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem reading numbers in a txt file
    By nimamc in forum C Programming
    Replies: 3
    Last Post: 06-03-2009, 02:35 PM
  2. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  3. Slight problem with socket reading!!!
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 02-15-2006, 09:55 AM
  4. Replies: 6
    Last Post: 05-12-2005, 03:39 AM