` #include "Client.h"
#include <fstream>
#include <sstream>
#include <iostream>
using namespace std;
Car* readCarsFromCSV(const string& filename, int& numCars) {
ifstream file(filename);
if (!file.is_open()) {
cout << "Error: Unable to open " << filename << " for reading." << endl;
numCars = 0;
return nullptr;
}
Car* cars = nullptr;
string line;
int index = 0;
while (getline(file, line)) {
if (line.empty()) continue;
stringstream ss(line);
if (index == 0) {
cars = new Car[1];
} else {
Car* temp = new Car[index + 1];
for (int i = 0; i < index; ++i) {
temp[i] = cars[i];
}
delete[] cars;
cars = temp;
}
getline(ss, cars[index].plateNumber, ',');
getline(ss, cars[index].brand, ',');
string yearStr;
getline(ss, yearStr, ',');
bool validYear = true;
for (char c : yearStr) {
if (!isdigit(c)) {
validYear = false;
break;
}
}
if (validYear && !yearStr.empty()) {
cars[index].year = stoi(yearStr);
} else {
cerr << "Invalid year: '" << yearStr << "' in line: " << line << endl;
cars[index].year = 0;
}
getline(ss, cars[index].model, ',');
string priceStr;
getline(ss, priceStr, ',');
try {
cars[index].pricePerDay = stod(priceStr);
} catch (const invalid_argument& e) {
if (!priceStr.empty()) {
cerr << "Invalid price: " << priceStr << " in line: " << line << endl;
}
cars[index].pricePerDay = 0.0;
}
getline(ss, cars[index].color);
index++;
}
file.close();
numCars = index;
return cars;
}
Rental* readRentalsFromCSV(const string& filename, int& numRentals) {
ifstream file(filename);
if (!file.is_open()) {
cout << "Error: Unable to open " << filename << " for reading." << endl;
numRentals = 0;
return nullptr;
}
Rental* rentals = nullptr;
string line;
int index = 0;
while (getline(file, line)) {
stringstream ss(line);
if (index == 0) {
rentals = new Rental[1];
} else {
Rental* temp = new Rental[index + 1];
for (int i = 0; i < index; ++i) {
temp[i] = rentals[i];
}
delete[] rentals;
rentals = temp;
}
getline(ss, rentals[index].idNumber, ',');
getline(ss, rentals[index].plateNumber, ',');
getline(ss, rentals[index].d.startDate, ',');
getline(ss, rentals[index].d.endDate);
index++;
}
file.close();
numRentals = index;
return rentals;
}
void writeRentalCarsToCSV(const Rental* rentals, int numRentals) {
ofstream file("C:\Games\Car Rental Program\rentalcars.csv");
if (!file.is_open()) {
cout << "Error: Unable to open rentalcars.csv for writing." << endl;
return;
}
file << "User ID,Plate Number,Start Date,End Date" << endl;
for (int i = 0; i < numRentals; ++i) {
if (i < numRentals && rentals[i].idNumber != "" && rentals[i].plateNumber != "" && rentals[i].d.startDate != "" && rentals[i].d.endDate != "") {
file << rentals[i].idNumber << "," << rentals[i].plateNumber << "," << rentals[i].d.startDate << "," << rentals[i].d.endDate << endl;
}
}
file.close();
cout << "Rented cars data written to rentalcars.csv successfully." << endl;
}
void displayAvailableCars(const Car* cars, int numCars) {
cout << "Available cars:" << endl;
for (int i = 0; i < numCars; ++i) {
cout << "Plate Number: " << cars[i].plateNumber << endl;
cout << "Brand: " << cars[i].brand << endl;
cout << "Year: " << cars[i].year << endl;
cout << "Model: " << cars[i].model << endl;
cout << "Price per Day: " << cars[i].pricePerDay << endl;
cout << "Color: " << cars[i].color << endl;
cout << endl;
}
}
bool checkAvailability(const Rental* rentals, int numRentals, const string& plateNumber, const string& rentedDate) {
for (int i = 0; i < numRentals; ++i) {
if (rentals[i].plateNumber == plateNumber && rentals[i].d.startDate <= rentedDate && rentals[i].d.endDate >= rentedDate) {
return false;
}
}
return true;
}
void rentCar(Rental*& rentals, int& numRentals, const string& plateNumber, const string& startDate, const string& endDate) { //this is where i believe the error is
if (numRentals < 0) {
cout << "Error: numRentals is not initialized properly." << endl;
return;
}
if (numRentals % 10 == 0) {
Rental* temp = new Rental[numRentals + 10];
if (!temp) {
cout << "Error: Memory allocation failed." << endl;
return;
}
for (int i = 0; i < numRentals; ++i) {
temp[i] = rentals[i];
}
delete[] rentals;
rentals = temp;
}
rentals[numRentals] = {to_string(numRentals + 1), plateNumber, {startDate, endDate}};
++numRentals;
writeRentalCarsToCSV(rentals, numRentals);
cout << "Car rented successfully!" << endl;
}
void cancelRental(Rental*& rentals, int& numRentals, Car* cars, int numCars) {
cout << "Rented cars:" << endl;
for (int i = 0; i < numRentals; ++i) {
cout << "Reservation ID: " << rentals[i].idNumber << endl;
cout << "Plate Number: " << rentals[i].plateNumber << endl;
cout << "Start Date: " << rentals[i].d.startDate << endl;
cout << "End Date: " << rentals[i].d.endDate << endl;
cout << endl;
}
string plateNumber;
cout << "Enter the plate number of the car reservation to cancel: ";
getline(cin, plateNumber);
bool found = false;
for (int i = 0; i < numRentals; ++i) {
if (rentals[i].plateNumber == plateNumber) {
found = true;
for (int j = 0; j < numCars; ++j) {
if (cars[j].plateNumber == plateNumber) {
cars[j].rented = false;
cars[j].startDate = "";
cars[j].endDate = "";
break;
}
}
for (int j = i; j < numRentals - 1; ++j) {
rentals[j] = rentals[j + 1];
}
--numRentals;
cout << "Reservation canceled successfully." << endl;
break;
}
}
if (!found) {
cout << "No reservation found for the entered plate number." << endl;
}
}
void modifyRentalDates(Rental* rentals, int numRentals, Car* cars, int numCars) {
string plateNumber;
cout << "Enter the plate number of the rented car to modify dates: ";
getline(cin, plateNumber);
bool found = false;
for (int i = 0; i < numRentals; ++i) {
if (rentals[i].plateNumber == plateNumber) {
found = true;
string newStartDate, newEndDate;
cout << "Enter the new start date (YYYY-MM-DD): ";
getline(cin, newStartDate);
cout << "Enter the new end date (YYYY-MM-DD): ";
getline(cin, newEndDate);
rentals[i].d.startDate = newStartDate;
rentals[i].d.endDate = newEndDate;
// Update car's rental dates
for (int j = 0; j < numCars; ++j) {
if (cars[j].plateNumber == plateNumber) {
cars[j].startDate = newStartDate;
cars[j].endDate = newEndDate;
break;
}
}
cout << "Rental dates modified successfully." << endl;
break;
}
}
if (!found) {
cout << "No rental found for the entered plate number." << endl;
}
}
void executeClientFunctionalities() {
int numCars;
Car* cars = readCarsFromCSV("C:\Games\Car Rental Program\cars.csv", numCars);
if (!cars) {
cout << "Error: Unable to read cars data." << endl;
return;
}
int numRentals;
Rental* rentals = readRentalsFromCSV("C:\Games\Car Rental Program\rentalcars.csv", numRentals);
if (!rentals) {
cout << "Error: Unable to read rentals data." << endl;
delete[] cars;
return;
}
while (true) {
string choice;
cout << "nMenu:" << endl;
cout << "1. View available cars" << endl;
cout << "2. Check car availability" << endl;
cout << "3. Rent a car" << endl;
cout << "4. Cancel rental reservation" << endl;
cout << "5. Modify rented car dates" << endl;
cout << "6. Exit" << endl;
cout << "Enter your choice: ";
getline(cin, choice);
if (choice == "1") {
displayAvailableCars(cars, numCars);
} else if (choice == "2") {
string plateNumber;
cout << "Enter the plate number of the car: ";
getline(cin, plateNumber);
if (checkAvailability(rentals, numRentals, plateNumber, "")) {
cout << "The car with plate number " << plateNumber << " is available." << endl;
} else {
cout << "The car with plate number " << plateNumber << " is not available." << endl;
}
} else if (choice == "3") {
string plateNumber, startDate, endDate;
cout << "Enter the plate number of the car: ";
getline(cin, plateNumber);
cout << "Enter the start date (YYYY-MM-DD): ";
getline(cin, startDate);
cout << "Enter the end date (YYYY-MM-DD): ";
getline(cin, endDate);
rentCar(rentals, numRentals, plateNumber, startDate, endDate);
} else if (choice == "4") {
cancelRental(rentals, numRentals, cars, numCars);
} else if (choice == "5") {
modifyRentalDates(rentals, numRentals, cars, numCars);
} else if (choice == "6") {
break;
} else {
cout << "Invalid choice. Please enter a number between 1 and 6." << endl;
}
}
delete[] cars;
delete[] rentals;
}
This is my code, after i enter the end date for the rental, it just freezes for a few seconds waiting for input, then it just throws out an error: Process finished with exit code -1073741819 (0xC0000005), which i think is related to memory access violation.
It’s a 3 part code program where there is Administration, Authentication, and Client, and after debugging, I’m certain that the error is in the rentCar and writeCar part of the code.`