class reservation program

Im in desperate need of assisstance. I have project to make a class reservation program but i do not know how to add loop in my program. I am still a beginner so please guide and correct me. Thank you

#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;

int main()
{
int choice, room, mMenu, chCancel, roomAv, cancel;
long int date;
char group[10], typeRoom [3], studentId[11], firstname[10], lastname[10];
double numStudent, startTime, endTime;
float hourBooked;

cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
cout << "\n\tUNI Class E-Reservation Program" << endl;
cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
cout << "\n\t >MENU<" << endl;
cout << "\n\t1. Book Class \n\t2. Class Booking Timetable \n\t3. Cancel Booking \n\t4. Exit" << endl;
cout << "\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
cout << "\nENTER CHOICE >" <<endl;
cin >> choice;

switch(choice)
{
case 1:

cout << "\nENTER FIRST NAME >" << endl;
cin >> firstname;
cout << "\nENTER LAST NAME >" << endl;
cin >> lastname;
cout << "\nENTER STUDENT ID >"<< endl;
cin >> studentId;
cout << "\nENTER GROUP >" << endl;
cin >> group;
cout << "\nENTER NUMBER OF STUDENTS >" << endl;
cin >> numStudent;

if (numStudent >= 25 && numStudent <=40)
{
cout << "\n________________TYPE A_________________________" << endl;
cout << "Room Name \tCapacity \tBlock" << endl;
cout << "\n1.ROOM 01 \t33 students \tA" << endl;
cout << "\n2.ROOM 02 \t33 students \tB" << endl;
cout << "\n3.ROOM 03 \t40 students \tA" << endl;
cout << "\n4.ROOM 04 \t40 students \tB" << endl;
cout << "_______________________________________________" << endl;

cout << "\nEnter type of room (A or B):" << endl;
cin >> typeRoom;
cout << "\nPlease choose your room (1,2,3 or 4) >" << endl;
cin >> room;
}


else if (numStudent <= 24 && numStudent > 0)
{
cout << "\n________________TYPE B_________________________" << endl;
cout << "Room Name \tCapacity \tBlock" << endl;
cout << "\n1.ROOM 01 \t25 students \tA" << endl;
cout << "\n2.ROOM 02\t25 students \tB" << endl;
cout << "\n3.ROOM 03 \t15 students \tA" << endl;
cout << "\n4.ROOM 04 \t15 students \tB" << endl;
cout << "_______________________________________________" << endl;

cout << "\nEnter type of room (A or B):" << endl;
cin >> typeRoom;
cout << "\nPlease choose your room (1,2,3 or 4) >" << endl;
cin >> room;
}

else if (numStudent >=41)
{
cout << "Students EXCEEDED maximum value of 40. Booking UNSUCCESSFUL! Please try again. " << endl;
return main ();
}
else
{
cout << "INVALID input. Booking UNSUCCESSFUL! Please try again." << endl;
return main ();
}

cout << "\nPlease enter date of reservation (DDMMYYYY): " << endl;
cin >> date;
cout << "\n-Please enter booking time-" << endl;
cout << "\nEnter start time (in 24hr 00.00 form)>" << endl;
cin >> startTime;
cout << "Enter end time (in 24hr 00.00 form)>" << endl;
cin >> endTime;

cout << "\n_____________________________________________" << endl;
cout << "\t-BOOKING APPROVED BY THE SYSTEM-"<< endl;
cout << "Name : " << firstname << lastname << endl;
cout << "Student ID: " << studentId << endl;
cout << "\nGroup:" << group << endl;
cout << "Room Name: ROOM 0" << room << endl;
cout << "Date: " << date << endl;
cout << "Start Time >" << startTime << endl;
cout <<"End Time >" << endTime << endl;
hourBooked = endTime - startTime;
cout << "Hours booked >" << hourBooked << "hr(s)"<< endl;
cout << "_____________________________________________" << endl;

cout << "\nRETURN to main menu 1-Yes 2-No?" << endl;
cin >> mMenu;
switch(mMenu)
{
case 1: cout << "\nReturning to main menu..." << endl;
return main ();
break;
case 2: cout << "\nExiting.." << endl;
return 0;
break;
}
break;

case 2:


cout << "\n________________TYPE A_________________________" << endl;
cout << "Room Name \tCapacity \tBlock" << endl;
cout << "\n1.ROOM 01 \t33 students \tA" << endl;
cout << "\n2.ROOM 02 \t33 students \tB" << endl;
cout << "\n3.ROOM 03 \t40 students \tA" << endl;
cout << "\n4.ROOM 04 \t40 students \tB" << endl;
cout << "_______________________________________________" << endl;


cout << "\n________________TYPE B_________________________" << endl;
cout << "Room Name \tCapacity \tBlock" << endl;
cout << "\n1.ROOM 01 \t25 students \tA" << endl;
cout << "\n2.ROOM 02\t25 students \tB" << endl;
cout << "\n3.ROOM 03 \t15 students \tA" << endl;
cout << "\n4.ROOM 04 \t15 students \tB" << endl;
cout << "_______________________________________________" << endl;

cout << "\nCheck room availability? (1-Yes 2-No)" << endl;
cin >> roomAv;

cout << "\nEnter room type (A or B):" << endl;
cin >> typeRoom;
cout << "Enter room number (1,2,3 or 4):" << endl;
cin >> room;
cout << "\nPlease enter date of reservation (DDMMYYYY): " << endl;
cin >> date;
cout << "Enter booking START time (in 24hr 00.00 form):" << endl;
cin >> startTime;
cout << "Enter booking END time (in 24hr 00.00 form):" << endl;
cin >> endTime;

// User must input all reservation data to ensure class is available //
cout << "\n_____________________________________________" << endl;
cout << "\t-ROOM DETAILS-"<< endl;
cout << "Room type: TYPE" << typeRoom << endl;
cout << "Room Name: ROOM 0" << room << endl;
cout << "Date: " << date << endl;
cout << "Start Time >" << startTime << endl;
cout <<"End Time >" << endTime << endl;
hourBooked = endTime - startTime;
cout << "Hours would like to book >" << hourBooked << "hr(s)"<< endl;
cout << "_____________________________________________" << endl;

cout << "\nRoom is AVAILABLE! Proceed to main menu for booking? 1-Yes 2-No" << endl;
cin >> mMenu;

switch(mMenu)
{
case 1: cout << "\nReturning to main menu..." << endl;
return main ();
break;
case 2: cout << "\nExiting.." << endl;
return 0;
break;
}
break;

case 3:
cout << "Cancel class reservation? (1-Yes 2-No)" << endl;
cout << "\nPlease enter your option >" << endl;
cin >> chCancel;
if(chCancel==1)
{

cout << "\nENTER FIRST NAME >" << endl;
cin >> firstname;
cout << "\nENTER LAST NAME >" << endl;
cin >> lastname;
cout << "\nPlease enter your student ID >" << endl;
cin>> studentId;
cout << "Please enter your group >" << endl;
cin>> group;
cout << "Please enter your booked room type (A or B) >" << endl;
cin>> typeRoom;
cout << "Please enter your booked room number (1,2,3 or 4) >" << endl;
cin>> room;
cout << "\nPlease enter date of reservation (DD/MM/YYYY): " << endl;
cin >> date;
cout << "Please enter booked START time (in 24hr 00.00 form):" << endl;
cin >> startTime;
cout << "Please enter booked END time (in 24hr 00.00 form):" << endl;
cin >> endTime;

cout << "\n_____________________________________________" << endl;
cout << "\t-BOOKING DETAILS-"<< endl;
cout << "Reserved by: " << firstname << lastname << endl;
cout << "Student ID: " << studentId << endl;
cout << "\nGroup:" << group << endl;
cout << "Room type: TYPE" << typeRoom << endl;
cout << "Room Name: ROOM 0" << room << endl;
cout << "Date: " << date << endl;
cout << "Start Time >" << startTime << endl;
cout <<"End Time >" << endTime << endl;
hourBooked = endTime - startTime;
cout << "Hours booked >" << hourBooked << "hr(s)"<< endl;
cout << "_____________________________________________" << endl;
cout <<"\n Proceed to cancel? 1-Yes 2-No" << endl;
cin >> cancel;

Hello nurnisa,

It is not a good idea to double poat. You should delete the other post before someone replies to it.


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.


Something to work on while I look at your program.

Andy
Don't spam the forum with multiple separate posts on the same topic!
Delete your other post.
Also, when posting code you need to put it in code tags, like this:

[code]
your code goes here
[/code]

Edit the above post to add in the code tags.
once you do that, say what you want to loop over as well.
you may want to review this:
https://www.cplusplus.com/doc/tutorial/control/
Hello nurnisa,

Sorry it has taken so long, but your code is a bit hard to read.

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
#include <iostream>
#include <fstream>
#include <string>
//#include <cstring>  // <--- Not needed or used.

using namespace std;

int main()
{
    int choice, room, mMenu, chCancel, roomAv, cancel;
    long int date;
    //char group[10], typeRoom[3], studentId[11], firstname[10]{}, lastname[10]{};
    std::string group, typeRoom, studentId, firstname, lastname;
    double numStudent, startTime, endTime;
    double hourBooked;  // <--- Changed.

    cout <<
        "\n"
        //<< std::string(48, '~') << '\n'  // <--- An alternative to the next line and the others.
        "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
        "\n\tUNI Class E-Reservation Program\n\n"
        "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n"
        "\n\t >MENU<\n"
        "\n\t1. Book Class"
        "\n\t2. Class Booking Timetable"
        "\n\t3. Cancel Booking"
        "\n\t4. Exit\n"
        "\n\t ENTER CHOICE > ";
    cin >> choice;
    std::cout << "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";

    switch (choice)
    {

Line 4, originally "string.h", should be "cstring" for a C++ program, but read the comment.

Line 12 creates several "char" arrays. You do not need "cstring" for this. It is the same concept of some people thing that they need "cmath" for (+, -, *, / and %). These are built into the IDE and compiler, in a maner of speaking.

Line 13 is what you should be using for a C++ program.

Also "string.h" and "cstring" ace C header files for working with C stings. "string" is a C++ header file for working with the string class.

I changed line 15 to a double to eliminate the warning from the compiler. Trying to put the larger "double" into the smaller "float" might cause data loss when some of the digits to the right of the decimal point are dropped to fit the "float".

Unless you are doing something unique with the time there is no point in using a floating point variable for this. Usually "int"s are used. You could prompt to enter the time as "10:35" and the "cin" would look like std::cin >> hour >> junk >> minute. The "junk" variable would be defined as a "char" just to read the (:) in the line.

A bit off track, but I wanted to get that out before I missed it.

The "cout" statement is much easier to work with like this and yo do not need a "cout" and "endl" for each line. Because each line is a quoted string the IDE and compiler treat as 1 string. So in this case it is considered 1 string of 235 characters. That includes the "\0" at the end because it is considered a C string.

The other advantage is that it looks so much like the final output that you have a good of what the final output will look like.

I think what you are looking for would be to put from line 17 to the closing } of the switch in a do while loop so that it keeps repeating until you choose to exit.

Some other code I noticed to be a problem, found in case 1,:
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
if (numStudent >= 25 && numStudent <= 40)
{
    cout <<
        "\n________________TYPE A_________________________\n"
        "Room Name \tCapacity \tBlock\n"
        "\n1.ROOM 01 \t33 students \tA\n"
        "\n2.ROOM 02 \t33 students \tB\n"
        "\n3.ROOM 03 \t40 students \tA\n"
        "\n4.ROOM 04 \t40 students \tB\n"
        "_______________________________________________\n";

    cout << "\nEnter type of room (A or B): ";
    cin >> typeRoom;

    cout << "\nPlease choose your room (1,2,3 or 4) > ";
    cin >> room;
}
else if (numStudent <= 24 && numStudent > 0)
{
    cout << "\n________________TYPE B_________________________" << endl;
    cout << "Room Name \tCapacity \tBlock" << endl;
    cout << "\n1.ROOM 01 \t25 students \tA" << endl;
    cout << "\n2.ROOM 02\t25 students \tB" << endl;
    cout << "\n3.ROOM 03 \t15 students \tA" << endl;
    cout << "\n4.ROOM 04 \t15 students \tB" << endl;
    cout << "_______________________________________________" << endl;

    cout << "\nEnter type of room (A or B):" << endl;
    cin >> typeRoom;
    cout << "\nPlease choose your room (1,2,3 or 4) >" << endl;
    cin >> room;
}

else if (numStudent >= 41)
{
    cout << "Students EXCEEDED maximum value of 40. Booking UNSUCCESSFUL! Please try again.\n";
}
else
{
    cout << "INVALID input. Booking UNSUCCESSFUL! Please try again." << endl;
}

Line 34 on is a problem.
These 2 blocks print an error message, but after that the program continues as if nothing is wrong. Both of these blocks need a "break" statement to exit the switch and start over.

Several places in your code I noticed: return main();. NEVER call "main" either from the "main" function or another function. "main" is special and is only called once when the operating system starts the program. By using return main(); I would say that you are creating a recursive function call with no way out. Also putting most of "main" in a do/while loop you would eliminate the need to call "main".

I do not know if you are up to using functions yet, but most of the code in the case statements of the first switch would be better used in a function called by the case statements. This would greatly shorten the switch in main to just directing the program flow to the needed functions.

Andy
Hello nurnisa,

After working on your program I have concluded that it makes no sense.

Even putting most of "main" in a do/while loop it has no use.

The program takes in information for 1 person and that is it. The main menu choice 1 and 2 seem backwards yet could work the way they are.

Case 3 for the delete is pointless because if you choose 1 or 2 you will just overwrite the information, so the delete is taken care of. A delete case or function tends to imply that you have more than 1 thing to choose from to be deleted. You do not have that right now.

You need to post the directions or instructions that you were given so that those reading this will know what you have to do and will be able to figure out what you have done wrong.

I feel that starting over and with better planning this would be much easier to work with.

A couple of points I would like to make:

Using (\t) in the middle of a string may seem easy, but be careful as the (\t) may not always tab things the same for each line.

You use to many "cout" and "endl"s in your program. "endl" is a function that takes time to execute. the more "endl"s that you have the longer the program will take to run. With the newer compilers the new line (\n) works just fine. Also a "cout" followed by a "cin" the "cin" will flush the output buffer before any input is taken. You can use this to your advantage.

In your code:
1
2
3
4
5
6
7
8
9
10
11
cout << "\nRETURN to main menu 1-Yes 2-No?" << endl;
cin >> mMenu;
switch (mMenu)
{
    case 1: cout << "\nReturning to main menu..." << endl;
        return main();
        break;
    case 2: cout << "\nExiting.." << endl;
        return 0;
        break;
}

This is a nice idea and unless it is for practice it can be shortened to:

1
2
3
4
5
6
7
8
9
cout << "\nRETURN to main menu 1-Yes 2-No? ";
cin >> mMenu;

if (mMenu == 2)
{
    cout << "\nExiting..\n";

    choice = 4;  // <--- For later use in the do/while condition.
}

The if you define "mMenu" as a "char" the prompt could end with just "Y / N". and line 4 could be changed to if (std::toupper(mMenu) == 'N'). To make sure that you are using only upper case letters. "std::toupper()" wil need the header file "<cctype>".

And for the code that precedes this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
hourBooked = endTime - startTime;

cout <<
    "\n"
    "_____________________________________________\n"
    "\t-BOOKING APPROVED BY THE SYSTEM-\n"
    "Name: " << firstname << ' ' << lastname << '\n' <<
    "Student ID: " << studentId << '\n' <<
    "\nGroup: " << group << '\n' <<
    "Room Name: ROOM " << room << '\n' <<
    //"Room type: TYPE " << typeRoom << '\n' <<  // <--- Should this be here. If not delete line.
    "Date: " << date << '\n' <<
    "Start Time > " << startTime << '\n' <<
    "End Time   > " << endTime << '\n' <<
    "Hours booked > " << hourBooked << " hr(s)\n"
    "_____________________________________________\n";

By moving "hourBooked" before the "cout" you can make just 1 big "cout" statement.

Andy
Topic archived. No new replies allowed.