need help checking my code; thank:)

I know this is supposed to be a c++ programming website, but i would really appreciate if someone would help me check this c programming code. thanks in advance:)


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <stdio.h>
#define DIM 512
#define MAXCHAR 30

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,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#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 :)

The problem is that scanf(...) does not remove the new line character from the stream. The first fgets(...) will be satisfied (because it finds the remaining new line character in the stream) and returns immediately.

One solution could be to place a fgets(...) after line 20 that removes the new line character.
@coder777
im sorry, im a little confused here. So are you saying that I should replace scanf(), with fgets()? If yes, i thought that we can only use fgets for char inputs.

Your solution;
do i just put the fgets under line 20? what will it include then?
Similar problems arise in C++ too, so I've suggested a similar solution.

Minimal code to reproduce the problem:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <stdio.h>

#define DIM 512
#define MAXCHAR 30

void op1();

int main()
{    
    int option;
    
    printf("What is your Option <1-5>?");
    scanf("%d", &option);
        
    op1();

    return 0;
}

void op1()
{
    char bknum[DIM];
    char customer[MAXCHAR];
    
    printf("Please enter Booking Number:\n");
    fgets(bknum, sizeof(bknum), stdin);
    
    printf("Please enter Customer Name:\n");
    fgets(customer, sizeof(customer), stdin);
    
    printf("\n%s", bknum);
    printf("%s", customer);
}


The problem is, when the user enters a number at line 13, the enter key is pressed. That leaves a trailing newline character in the input buffer.

Next, at line 26, the fgets() will read characters into the array until it finds a newline character. Unfortunately, it finds the newline immediately, so stores an empty string and proceeds to the next statement.

Solution? Remove the unwanted newline from the input buffer. This is my suggestion. (I thought there might be some clever scanf option, but that seems to apply to leading whitespace only, not to trailing whitespace).

13
14
15
16
    scanf("%d", &option);
    
    while ( getc(stdin) != '\n' ) 
        ; /* Empty loop */

The loop at line 15 is used in case there were any extraneous characters typed after the user input), it discards characters until a newline is found.

By the way, I used the sizeof operator in my version, it saves having to check what the size of the array was, and should avoid accidentally typing the wrong size.
do i just put the fgets under line 20?
Yes.

what will it include then?
Nothing relevant. Usually only the new line character. You may use the solution from Chervil as well.
ok!! Thanks a lot mate!
appreciate it!
Topic archived. No new replies allowed.