Hear me out because I'm a noob and I know this is a potential duplicate but I've browsed so much pages and I still can't figure this out. I'm trying to take a name but for some reason it's not allowing me to input data into first fgets.
void getModuleDetails(struct moduleInfo* moduleInfo)
{
FILE *fp;
fp = fopen("moduleInfo.txt", "a+");
if(fp == NULL)
exit(1);
char name[100];
char code[100];
int id;
int ca;
int exam;
while((ch =getchar())!= '\n' && ch != EOF)
{
printf("Module Name:");
if(fgets(name, 100, stdin) != NULL)
{
fprintf(fp,"%s", name);
}
}
printf("Module Code:");
if(fgets(code, 100, stdin) != NULL)
fprintf(fp,"%s", code);
printf("Module ID:");
if(scanf("%d", &id) != NULL)
fprintf(fp,"%d\n", id);
printf("CA%:");
if(scanf("%d", &ca) != NULL)
fprintf(fp,"%d\n", ca);
printf("Exam%:");
if(scanf("%d", &exam) != NULL)
fprintf(fp,"%d\n", exam);
fclose(fp);
}
As you can see I have removed all '\n's that I had after my printf's, added getchar to handle a \n. I also know I shouldn't be mixing fgets with scanf but it's what our lecturer wants. If anyone could point out where I'm going wrong, it'd be extremely appreciated.