Right now, I'm in the middle of a project and wrote a code for it. When i run the code below on its own it runs fine
#include <stdio.h>
#define DIM 512
#define MAXCHAR 30
Code:
int main()
{
FILE *myfile = NULL;
char bknum[DIM], customer[MAXCHAR], movie[MAXCHAR], date[DIM], time[DIM];
char guest[DIM], house[DIM], ticket[MAXCHAR], fee[DIM];
myfile = fopen("project.txt", "a+");
if (myfile == NULL)
{
printf("File open failed!! Exiting!!\n");
return 1;
}
printf("Please enter Booking Number:\n");
fgets(bknum, DIM, stdin);
printf("Please enter Customer Name:\n");
fgets(customer, MAXCHAR, stdin);
printf("Please enter Movie Name:\n");
fgets(movie, MAXCHAR, stdin);
printf("Please enter the Movie Date:\n");
fgets(date, DIM, stdin);
printf("Please enter Movie Time\n");
fgets(time, DIM, stdin);
printf("Please enter Number of Guests:\n");
fgets(guest, DIM, stdin);
printf("Please enter House Number:\n");
fgets(house, DIM, stdin);
printf("Please enter Ticket Types:\n");
fgets(ticket, MAXCHAR, stdin);
printf("Please enter Total Fee:\n");
fgets(fee, DIM, stdin);
fprintf(myfile, "\n%s", bknum);
fprintf(myfile, "%s", customer);
fprintf(myfile, "%s", movie);
fprintf(myfile, "%s", date);
fprintf(myfile, "%s", time);
fprintf(myfile, "%s", guest);
fprintf(myfile, "%s", house);
fprintf(myfile, "%s", ticket);
fprintf(myfile, "%s", fee);
fclose (myfile);
return 0;
}
But when I put it back on its back bone (main, where its supposed to be), it has some bugs. The main code is below,
Code:
#include <stdio.h>
#define DIM 512
#define MAXCHAR 30
int main()
{
void op1();
int option;
printf(" *** Welcome to HK Grand SPACE Movie Ticketing Management System 2017 ***\n");
printf(" *** This system is developed by CCIT4020 Class No.CL-?? Group No.?? ***\n");
printf("\n--<Basic Functions>--\n");
printf("1. Add New Movie Ticketing Record(s) \n");
printf("2. Display ALL Movie Ticketing Records \n");
printf("3. Modify Movie Ticketing Record(s) \n");
printf("4. Search Movie Ticketing Record(s) \n");
printf("5. Delete Movie Ticketing Record(s) \n");
printf("What is your Option <1-5>?");
scanf("%d",&option);
switch (option)
{
case 1:
op1();
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
}
}
void op1()
{
FILE *myfile = NULL;
char bknum[DIM], customer[MAXCHAR], movie[MAXCHAR], date[DIM], time[DIM];
char guest[DIM], house[DIM], ticket[MAXCHAR], fee[DIM];
myfile = fopen("project.txt", "a+");
if (myfile == NULL)
{
printf("File open failed!! Exiting!!\n");
}
printf("Please enter Booking Number:\n");
fgets(bknum, DIM, stdin);
printf("Please enter Customer Name:\n");
fgets(customer, MAXCHAR, stdin);
printf("Please enter Movie Name:\n");
fgets(movie, MAXCHAR, stdin);
printf("Please enter the Movie Date:\n");
fgets(date, DIM, stdin);
printf("Please enter Movie Time\n");
fgets(time, DIM, stdin);
printf("Please enter Number of Guests:\n");
fgets(guest, DIM, stdin);
printf("Please enter House Number:\n");
fgets(house, DIM, stdin);
printf("Please enter Ticket Types:\n");
fgets(ticket, MAXCHAR, stdin);
printf("Please enter Total Fee:\n");
fgets(fee, DIM, stdin);
fprintf(myfile, "\n%s", bknum);
fprintf(myfile, "%s", customer);
fprintf(myfile, "%s", movie);
fprintf(myfile, "%s", date);
fprintf(myfile, "%s", time);
fprintf(myfile, "%s", guest);
fprintf(myfile, "%s", house);
fprintf(myfile, "%s", ticket);
fprintf(myfile, "%s", fee);
fclose (myfile);
}
The thing I'm trying to do is to accept a few pieces of information, then putting those information into project.txt. On its own, the code runs fine, no bugs. But when I run the main part, it will ask me to choose an option (op1), then it will call the code "void op1()", to run it (that is how its supposed to be), the problem comes after, the user inputs are supposed to come one at a time, for some reason "enter a booking number" & " enter customers name" show up together.
Please help me try to fix this and let me know the problem as well. Thank a lot