-
Notifications
You must be signed in to change notification settings - Fork 0
/
carRentalRegistration.h
117 lines (74 loc) · 2.4 KB
/
carRentalRegistration.h
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
#include <stdio.h>
#include <stdlib.h>
// Define color codes
#define RED "\033[1;31m"
#define GREEN "\033[1;32m"
#define YELLOW "\033[1;33m"
#define BLUE "\033[1;34m"
#define CYAN "\033[1;36m"
#define RESET "\033[1;0m"
int extract_car_id ( void ) {
FILE *idfptr;
if ( (idfptr = fopen ("carid.txt", "r")) == NULL) {
puts ("File can not be opened");
exit(EXIT_SUCCESS);
}
int id;
fscanf (idfptr, "%d", &id);
fclose (idfptr);
// printf ("%d", id);
return id;
}
// ! UPDATING OUR ID FILE
void update_car_id ( int id ) {
FILE *idfptr;
if ( (idfptr = fopen ("carid.txt", "w")) == NULL) {
puts ("File can not be opened");
exit(EXIT_SUCCESS);
}
fprintf (idfptr, "%d", id);
fclose (idfptr);
}
void car_rental_registration ( void ){
Car_Rental car = generate_empty_car();
// Providing th eunique id
unsigned short int id = extract_car_id ();
car.id = id;
++id;
update_car_id( id );
while ( '\n' != getchar() );
printf ("Enter your user name: ");
scanf("%29[^\n]", car.username);
while ('\n' != getchar());
printf ("Enter your company name: ");
scanf("%29[^\n]", car.company_name);
while ('\n' != getchar());
do {
printf ("Enter your city name: ");
scanf("%29[^\n]", car.city_name);
if ( check_name(car.city_name) ) break;
else {
puts ("We are not currently available here yet.");
continue;
}
}while (1);
while ('\n' != getchar());
// ! UPADTING THE FILE HERE
FILE *cfptr; // car file pointer
if ( (cfptr = fopen("cars.dat", "rb+")) == NULL ) { // opening the file
puts ("FIle can not be opened!");
return;
}
// writing the changes
fseek ( cfptr, (car.id - 1) * sizeof (Car_Rental), SEEK_SET );
fwrite( &car, sizeof (Car_Rental), 1, cfptr );
fflush (cfptr);
fclose ( cfptr ); // closing file
puts (BLUE"+--------------------------------------------------------------------+"RESET);
printf (BLUE"|"CYAN"Remember your following credentials, you need them while logging in."BLUE"|\n"RESET);
puts (BLUE"+--------------------------------------------------------------------+"RESET);
printf (GREEN"=>id: %d\n"RESET, car.id);
printf (GREEN"=>username: %s\n\n"RESET, car.username);
press_enter_to_continue();
clearScreen();
}