NEED HELP PLEASE

i need help converting this code into functions

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
#include <string>
using namespace std;
int main()
{	
	// declare variables
	bool reserveStatus[20];
	string reserveName[20];
	
	// initialize the arrays
	for(int i = 0; i < 20; i++)
	{
		reserveStatus[i] = false;
		reserveName[i] = "none";
	}
	
	// this is the main loop of the program
	int choice = 0;
	while(choice != 4)
	{
		// display a nice menu
		cout << endl << endl;
		cout << "Please select one of the following options." << endl;
		cout << "1- Reserve a table" << endl;
		cout << "2- Clear reservation" << endl;
		cout << "3- Report" << endl;
		cout << "4- Exit program" << endl;
		cin >> choice;

		if (choice == 1) // user wants to reserve a table
		{				
			// get the table he/she wants to reserve
			int TableNum; 
			cout << "Please select a table number between 0 and 19. " << endl;
			cin >> TableNum;

			// see if it is already reserved
			if (reserveStatus[TableNum] == true) 
			{
				cout << "Sorry. The table you requested is already reserved."<< endl;
			}	
			else
			{
				string name; // table is available, let's get the reservation name
				cout << "There seems to be an open seat. What is your last name? "<< endl;
				cin >> name;
			
				// set the table as reserved and set the reservation name
				reserveStatus[TableNum] = true;
				reserveName[TableNum] = name;

				cout << "Okay, you have reserved a table. Congratulations."<< endl;
			}
		}
		else if (choice == 2)	// user want to cancel a reservation
		{
			int TableNum;
			cout << "Please select a table number between 0 and 19. "<< endl;
			cin >> TableNum;
			
			// see if the reservation is already clear on this table
			if (reserveStatus[TableNum] == false)
			{
				cout << "The table reservation you are trying to clear is already available." << endl;
			}	
			else
			{
				// otherwise, clear reservation status
				reserveStatus[TableNum] = false;
				cout << "Okay, the table reservation is now cleared." << endl;
			}
		}		
		else if (choice == 3)	
		{ 
			// report all reservations
			for(int i = 0; i < 20; i++)	// go through all tables 0...19
			{
				// see if the table is available
				if (reserveStatus[i] == false)
				{
					cout << "Table :" << i << " is available." << endl;
				}
				else 
				{
					cout << "Table :" << i << " is reserved by " << reserveName[i] << "." << endl;
				}
			}

		}
		else if (choice == 4)
		{
			break; // exit out of the main loop
		}
	}	
	
	system("PAUSE");
	return 0;
}
Last edited on
closed account (3qX21hU5)
Well you won't get answers unless you post something specific not just need help with converting to functions. For one we don't even know what you want converted, there are many ways to do it.

Show what you have done, what you are stuck on, what concepts you don't understand. Be thorough in you explanations. Don't just post what your teacher gave you and expect us to give you answers :p
Last edited on
this is what i got so far

this code gives you 4 options
the first option is suppose to reserve a table
the second option is suppose to clear a table
the third option is suppose to report if table is reserved or not
the fourth option is suppose exit the program

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#include <iostream>
#include <string>
using namespace std;
void ReserveTable();
void ClearTable();
void ReportTable();

int main()
{	
	// declare variables
	bool reserveStatus[20];
	string reserveName[20];
	
	// initialize the arrays
	for(int i = 0; i < 20; i++)
	{
		reserveStatus[i] = false;
		reserveName[i] = "none";
	}
	
	// this is the main loop of the program
	int choice = 0;
	while(choice != 4)
	{
		// display a nice menu
		cout << endl << endl;
		cout << "Please select one of the following options." << endl;
		cout << "1- Reserve a table" << endl;
		cout << "2- Clear reservation" << endl;
		cout << "3- Report" << endl;
		cout << "4- Exit program" << endl;
		cin >> choice;

		if (choice == 1) // user wants to reserve a table
		{				
			// get the table he/she wants to reserve


			// see if it is already reserved

		else if (choice == 2)	// user want to cancel a reservation
		
		else if (choice == 3)	
		
		else if (choice == 4)
		{
			break; // exit out of the main loop
		}
	}	
	
	system("PAUSE");
	return 0;
}

void ReserveTable()

{
			if (reserveStatus[TableNum] == true) 
			{
				cout << "Sorry. The table you requested is already reserved."<< endl;
			}	
			else
			{
				string name; // table is available, let's get the reservation name
				cout << "There seems to be an open seat. What is your last name? "<< endl;
				cin >> name;
			
				// set the table as reserved and set the reservation name
				reserveStatus[TableNum] = true;
				reserveName[TableNum] = name;

				cout << "Okay, you have reserved a table. Congratulations."<< endl;
			}
		}
}
void ClearTable()

			int TableNum; 
			cout << "Please select a table number between 0 and 19. " << endl;
			cin >> TableNum;
{
			{
			int TableNum;
			cout << "Please select a table number between 0 and 19. "<< endl;
			cin >> TableNum;
			
			// see if the reservation is already clear on this table
			if (reserveStatus[TableNum] == false)
			{
				cout << "The table reservation you are trying to clear is already available." << endl;
			}	
			else
			{
				// otherwise, clear reservation status
				reserveStatus[TableNum] = false;
				cout << "Okay, the table reservation is now cleared." << endl;
			}
		}
}

