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.