Would anyone please help me figure out why my void printMessage(char name[],char
ID: 3730216 • Letter: W
Question
Would anyone please help me figure out why my void printMessage(char name[],char num[]); and int loginMatch(int passcode,int adminPasscode); are not working correctly in my c program? Thank you.
Here is the code:
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <time.h>
8 #include <string.h>
9
10 #define ROW 6
11 #define COL 4
12 #define MAX 25
13
14 void initialSeats(char flight[][COL], int count);
15 void displayMenu();
16 void printFlightMap(char flight[][COL]);
17 int loginMatch(int passcode, int adminPasscode);// my passcode is 10506//
18 int getTotalRevenue(char f1[][COL], char f2[][COL], char f3[][COL]);
19 int costMatrix[ROW][COL] = {{500, 200, 200, 500};
20 {500, 200, 200, 500},
21 {500, 200, 200, 500},
22 {500, 200, 200, 500},
23 {500, 200, 200, 500},
24 {500, 200, 200, 500}};
25 void flightMenu();
26 void seatReservation(char flight[][COL]);
27 void printMessage(char name[], char num[]);
28
29
30 void flightMenu()
31 {
32
33 printf(" 1.) COU----> MIA 2.)---->COU ----> BNA 3.) COU ----> LAS ");
34 printf("Choose a flight:");
35 }
36
37
38
39
40
41
42 void displayMenu()
43 {
44 printf("*******WELCOME TO JOE'S AIRLINE BOOKING SYSTEM******* ");
45 printf(" 1.) Admin Log-in 2.) Reserve a seat 3.)Exit ");
46
47 }
48
49
50
51
52
53 int loginMatch(int passcode, int adminPasscode)
54 {
55 if (passcode == adminPasscode)
56 {
57 return 1;
58 }
59 else return 0;
60 }
61
62
63
64
65
66 int getTotalRevenue(char f1[][COL], char f2[][COL], char f3[][COL])
67 {
68 int cost = 0;
69 int i = 0, j = 0;
70 for (i = 0; i < ROW; i++){
71 for (j = 0; j < COL; j++){
72 if(f1[i][j] == 'X'){
73 cost = cost + costMatrix[i][j];
74 }
75 }
76 }
77
78 for (i = 0; i < ROW; i ++){
79 for (j = 0; j < COL; j++){
80 if (f2[i][j] == 'X') {
81 cost = cost + costMatrix[i][j];
82
83
84 }
85 }
86 }
87
88 for(i= 0; i < ROW; i++){
89 for (j = 0; j < COL; j++){
90 if (f3[i][j] == 'X'){
91 cost = cost + costMatrix[i][j];
92
93 }
94 }
95 }
96
97 return cost;
98 }
99
100
101
102
103
104 void seatReservation(char flight [][COL])
105 {
106 int r,c;
107 int g = 0;
108 while(g == 0)
109 {
110 printf(" Which Seat row do you want?:");
111 scanf("%d",&r);
112 printf(" Which Seat column do you want?:");
113 scanf("%d", &c);
114
115 if(r>5||r<0)
116 {
117 printf(" Seat Rows are between 0 and 5");
118 }
119
120 else if(c>3||c<0)
121 {
122 printf(" Seat columns are between 0 and 3. ");
123 }
124 else if (flight[r][c] =='X')
125 {
126 printf(" Sorry! Someone has reserved that seat. Please Try Again. ");
127 }
128 else
129 {
130 flight[r][c] = 'X';
131 printf(" Your requested seat has been reserved ");
132 g = 1;
133 }
134 }
135 }
136
137
138
139
140
141
142 void printFlightMap(char flight[][COL])
143 {
144 int i, j;
145 for(i = 0; i<ROW; i++)
146 {
147 for(j=0;j<COL;j++)
148 {
149 printf("%c",flight[i][j]);
150 }
151 printf(" ");
152 }
153 }
154
155
156
157
158
159 void initialSeats(char flight[][COL], int count)
160 {
161
162 int i,j,row, col,x = 0;
163 srand((unsigned)time(NULL));
164 for(i = 0; i <ROW; i++)
165 {
166 for(j = 0; j<COL; j++)
167 {
168 flight[i][j] = 'O';
169 }
170 }
171 while(x< count)
172 {
173 row = rand() % ROW;
174 col = rand() % COL;
175 flight[row][col] = 'X';
176 x++;
177 }
178 }
179
180
181 //void printMessage(char name[], char num[])
182 //{
183 // printf("Congratulations %s", &name);
184 // printf("Your flight %d is booked. Your e-ticket # is: ", &num);
185 // int size1 = 0;
186 // int size while (name[size size1--;
187
188 // while(num[size2] !='')
189 // {
190 // size2++;
191 // }
192 // size2--;
193
194 // int i = 0, j = 0;
195 // while(i<size1 && j< size2)
196 // {
197 // printf("%s %s", name[i++], num[j++]);
198 // }
199 // if(i <size1)
200 // {
201 // while(i< size1){
202 // printf("%s", name[i++]);
203 // }
204 // }
205 // if (j< size2){
206 // while(j < size2){
207 // printf("%s",num[j++]);
208 // }
209 // }
211 //
212
213
214 int main (void)
215 {
216
217 char flight_1[ROW][COL], flight_2[ROW][COL], flight_3[ROW][COL];
218 int reservedSeats = (rand() % 5) + 1;
219
220 initialSeats(flight_1, reservedSeats);
221 initialSeats(flight_2, reservedSeats);
222 initialSeats(flight_3, reservedSeats);
223
224 int choice= -1;
225 int result = -1;
226 int adminPasscode = 105016;
227 int passcode, total;
228
229 while (choice != 3)
230 {
231 displayMenu();
232 printf("Choose an option: ");
233 scanf("%d", &choice);
234
235 if (choice ==1)
236 {
237 result = 0
238 ;
239 while (result !=1)
240 {
241 printf("Enter the login passcode to log in as an Administrator:");
242 scanf("%d", &passcode);
243
244
245 //I can't figure out why my passcode is 10506 and not 105016//
246 result = (loginMatch(passcode, adminPasscode) == 0);
247 if (result == 0)
248 {
249 printf("Invalid passcode, Try again: ");
250 scanf("%d", &passcode);
251 }
252 else
253 {
254 printf("login ");
255 }
256
257 printf(" Printing the Flight Map of the flight Columbia to Miami...");
258 printFlightMap(flight_1);
259
260 printf(" Printing the Flight Map of the flight Columbia to Nashville...");
261 printFlightMap(flight_2);
262
263 printf(" Printing the Flight Map of the flight Columbia to Las Vegas....");
printFlightMap(flight_3);
265
266
267 total = getTotalRevenue(flight_1, flight_2, flight_3);
268 printf(" The total earning from all the flights: $%d ", total);
269
270
271 printf("You are logged out now!");
272 }
273
274 }
275 }
276
277 if ( choice == 2)
278 {
279 result = -1;
280
281 while (result < 1 || result > 3)
282 {
283 flightMenu();
284 printf(" Choose a flight:");
285 scanf("%d", &result);
286 }
287 if(result == 1)
288 {
289 seatReservation(flight_1);
290 //printMessage(firstName,MIA)
291 }
292 else if (result == 2)
293 {
294 seatReservation(flight_2);
295 //printMessage(firstName, BNA);
296 }
297 else if (result == 3)
298 {
299 seatReservation(flight_3);
300 // printMessage(firstName,LAS);
301 }
302 }
303
304 return 1;
305 }
This is what the function is supposed to do:
void printMessage(char name[],char num[]);
//DO THIS, IS IF YOU ARE DOING THE BONUS
This function is called for each flight option after the seatReservation() is called. It takes in two
strings, name of passenger and the flight number for that particular case. Both are character
arrays (strings). For flight Cou->Miami number must be set as “MIA1050”, for Cou->Nashville
number must be set as “BNA1050”, for flight Cou->Las Vegas num must be set as “LAS1050”,
this must be done in main and the name and particular number must be sent to this function.
Once in this function, you will merge (important part) the two strings into third string and print
that as the e-Ticket as shown below and print a confirmation message.
Eg1: name is “Fred” number is “MIA1050”, your merged string should be FMrIeAd1050. Print
this as the booking confirmation number.
Eg2: name is “Guilliams” number is “LAS1050”, you merged string should be
GLuAiSl1l0i5a0ms.
Eg3: name is “Christopher” number is “BNA1050”, your merged string should be
CBhNrAi1s0t5o0pher
SAMPLE OUTPUT:
1.) COU ----> MIA
2.) COU ----> BNA
3.) COU ----> LAS
Choose a flight:
3
Enter your first name:
Sue
O X O O
O O O O
X O O O
O O O O
O O O O
O O O O
Which Seat row do you want? :
2
Which seat column do you want? :
0
Sorry!! someone has reserved that seat. Please Try Again.
Which Seat row do you want? :
2
Which seat column do you want? :
3
Your requested seat has been reserved
O X O O
O O O O
X O O
X
//Chosen seat $500
O O O O
O O O O
O O O O
Congrats Sue, your flight LAS1050 is booked, enjoy your trip.
Your e-ticket number is : SLuAeS1050
Enjoy your Thanksgiving Break :)
Explanation / Answer
There are certain concerns with the functions and their usage:
loginMatch is a simple function.
The way it is called in the code:
result = (loginMatch(passcode, adminPasscode) == 0);
it can be simply called as :
result = loginMatch(passcode, adminPasscode) // You should definately get the right result
2. PrintMessage
The current code of the PrintMessage has print statements which are not correct, they
might give you a segmengtation fault.
for example
printf("Your flight %d is booked. Your e-ticket # is: ", &num);
%d is given and the argument is &num (Not correct)
printf("%s %s", name[i++], num[j++]);
%s is given and the argument is num[i++] (Not correct)
num[i++] is a character and not string.
Please correct the print statements in order to check your logic and call the loginMatch
function in a simpler way.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.