void ReportTable()

	{ 
			// report all reservations
			for(int i = 0; i < 20; i++)	// go through all tables 0...19
			{
				// see if the table is available
				if (reserveStatus[i] == false)
				{
					cout << "Table :" << i << " is available." << endl;
				}
				else 
				{
					cout << "Table :" << i << " is reserved by " << reserveName[i] << "." << endl;
				}
			}

		}
Last edited on
You're pretty close.

You need to pass the arrays with the function:
1
2
3
4
5
6
#include <iostream>
#include <string>
using namespace std
void ReserveTable(bool reserveStatus[], int choice);  // size is 20
void ClearTable(bool reserveStatus[], string reserveName[], int size);
void ReportTable(bool reserveStatus[], string reserveName[], int size);


And then call the functions:
1
2
3
4
5
6
7
8
9
if (choice == 1) // user wants to reserve a table
{				
  // get the table he/she wants to reserve

  cin >> choice;
      
  // see if it is already reserved
  // if (choice is in bounds)
      ReserveTable(reserveStatus, choice);


Last edited on
this is what i have now. why does it have so many errors? what did i do wrong?

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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include <string>
using namespace std;
void ReserveTable(bool reserveStatus[], int choice);  // size is 20
void ClearTable(bool reserveStatus[], string reserveName[], int size);
void ReportTable(bool reserveStatus[], string reserveName[], int size);

int main()
{	
	// declare variables
	bool reserveStatus[20];
	string reserveName[20];

	// initialize the arrays
	for(int i = 0; i < 20; i++)
	{
		reserveStatus[i] = false;
		reserveName[i] = "none";
	}

	// this is the main loop of the program
	int choice = 0;
	while(choice != 4)
	{
		// display a nice menu
		cout << endl << endl;
		cout << "Please select one of the following options." << endl;
		cout << "1- Reserve a table" << endl;
		cout << "2- Clear reservation" << endl;
		cout << "3- Report" << endl;
		cout << "4- Exit program" << endl;
		cin >> choice;

		if (choice == 1) // user wants to reserve a table
		{				

			cin >> choice;
			ReserveTable(reserveStatus, choice);
		}

		else if (choice == 2)	// user want to cancel a reservation

		{
			ClearTable(reserveStatus, reserveName, size);
		}

		else if (choice == 3)

		{
			ReportTable(reserveStatus, reserveName, size);
		}

		else if (choice == 4)
		{
			break; // exit out of the main loop
		}
	}	

	system("PAUSE");
	return 0;
}

void ReserveTable()

{
	if (reserveStatus[TableNum] == true) 
	{
		cout << "Sorry. The table you requested is already reserved."<< endl;
	}	
	else
	{
		string name; // table is available, let's get the reservation name
		cout << "There seems to be an open seat. What is your last name? "<< endl;
		cin >> name;

		// set the table as reserved and set the reservation name
		reserveStatus[TableNum] = true;
		reserveName[TableNum] = name;

		cout << "Okay, you have reserved a table. Congratulations."<< endl;
	}
}
}
void ClearTable()

	int TableNum; 
cout << "Please select a table number between 0 and 19. " << endl;
cin >> TableNum;
{
	{
		int TableNum;
		cout << "Please select a table number between 0 and 19. "<< endl;
		cin >> TableNum;

		// see if the reservation is already clear on this table
		if (reserveStatus[TableNum] == false)
		{
			cout << "The table reservation you are trying to clear is already available." << endl;
		}	
		else
		{
			// otherwise, clear reservation status
			reserveStatus[TableNum] = false;
			cout << "Okay, the table reservation is now cleared." << endl;
		}
	}
}

void ReportTable()

{ 
	// report all reservations
	for(int i = 0; i < 20; i++)	// go through all tables 0...19
	{
		// see if the table is available
		if (reserveStatus[i] == false)
		{
			cout << "Table :" << i << " is available." << endl;
		}
		else 
		{
			cout << "Table :" << i << " is reserved by " << reserveName[i] << "." << endl;
		}
	}

}
Last edited on
Topic archived. No new replies allowed.