I am doing right in C++?

I solved a problem given in my C++ book. The problem is given in this image https://i.imgsafe.org/de2f3f9.png . Am I doing right or 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

#include <iostream>
using namespace std;
int main()
{
	const int MAX_PEO = 250; // Maximum people in a meeting
	int num_p, rem_p;
	char again;



	do {
	    cout << "Enter the number of people in your meeting in a single room:- ";
	    cin >> num_p;
		if (num_p <= 250)
		{
			cout << "  \n";
			cout << "You have entered " << num_p << " number of people in meeting room.\n";
		        cout << "Oky, this room is safe for meeting accourding to Fire Laws.\n";
			cout << "   \n";
			rem_p = 250 - num_p;
			cout << "Remaining people you can call are " << rem_p << endl;
		}
		else
		{
			cout << "  \n";
			cout << "No this room is not safe for meeting\n";
			cout << "Select anyother room\n";
		}
		cout << "   \n";
		cout << "Do you want to try again?\n";
		cout << "Tpre y for Yes and n for No.\n";
		cin >> again;
		cout << "  \n";
	} while ( (again == 'y') || (again == 'Y'));

	return 0;
}
Last edited on
Just two minor mistakes. First you can and should use the MAX_PEO constant you have defined earlier in place of 250. Secondly in the else condition you must also tell the user that how many people are over the limit and if they leave the meeting is allowed to be conducted. Just add the code

1
2
rem_p = num_p - MAX_PEO;
cout << rem_p << " people must leave the room." << endl;
That's the right idea, but there are a few things you can do to improve your code.

You set MAX_PEO to 250 but you don't use it in the code. Change all other instances of "250" to MAX_PEO. The goal here is that if you want to change the size of the room, you should only have to change MAX_PEO.

Lines 26-27: the assignment says you should print the number of additional people who can attend the meeting, you aren't doing this. In general, I recommend carefully re-reading the assignment when you're done to make certain you've done everything required.
Corrected all mistakes.
Now its right?
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

#include <iostream>
using namespace std;
int main()
{
	const int MAX_PEO = 250; // Maximum people in a meeting for safly work
	int num_p, rem_p, peo_leave, end_p;
	char again;



	do {
	    cout << "Enter the number of people in your meeting in a single room:- ";
	    cin >> num_p;
		if (num_p <= MAX_PEO)
		{
			cout << "  \n";
			cout << "You have entered " << num_p << " number of people in meeting room.\n";
		        cout << "Oky, this room is safe for meeting accourding to Fire Laws.\n";
			cout << "   \n";
			rem_p = MAX_PEO - num_p;
			cout << "Remaining people you can call are " << rem_p << endl;
		}
		else
		{
			peo_leave = num_p - MAX_PEO;
			cout << "  \n";
			cout << "No this room is not safe for meeting\n";
			cout << peo_leave << " People should leave or select anyother room\n";
		}
		cout << "   \n";
		cout << "Do you want to try again?\n";
		cout << "Tpre y for Yes and n for No.\n";
		cin >> again;
		cout << "  \n";
	} while ( (again == 'y') || (again == 'Y'));

	cout << "Type End then pres return key to quit from program\n";
	cin >> end_p;

	return 0;
}
Last edited on
But don't your instructions say "The program will read in the maximum room capacity and the number of people attending the meeting."?

Also in C++ it is normal to define variables close to first use instead of a big glob at the beginning of the function.

Last edited on
#Jilb Yah I know, but I created it in other sense, mean if you already know a room can accommodate 250 people then who will use this software, simply call 250 people. But for example if you are arranging a meeting in a hotel then your are encountering with a hotel's software in which maximum people variable is already listed mean a hotel's room can accommodate only 250 people and it is not allowed to call more than 250 people in hotel's room.
Last edited on
mean if you already know a room can accommodate 250 people then why will use this software,

How do you know, before you ask the proprietor, how many people the meeting room can occupy?

What happens if the room can only occupy 100 people? 50 people?

According to the instructions you should be entering the maximum room capacity not using some random constant value.
#Jlb you are not my point on which I am thinking.
The instructions clearly state, "The program will read in the maximum room capacity and the number of people to attend the meeting". That is two pieces of data to be read as input. Not sure why this is being ignored.
Oky oky as every one saying so now? #Jib and #Chervil

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

#include <iostream>
using namespace std;
int main()
{
	int MAX_PEO, num_p, rem_p, peo_leave, end_p;
	char again;



	do {
		cout << "Enter the maximum number of people in a room according to Fire Laws:-\n";
		cin >> MAX_PEO;
	        cout << "Enter the number of people in your meeting in a single room:- ";
	        cin >> num_p;
		if (num_p <= MAX_PEO)
		{
			cout << "  \n";
			cout << "You have entered " << num_p << " number of people in meeting room.\n";
		        cout << "Oky, this room is safe for meeting according to Fire Laws.\n";
			cout << "   \n";
			rem_p = MAX_PEO - num_p;
			cout << "Remaining people you can call are " << rem_p << endl;
		}
		else
		{
			peo_leave = num_p - MAX_PEO;
			cout << "  \n";
			cout << "No this room is not safe for meeting\n";
			cout << peo_leave << " People should leave or select anyother room\n";
		}
		cout << "   \n";
		cout << "Do you want to try again?\n";
		cout << "Tpre y for Yes and n for No.\n";
		cin >> again;
		cout << "  \n";
	} while ( (again == 'y') || (again == 'Y'));

	cout << "Type End then pres return key to quit from program\n";
	cin >> end_p;

	return 0;
}
There is nothing really wrong with your code, but how about something like:
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
#include <iostream>
#include <cctype>

using namespace std;

int main()
{
    char again;
	
	do {
        int max_people, people_present;
		cout << "Enter the maximum number of people in a room according to Fire Laws: ";
		cin >> max_people;
        cout << "Enter the number of people in your meeting in a single room: ";
        cin >> people_present;

		if (people_present <= max_people)
		{
			cout << "\nYou have entered " << people_present << " number of people in meeting room.\n";
            cout << "Okay, this room is safe for meeting according to Fire Laws.\n\n";
			int remaining_people = max_people - people_present;
			cout << "Remaining people you can call are " << remaining_people << endl;
		}
		else
		{
			int people_to_leave = people_present - max_people;
			cout << "  \n";
			cout << "No this room is not safe for meeting\n";
			cout << people_to_leave << " people should leave or select another room\n";
		}

		cout << "\nDo you want to try again? (Y/N): ";
		cin >> again;
	} while ( toupper(again) == 'Y');

	return 0;
}


IMO this version has more meaningful variable names and the variables are declared closer to the area of first use.
Last edited on
#Jilb Good idea...
Topic archived. No new replies allowed.