I am trying to make a program that prompts you to choose 1 or 2. 1 being to make a file and 2 being to append a file. Whenever I run the code for if (opt == 1) with the if it skips the prompt that asks about what the file name will be. I hope I'm being clear and you can understand my problem.
EXPECTED OUTPUT-
Enter a file name: (input)
Enter file contents: (input)
ACTUAL OUTPUT-
Enter a file name: Enter file contents: (input)
#include <stdio.h>
int main(void) {
char fname[20];
char fcontents[250];
int opt;
printf("Make a file(1),Append a file(2)");
scanf("%d", &opt);
if (opt == 1)
{
printf("Enter a file name: ");
fgets(fname, 20, stdin);
printf("Enter file contents: ");
fgets(fcontents, 250, stdin);
FILE* fpointer = fopen(fname, "w");
fprintf(fpointer, "%s", fcontents);
fclose(fpointer);
printf("FILE: %s successfully created", fname);
}
else if (opt == 2)
{
printf("Enter a file name: ");
fgets(fname, 20, stdin);
printf("Enter file appends: ");
fgets(fcontents, 250, stdin);
FILE* fpointer = fopen(fname, "a");
fprintf(fpointer, "%s", fcontents);
fclose(fpointer);
printf("FILE: %s successfully created", fname);
}
}