I’m making a function for my project (car rental program) and I have an issue with a cancel a reservation function

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

New contributor

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

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật