0

I am using fprintf to store the values entered by user in a file . Code is not working as expected (First thing that after prompting "Do you want to continue? Y or N : " program is automatically taking some value and continues the loop . Secondly it is not storing the value input by user into intended file . Update: I am able to let program prompt use whether to continue loop or not by putting space in scanf before %c but still file admin_db.txt is not getting populated properly.

#include<stdio.h>

typedef struct {

char str[30];
int a[10];

}UNIX;

int main()

{

UNIX admin;
char ch;
FILE *fp;

fp=fopen("admin_db.txt","w+");

while(1)

{
printf("\nEnter the name of admin :  ");
 scanf("%s",&admin.str);

 printf("\nEnter the age of admin : ");
  scanf("%d",&admin.a);

  fprintf(fp,"%s%t%d",admin.str,admin.a);

  printf("\nDo you want to continue ? Y or N :");
   scanf("%c",&ch);

   if(ch=='n' || ch=='N')
     break;

}

fclose(fp);

}

Output:

Enter the name of admin :  Akhil

Enter the age of admin : 23

Do you want to continue ? Y or N :        //Automatically taking some value                                        //                                        other than n or N and continuing loop.
Enter the name of admin :  sukhi

Enter the age of admin : 30

Do you want to continue ? Y or N :
Enter the name of admin :
Enter the age of admin : ^C 

Also , sizeof file admin_db.txt is of 0 bytes.

theartist33
  • 470
  • 1
  • 6
  • 13
  • 2
    `%t` is not a valid [`fprintf`](http://en.cppreference.com/w/c/io/fprintf) specifier (that I know of), did you mean `\t`? – Kninnug Aug 19 '15 at 18:40
  • 3
    Many times a duplicate. The `scanf()` leaves the newline in the input buffer; the `scanf("%c", …)` grabs the newline. Put a space in front of the `%c`: scanf(" %c", &ch);` to skip the white space. Check the return value from `scanf()`, every time. If it isn't the number of values you expected (1 in your code), there's a problem. – Jonathan Leffler Aug 19 '15 at 18:41
  • @Kninnug you are right I meant \t , anyway situation stands same. – theartist33 Aug 19 '15 at 18:44
  • Wrong (or not best) duplicate chosen. Suggest [SO 217074](http://stackoverflow.com/q/217074) instead. – Jonathan Leffler Aug 19 '15 at 18:44
  • @JonathanLeffler I put space before %c in scanf now and it is now prompting user to enter a value and waits for input but even if I enter y (yes want to continue) program is exiting . – theartist33 Aug 19 '15 at 18:49
  • Did you print out the value read into `ch`? The first and simplest step in debugging is to check that the program sees what you think it sees. – Jonathan Leffler Aug 19 '15 at 18:52
  • in `scanf`, `&admin.str` should be just `admin.str` since `%s` expects a `char *` similarly in `fprintf`, '%d' expects an `int` which `admin.a` is not..... there are more type errors, compile with warnings enabled. – ryanpattison Aug 19 '15 at 18:57
  • yes , checked . Enter the name of admin : akhil Enter the age of admin : 23 Do you want to continue ? Y or N :y value you entered in ch is y – theartist33 Aug 19 '15 at 18:58

1 Answers1

0

You have to flush stdin before taking additional input. The following is a portable method:

int ch;
while (((ch = getchar()) != '\n') && (ch != EOF));
Fiddling Bits
  • 8,712
  • 3
  • 28
  • 46
Raghu Srikanth Reddy
  • 2,703
  • 33
  • 29
  • 42
  • For this `ch` must be an `int` (OP has `ch` declared as `char`). – Kninnug Aug 19 '15 at 18:40
  • @Kninnug: Thanks. Updated my answer. :) – Raghu Srikanth Reddy Aug 19 '15 at 18:43
  • not working , still exiting even if I enter Y or y. Also before entering into loop compiler is waiting for user to enter value in order to execute loop , if you press enter it exits (because of your code logic '\n' ) – theartist33 Aug 19 '15 at 18:54
  • Below is the Answer : #include typedef struct { char str[40]; int a[5] ; }UNIX; int main() { FILE *fp; char ch='y'; UNIX admin; fp=fopen("/home/akhils/file_dir/admin_db.data","a"); int i=0; while(1) { printf("Enter the name of admin : "); scanf("%s[^\n]",admin.str); printf("\nEnter age of admin : "); scanf("%d[^\n]",admin.a); printf("\nDo you want to continue ? Y or N : " ) ; scanf(" %c",&ch); fprintf(fp,"%s \t %d\n",admin.str,admin.a[0]); i++; if(ch=='N' || ch=='n') break; } fclose(fp); } – theartist33 Aug 21 '15 at 18:27