tags:

views:

526

answers:

5
+3  A: 

fflush(stdin) is undefined behavior. Instead, make scanf "eat" the newline:

scanf("%s %d %f\n", e.name, &e.age, &e.bs);

Everyone else makes a good point about scanf being a bad choice. Instead, you should use fgets and sscanf:

const unsigned int BUF_SIZE = 1024;
char buf[BUF_SIZE];
fgets(buf, BUF_SIZE, stdin);
sscanf(buf, "%s %d %f", e.name, &e.age, &e.bs);
Andrew Keeton
I tried putting \n.But even after entering name,age,salary for the first record,the prompt message for entering Y or N for next record is not getting displayed.
deovrat singh
Did you drop the fflush()? Can you post your updated code?
Rob Jones
Adding the '\n' will end up causing scanf to read until it encounters the end of the file (use Control+D in *nix or Control+Z in Windows).
Whisty
I have posted the updated code,it is not working even after incorporating sscanf and fgets.
deovrat singh
Ok, see my updated answer below.
Rob Jones
+1  A: 

It's not a good practice to use fflush( stdin ) as it has undefined behavior. Generally, functions like scanf() leaves trailing newlines in stdin. So, it is better to use functions that are "cleaner" than scanf(). You can replace your scanf() with a combination of fgets() and sscanf() and you can do away with fflush( stdin ).

swatkat
How to do that?? Can u please give me an example,here in this case.Thanks :)
deovrat singh
printf( "\nEnter name,age and basic salary" );char szTempBuf[1024];fgets( szTempBuf, 1024, stdin );sscanf( szTempBuf, "%s %d %f", e.name,
swatkat
it is still not working properly,check out the updated code and its output
deovrat singh
+1  A: 

Update: You need to add another getchar() at the end of your loop to consume the '\n' that follows the Y/N. I don't think this is the best way to go, but it will make your code work as it stands now.

while(another=='y') {
    printf("\nEnter name,age and basic salary : ");
    fgets(buf, BUF_SIZE, stdin);
    sscanf(buf, "%s %d %f", e.name, &e.age, &e.bs);
    fwrite(&e,sizeof(e),1,fp);
    printf("Add another record (Y/N)");
    another=getchar();
    getchar();
}

I would suggest reading the data you want to parse (up to and including the '\n') into a buffer and then parse it out using sscanf(). This way you consume the newline and you can perform other sanity checks on the data.

Rob Jones
fflush() is for standard I/O streams - what do you mean? You don't use it for input files - it is intended to flush buffered output. But that isn't what you said. And it is fine - even sensible - to use fflush(stdout); especially in debugging modes. (It's probably more sensible to use fflush(0), but that is a slightly different issue again.)
Jonathan Leffler
Thanks Rob :).Gosh!never knew that it would be this difficult to get a input in C.Can u please suggest me some good website to refer for parsing input.
deovrat singh
I don't know about a website for parsing input, per se. Your best bet is probably searching this site and posting additional questions. One thing to do is to write your parsing routine and then try to break it with malformed input. You'll learn a lot.
Rob Jones
+1  A: 

Use this instead of getchar():

   char another[BUF_SIZE] = "y";
   while( 'y' == another[0] )
   {
        printf( "\nEnter name,age and basic salary : " );
        fgets( buf, BUF_SIZE, stdin );
        sscanf( buf, "%s %d %f", e.name, &e.age, &e.bs );
        fwrite( &e, sizeof(e) , 1, fp );
        printf( "Add another record (Y/N)" );
        fgets( another, BUF_SIZE, stdin );
    }
Note that you'll have to replace "`unsigned int const BUF_SIZE = 1024;`" with "`#define BUF_SIZE 1024`" for this to work in C.
Whisty
+1  A: 

I would recommend the fgets()+sscanf() approach that a lot of other people have suggested. You could also use scanf("%*c"); before the call to getchar(). That will essentially eat a character.

D.Shawley
Thanks, the `scanf("%*c");` worked a treat for a tutorial I was trying to follow on Windows. But then I thought, hang on what if I try a `getchar()` in its place, i.e. _before_ the `c = getchar();` I am interested in. And that works as well, which is odd as the top answer to this question (and other similar questions) have the extra getchar(); at the end.
i5m