first of all lets say I have 2 rented cars already:
ID,PlateNumber,StartDate,EndDate,TotalPrice (this line doesn’t show in the csv file its just for reference)
output:
,,,, (this line is always empty and idk why)
3,123456Z,Tue May 07 01:00:00 2024,Wed May 08 01:00:00 2024,200.00$
3,789123B,Tue May 07 01:00:00 2024,Wed May 08 01:00:00 2024,100.00$
(the ID is the user who rented the cars)
and now let’s say user 3 doesn’t want the car of plate 123456Z
when I go ahead and delete the car and open the csv file, it all of a sudden looks like this:
output:
,,,Thu Jan 01 02:00:07 1970,
3,789123B,Thu Jan 01 02:00:00 1970, Thu Jan 01 02:00:07 1970,
and this is all the code related to it:
struct UserInformation {
string ID;
string firstName;
string lastName;
string password;
string email;
string phoneNumber;
string authority;
void printUserInformation() const {
cout << "ID: " << ID << endl;
cout << "First Name: " << firstName << endl;
cout << "Last Name: " << lastName << endl;
cout << "Password: " << password << endl;
cout << "Email: " << email << endl;
cout << "Phone Number: " << phoneNumber << endl;
cout << "Authority: " << authority << endl;
}
};
struct CarsInformation {
string type;
string brand;
string model;
string year;
string subType;
string color;
string plateNumber;
string price;
string availability;
void printCarsInformation() const {
cout << "Type: " << type << endl;
cout << "Brand: " << brand << endl;
cout << "Model: " << model << endl;
cout << "Year: " << year << endl;
cout << "SubType: " << subType << endl;
cout << "Color: " << color << endl;
cout << "Plate Number: " << plateNumber << endl;
cout << "Price per day: " << price << endl;
cout << "Availability: " << availability << endl;
}
};
struct RentInformation {
string ClientID;
string CarPlateNumber;
time_t RentStartDate;
time_t RentEndDate;
string TotalPriceForTheRent;
};
this one might have something to do with the empty line:
void appendRentInfoToCSV(const RentInformation& rentInfo) {
ofstream outputFile("C:\Users\User\Desktop\rented_cars.csv", ios::out | ios::app);
if (outputFile.is_open()) {
char startDateStr[50];
char endDateStr[50];
strftime(startDateStr, sizeof(startDateStr), "%a %b %d %H:%M:%S %Y", localtime(&rentInfo.RentStartDate));
strftime(endDateStr, sizeof(endDateStr), "%a %b %d %H:%M:%S %Y", localtime(&rentInfo.RentEndDate));
outputFile << rentInfo.ClientID << ',' << rentInfo.CarPlateNumber << ','
<< startDateStr << ',' << endDateStr << ','
<< fixed << setprecision(2) << rentInfo.TotalPriceForTheRent << endl;
cout << "Reservation information added successfully." << endl;
outputFile.close(); // Close the file inside the if block
} else {
cerr << "Failed to open file for writing." << endl;
}
}
this is the function:
void cancelRESERVATION(CarsInformation cars[], int& numCars, RentInformation rents[], int& numRents, UserInformation users[], int& numUsers, string userID) {
// Open the rented_cars.csv file to read the reservations
ifstream inputFile("C:\Users\User\Desktop\rented_cars.csv");
if (!inputFile.is_open()) {
cerr << "Failed to open file for reading." << endl;
return;
}
// Read and display all reservations of the user
bool hasReservations = false;
int j = 1;
string line;
while (getline(inputFile, line)) {
// Split the CSV line into fields
stringstream ss(line);
string field;
string fields[5]; // Assuming 5 fields per line
int fieldIndex = 0;
while (getline(ss, field, ',')) {
fields[fieldIndex++] = field;
}
// Check if the line corresponds to the current user's reservation
if (fieldIndex == 5 && fields[0] == userID) {
// Print reservation information
cout << "Car #" << j++ << endl;
cout << "Plate Number: " << fields[1] << endl;
cout << "Start Date: " << fields[2] << endl;
cout << "End Date: " << fields[3] << endl;
cout << "Total Price: " << fields[4] << endl;
hasReservations = true;
}
}
inputFile.close();
// If the user has no reservations, inform them and return
if (!hasReservations) {
cout << "You have no reservations to cancel." << endl;
return;
}
// Ask the user to enter the plate number of the car they want to cancel the reservation for
string plateNumber;
cout << "Enter the plate number of the car you want to cancel the reservation for: ";
cin >> plateNumber;
// Find the reservation with the given plate number and user ID
int index = -1;
for (int i = 0; i < numRents; i++) {
if (rents[i].CarPlateNumber == plateNumber && rents[i].ClientID == userID) {
index = i;
break;
}
}
// If the reservation is found, cancel it
if (index != -1) {
// Move all reservations after the canceled one one position to the left
for (int i = index; i < numRents - 1; i++) {
rents[i] = rents[i + 1];
}
numRents--;
// Update the car's availability to "available" in the cars array
for (int i = 0; i < numCars; i++) {
if (cars[i].plateNumber == plateNumber) {
cars[i].availability = "available";
break;
}
}
// Rewrite the updated list of cars to the CSV file
ofstream outputFile("C:\Users\User\Desktop\cars.csv");
if (outputFile.is_open()) {
for (int i = 0; i < numCars; i++) {
outputFile << cars[i].type << ',' << cars[i].brand << ',' << cars[i].model << ',' << cars[i].year << ',' << cars[i].subType << ',' << cars[i].color << ',' << cars[i].plateNumber << ',' << cars[i].price << ',' << cars[i].availability << endl;
}
outputFile.close();
cout << "Car availability updated successfully." << endl;
} else {
cerr << "Failed to open file for writing." << endl;
}
// Rewrite the updated list of reservations to the CSV file
ofstream outputFile2("C:\Users\User\Desktop\rented_cars.csv");
if (outputFile2.is_open()) {
for (int i = 0; i < numRents; i++) {
char startDateStr[50];
char endDateStr[50];
strftime(startDateStr, sizeof(startDateStr), "%a %b %d %H:%M:%S %Y", localtime(&rents[i].RentStartDate));
strftime(endDateStr, sizeof(endDateStr), "%a %b %d %H:%M:%S %Y", localtime(&rents[i].RentEndDate));
outputFile2 << rents[i].ClientID << ',' << rents[i].CarPlateNumber << ','
<< startDateStr << ',' << endDateStr << ','
<< fixed << setprecision(2) << rents[i].TotalPriceForTheRent << endl;
}
outputFile2.close();
cout << "Updated reservation information written to the CSV file." << endl;
} else {
cerr << "Failed to open file for writing." << endl;
}
cout << "Reservation canceled successfully." << endl;
} else {
cout << "Reservation not found for the provided plate number." << endl;
}
}
I do not have anything in mind, I just want it to work
Elie ISSA is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
4