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