0
#include<stdio.h>
   #include<string.h>
   typedef struct emp
   {
           char name[100];
           int age;
   }ee;
   void main()
   {
           struct emp p;
           FILE *ptr;
           int i;
           char ch[100];
           ptr=fopen("ddd.txt","w+");
           if(ptr==NULL)
           {
                   printf("\n not able to open the file");
           }
           for(i=0;i<3;i++)
           {
                   printf("Enter the name and the age");
                   fgets(p.name,100,stdin);
                   fflush(stdout);
                   scanf("%d",&p.age);
                   fprintf(ptr,"%s%d",p.name,p.age);
                   fflush(stdout);
           }
fclose(ptr);
   }

When i run the above code,on asking the second record, it does not ask for the age and the output "Enter the name and the age" comes directly. What is the reason for this and how can this be corrected??

2rd_7
  • 462
  • 1
  • 3
  • 15
Ankit batra
  • 31
  • 1
  • 7
  • 3
    Welcome to Stack Overflow. Please read the [About] page soon. This is a common problem — effectively a duplicate of another question. The `scanf("%d", &p.age)` leaves the newline after the number in the input buffer. The next `fgets()` reads up to that newline. You should check the return value from both `fgets()` and `scanf()` to be sure that they worked, and you'll need to skip up to and including the newline after the number. – Jonathan Leffler Jun 14 '16 at 06:44
  • " it does not asks for age". That part of the problem is because `printf` is line buffered by default. So it won't output until it sees a newline. Change all your `printf` strings to end with a newline rather than starting with a newline. – kaylum Jun 14 '16 at 06:49
  • Also note [What should `main()` return in C and C++](http://stackoverflow.com/questions/204476/). You check that `fopen()` worked, but your code goes on to use the unopened file stream which is not a good idea. Make sure you don't use the failed file stream. – Jonathan Leffler Jun 14 '16 at 06:49
  • i added the below statement fgets(garbage,10,stdin); after scanf("%d",&p.age); and it worked. Thanks, – Ankit batra Jun 14 '16 at 06:54
  • 1) Avoid using both `fgets(... stdin)` and `scanf()`. 2) Avoid using `scanf()`. Just use `fgets()`. – chux - Reinstate Monica Jun 14 '16 at 16:36
  • when coding, do not use `tabs`s for indenting. Each wordproccessor and editor has the tab stops/tab width set individually. Suggest using 4 spaces for each indent level as that is wide enough to be visible even with variable width fonts and still allows many indent levels across the page. – user3629249 Jun 14 '16 at 23:25
  • vertical spacing between headers, functions, code blocks (for, if, else, while, do...while, switch, case, default) makes the code much much easier to read and understand. – user3629249 Jun 14 '16 at 23:26
  • although visual studio will let you get away with it, the correct signature of `main()` is always either: `int main( void )` or `int main( int argc, char *argv[] )` – user3629249 Jun 14 '16 at 23:30
  • it is best to separate a structure definition from a `typedef` to rename that structure – user3629249 Jun 14 '16 at 23:31
  • variable names should indicate `content` or `usage` (or better, both) – user3629249 Jun 14 '16 at 23:31
  • there are some 'magic' numbers in the posted code. 'magic' numbers are numbers with no basis. I.E. the 100. 'magic' numbers make the code much more difficult to understand, debug, maintain. Suggest using an `enum` or `#define`s to give those 'magic' numbers meaningful names, then use those meaningful names throughout the code. – user3629249 Jun 14 '16 at 23:34
  • when outputting a message about an error, the message should be oriented to `stderr`, when it is about a returned value from a system function, use `perror()` so the reason the system thinks the error occurred is also output. For other error messages use: `fprintf( stderr, "formatString", parameters );` – user3629249 Jun 14 '16 at 23:38
  • when reading back the data in the `ddd.txt` file, the code will have some extreme difficultly because there is no 'marker' to separate one record from the next. Strongly suggest changing the format string from: `"%s%d"` to `"%s%d\n"` – user3629249 Jun 14 '16 at 23:41
  • the first call in each execution of the loop, to the function: `fflush(stdout);` does nothing as following an output by an input (printf()...scanf()) will automatically force the stdout buffer to flush – user3629249 Jun 14 '16 at 23:43
  • the call to `scanf()` will leave the newline sequence in stdin. The next call to `fgets()` will only see the newline. Suggest modifying the call to `scanf()` to something similar to: `if( 1 != scanf( "%d", age) ) { // handle error } int ch; while( (ch = getchar()) != '\n' && EOF != ch );` so any trash left in stdin is consumed AND checking for an error returned from `scanf()` – user3629249 Jun 14 '16 at 23:49

0 Answers0