I am having difficulties in figuring out the code for this program C++ visual st
ID: 3863227 • Letter: I
Question
I am having difficulties in figuring out the code for this program C++ visual studio.
Please Help.
THe code needs to be as simple as possible.
Your job is to create a reservation system for a restaurant. The restaurant has 20 tables. Here is the functionality required for this system (you may want to display a menu and let user choose options 1 to 4, make sure to put your program in a loop so program does not exit until user chooses menu 0):
1- Reserve a Table
User needs to input the table number (Tables are numbered from 0 to 19). If the table is not available (reserved), inform the user that the selected table is not available. If the table is available, then ask for the name (name is a single word, no space) and mark the table as reserved.
2- Clear Reservation
User needs to input the table number (Tables are numbered from 0 to 19). If the table is not reserved, inform the user that the selected table is already available (nothing to clear). If the table is reserved, mark it as available. That table is no longer reserved.
3- Report
Prints out the state of each table. Each table is printed on a separate line -- so your report will print out 20 rows. If reserved, it will print out the name on the reservation next to the table number. If available, it should print out "available".
0- Exit Program.
Each feature should be located inside its own function. I would recommend keeping two arrays (parallel arrays!), both are size 20, one of type boolean (true/false -- reserved/available) and the other one of type string (name on reservation if reserved, blank otherwise).
------------------------
Hints for the assignemnte.
for this assignment, you only need one array of size 20 -- because we have 20 tables. Let's call this array "tables":
string tables[20];
notice the data type -- it is string. For each table in tables, if the string value is empty (""), then the table is available (not reserved), otherwise the table is reserved and the value is the name of the person reserved the table. So at the begining of your program, make sure to initialize tables array so that all tables are available:
for(int i=0; i < 20; i++)
{
tables[i] = "";
}
Assume table numbers go from 0 to 19. Now if you want to know table #2 is available:
if(tables[2] == "")
{
cout << "Table 2 is available" << endl;
}
else
{
cout << "Table 2 is reserved to " << tables[2] << endl;
}
If you want to make table 3 available:
tables[3] = "";
if you want to reserve table 3 with name Chegg user:
tables[3] = "Chegg User";
Explanation / Answer
/*Restaurant.CPP:*/
#include<iostream>
using namespace std;
string tables[20];
bool status[20];
void reserve_table(int);
void clear_reservation(int);
void print_data();
int main()
{
int choice;
for(int i=0; i < 20; i++)
{
tables[i] = "";
status[i] = false;
}
do{
cout<<" Select one of the following:"<<endl;
cout<<"1.Reserve a Table 2.Clear Reservation 3.Report 0.Exit :";
cin>>choice;
int num;
switch(choice){
case 1:
cout<<" Enter table number to reserve:";
cin>>num;
reserve_table(num);
break;
case 2:
cout<<" Enter table number to clear reservation:";
cin>>num;
clear_reservation(num);
break;
case 3:
print_data();
break;
case 0:
cout<<" Exited";
}
}while(choice!=0);
return 0;
}
void reserve_table(int num){
if(tables[num] == "")
{
string pname;
cout << " Table "<<num<< " is available" << endl;
cout<<" Enter person name to reserve:";
cin>>pname;
tables[num] = pname;
status[num] = true;
cout << " Table "<<num<<" is reserved to " << tables[num] << endl;
}
else
{
cout << " Table "<<num<<" is reserved to " << tables[num] << endl;
}
}
void clear_reservation(int num){
if(tables[num] == "")
{
cout << " Table "<<num<<" is already available"<< endl;
}
else
{
tables[num] = "";
status[num] = false;
cout<< " Table "<<num<<" is cleared"<< endl;
}
}
void print_data()
{
cout<<" Table Status "<<endl;
cout<<"______ __________"<<endl;
for(int i=0;i<20;i++){
cout<<" Table "<<i<<" ";
if(status[i] == true)
cout<<tables[i];
else
cout<<"Available"<<endl;
}
}
/*Sample Input and Output */
Select one of the following:
1.Reserve a Table 2.Clear Reservation 3.Report 0.Exit :
1
Enter table number to reserve:
4
Table 4 is available
Enter person name to reserve:
Lakshman
Table 4 is reserved to Lakshman
Select one of the following:
1.Reserve a Table 2.Clear Reservation 3.Report 0.Exit :1
Enter table number to reserve:
6
Table 6 is available
Enter person name to reserve:
Mahesh
Table 6 is reserved to Mahesh
Select one of the following:
1.Reserve a Table 2.Clear Reservation 3.Report 0.Exit :
1
Enter table number to reserve:
5
Table 5 is available
Enter person name to reserve:
Manoj
Table 5 is reserved to Manoj
Select one of the following:
1.Reserve a Table 2.Clear Reservation 3.Report 0.Exit :
1
Enter table number to reserve:7
Table 7 is available
Enter person name to reserve:
Venky
Table 7 is reserved to Venky
Select one of the following:
1.Reserve a Table 2.Clear Reservation 3.Report 0.Exit :1
Enter table number to reserve:8
Table 8 is available
Enter person name to reserve:
Sravan
Table 8 is reserved to Sravan
Select one of the following:
1.Reserve a Table 2.Clear Reservation 3.Report 0.Exit :
3
Table Status
______ __________
Table 0 Available
Table 1 Available
Table 2 Available
Table 3 Available
Table 4 Lakshman
Table 5 Manoj
Table 6 Mahesh
Table 7 Venky
Table 8 Sravan
Table 9 Available
Table 10 Available
Table 11 Available
Table 12 Available
Table 13 Available
Table 14 Available
Table 15 Available
Table 16 Available
Table 17 Available
Table 18 Available
Table 19 Available
Select one of the following:
1.Reserve a Table 2.Clear Reservation 3.Report 0.Exit :
2
Enter table number to clear reservation:
5
Table 5 is cleared
Select one of the following:
1.Reserve a Table 2.Clear Reservation 3.Report 0.Exit :
2
Enter table number to clear reservation:
8
Table 8 is cleared
Select one of the following:
1.Reserve a Table 2.Clear Reservation 3.Report 0.Exit :
3
Table Status
______ __________
Table 0 Available
Table 1 Available
Table 2 Available
Table 3 Available
Table 4 Lakshman
Table 5 Available
Table 6 Mahesh
Table 7 Venky
Table 8 Available
Table 9 Available
Table 10 Available
Table 11 Available
Table 12 Available
Table 13 Available
Table 14 Available
Table 15 Available
Table 16 Available
Table 17 Available
Table 18 Available
Table 19 Available
Select one of the following:
1.Reserve a Table 2.Clear Reservation 3.Report 0.Exit :0
Exited
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.