1

im pretty new to c++ and ive been tasked to make a booking system as a mini project for school it would be great if someone could help me fix this mess that is far too many for loops for a basic booking system and tell me how id somehow make these arrays read from a text file so that it replaces the times if someone has already booked. thanks in advance

void Booking()
{
    string booked;
    string timeBooked;
    //Sets the week and time array 

    string Monday[11] = { "Monday   :","8am","9am","10am","11am","12pm","1pm","2pm","3pm","4pm","5pm" };
    string Tuesday[11] = { "Tuesday  :","8am","9am","10am","11am","12pm","1pm","2pm","3pm","4pm","5pm" };
    string Wednesday[11] = { "Wednesday:","8am","9am","10am","11am","12pm","1pm","2pm","3pm","4pm","5pm" };
    string Thursday[11] = { "Thursday :","8am","9am","10am","11am","12pm","1pm","2pm","3pm","4pm","5pm" };
    string Friday[11] = { "Friday   :","8am","9am","10am","11am","12pm","1pm","2pm","3pm","4pm","5pm" };
    string Saturday[11] = { "Saturday :","8am","9am","10am","11am","12pm","1pm","2pm","3pm","4pm","5pm" };
    string Sunday[11] = { "Sunday   :","8am","9am","10am","11am","12pm","1pm","2pm","3pm","4pm","5pm" };


    // Create and open a text file
    ofstream MyFile("Booking.txt");

    //Outputs a statment line for the day and time
    cout << "Available Times for next weeks driving lessons:" << endl;

    //runs through both day and time array to create a timetable of dates
    for (int i = 0; i < 11; i++)
    {
        cout << Monday[i] << " ";

    }
    cout << endl;
    for (int i = 0; i < 11; i++)
    {
        cout << Tuesday[i] << " ";
    }
    cout << endl;
    for (int i = 0; i < 11; i++)
    {
        cout << Wednesday[i] << " ";
    }
    cout << endl;
    for (int i = 0; i < 11; i++)
    {
        cout << Thursday[i] << " ";
    }
    cout << endl;
    for (int i = 0; i < 11; i++)
    {
        cout << Friday[i] << " ";
    }
    cout << endl;
    for (int i = 0; i < 11; i++)
    {
        cout << Saturday[i] << " ";
    }
    cout << endl;
    for (int i = 0; i < 11; i++)
    {
        cout << Sunday[i] << " ";
    }
    cout << endl << endl;

    bool valid = false;

    while (valid == false)
    {
        //Asks the user to input their wanted day 
        cout << "Please select an available day you wish to book: ";
        cin >> booked;

        if (booked != "Monday")
        {
            if (booked != "Tuesday")
            {
                if (booked != "Wednesday")
                {
                    if (booked != "Thursday")
                    {
                        if (booked != "Friday")
                        {
                            if (booked != "Saturday")
                            {
                                if (booked != "Sunday")
                                {
                                    cout << "Please enter a valid day" << endl;
                                }
                                else
                                {
                                    valid = true;
                                }
                            }
                            else
                            {
                                valid = true;
                            }
                        }
                        else
                        {
                            valid = true;
                        }
                    }
                    else
                    {
                        valid = true;
                    }
                }
                else
                {
                    valid = true;
                }
            }
            else
            {
                valid = true;
            }
        }
        else
        {
            valid = true;
        }
    }

    valid = false;

    while (valid == false)
    {
        cout << "Please enter the time you wish to book (Time am/pm E.g 2pm): ";
        cin >> timeBooked;

        for (int i = 0; i < 11; i++)
        {
            if (timeBooked == Monday[i])
            {
                valid = true;

                if (booked == "Monday")
                {
                    Monday[i] = "Unavailable";
                }
                else
                {
                    if (booked == "Tuesday")
                    {
                        Tuesday[i] = "Unavailable";
                    }
                    else 
                    {
                        if (booked == "Wednesday")
                        {
                            Wednesday[i] = "Unavailable";
                        }
                        else
                        {
                            if (booked == "Thursday")
                            {
                                Thursday[i] = "Unavailable";
                            }
                            else
                            {
                                if (booked == "Friday")
                                {
                                    Friday[i] = "Unavailable";
                                }
                                else
                                {
                                    if (booked == "Saturday")
                                    {
                                        Saturday[i] = "Unavailable";
                                    }
                                    else
                                    {
                                        if (booked == "Sunday")
                                        {
                                            Sunday[i] = "Unavailable";
                                        }
                                        else
                                        {

                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }   
    }

    for (int i = 0; i < 11; i++)
    {
        MyFile << Monday[i];
    }
    for (int i = 0; i < 11; i++)
    {
        MyFile << Tuesday[i];
    }
}
mch
  • 9,424
  • 2
  • 28
  • 42

1 Answers1

0

One useful principle for simplifying code (in any programming language) is:

  • Replace repetitive code with data

Variable names, like your Monday...Sunday arrays are code. You can't iterate over code, but you can iterate over data, so replace them with

std::vector<std::string> days { "Monday", "Tuesday", ..., "Sunday" };

Now, instead of writing 7 identical loops, you can write a single

for (const auto& day : days) {
    // print day and all the time slots
}

and instead of writing 7 nested if blocks, you can compare the input string with each day in turn, again using a loop.


Now, you still need to keep a list of available slots per day. Again, you don't need seven different variables for that:

using Slots = std::vector<std::string>;
Slots default_slots { "8am","9am","10am","11am","12pm","1pm","2pm","3pm","4pm","5pm" };

std::map<std::string, Slots> day_slots;
for (const auto& day : days) {
    day_slots[day] = default_slots;
}

Now, when you have both the (validated) day and the slot, you just need something like:

Slots& booked_slots = day_slots[booked];
auto i = std::find(booked_slots.begin(), booked_slots.end(), timeBooked);
if (i == booked_slots.end()) {
    // don't recognize this time, or it isn't available, so go round again
}
else
{
    *i = "Unavailable";
    break; // we're done, so exit the loop
}

I'm not in love with storing all your values as strings rather than, say, enumerating the days and storing integer hours, but this is the smallest change to your original code that makes it vaguely reasonable.

Similarly, you could replace vector here with array - I just didn't want to type out the array lengths because I'm not interested in those magic numbers. Either are better than using raw arrays, and vector gives you the option of erasing unavailable time slots instead of just changing the string to "Unavailable" (which then gives you the option of filtering out days for which no slots are available).

Useless
  • 64,155
  • 6
  • 88
  • 132
  • Thank you this was very helpful after doing some looking into vectors as id never heard about them outside the mathematics use case it was a huge help thank you for your time! – AlexBeepBoop Apr 14 '22 at 11:40
  • If you haven't even _heard_ of `std::vector`, you should really look at getting a good [book](https://stackoverflow.com/q/388242/212858). The "replace code with data" thing works in most languages, but it's no help if you don't know enough about C++ in the first place to actually implement it. – Useless Apr 14 '22 at 11:50
  • Thank you’ll I’ll definitely look into getting one considering my schools ability to teach reaches as far as for, else and while loops … – AlexBeepBoop Apr 15 '22 at 12:11