Classroom Structure

The assignment is Create a structure for a classroom. Make sure it includes the following:

Room Number,
Lecture Name,
List of Students,
Number of chairs,
Window (Yes/No),
Projector (Yes/No),
Available(Yes/No).

Instructions:

Create functions that allow you to add data to the attributes of the classroom.
Create functions that allow you to print out the classroom information.
Compare two classrooms based on the size (number of chairs) and report which classroom is larger. Also compare two classrooms base on utilization.

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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#include "iostream"
#include "string"
#include "vector"
#include "math.h"
#include "algorithm"


using namespace std;


	struct classroom // builds the structure of the classroom
	{
		int room_number, number_of_chairs, number_of_students;	//Integer Variables for Classroom Structure
		string Class_name;										// Class Name Variable
		vector<string> student_name;							// Student Name Vector
		bool windows, projectors;								// Voolean variable for Window or Projector
	};
	
	string get_Class_name(classroom& cls);  	// Class Name Function
	int get_room_number(classroom& cls);		// Room Number Function
	int get_number_of_students(classroom& cls); // Number of Students Function
	int get_number_of_chairs(classroom& cls);	// Number of Chairs Function
	bool get_windows(classroom& cls);			// Windows Function
	bool get_projectors(classroom& cls);			// Projectors Function
	
	int main()
	
	{
		classroom my_classroom;
		
		get_Class_name(my_classroom);			// Calling the function
		get_room_number(my_classroom);			// Calling the function
		get_number_of_chairs(my_classroom);		// Calling the function
		get_number_of_students(my_classroom);	// Calling the function
		get_windows(my_classroom);				// Calling the function
		get_projectors(my_classroom);			// Calling the function
		
		return 0;
	}
	
	int get_room_number(classroom& cls)
		{
			cout << "What is your Class #? ";	// Room Number function
			cin >> cls.room_number;
			cout << endl;
			
			return cls.room_number;
		}
		
	string get_Class_name(classroom& cls)		// Room Name function
		{
			cout << "What is your Class Name? ";
			cin >> cls.Class_name;
			cout << endl;
			
			return cls.Class_name;
		}
	
	int get_number_of_chairs(classroom& cls)	// Number of Chairs function
		{
		cout << "How many chairs are in the classroom? ";
		cin >> cls.number_of_chairs;
		cout << endl;
		
		return cls.number_of_chairs;
		}
	
	int get_number_of_students(classroom& cls)	// Number Of Students function
		{
		cout << "How many students are in the class? ";
		cin >> cls.number_of_students;
		cout << endl;
		
		return cls.number_of_students;
		}
			
	bool get_windows  (classroom& cls)				// Windows function
		{
		string answer;
		
		cout << "Are there any windows in the classroom? (y/n) ";
		cin >> answer;
		cout << endl;
		
		if (toupper(answer[0]) == 'Y')
		{
			while (true)
			
			{
				cout << "How many windows are in the classroom? ";
				cin >> cls.windows;
				cout << endl;
				if (cls.windows > 0)
				{
					break;
				}
				else
				{
					cout << "Error.  Try again. " << endl  << endl;
				}
			}
		}
		else
		{
			cls.windows = 0;
		}
		
		return cls.windows;
		}
		bool get_projectors(classroom& cls)
		{
		string answer;
		
		cout << "Are there any projectors in the classroom? (y/n) ";
		cin >> answer;
		cout << endl;
		
		if (toupper(answer[0]) == 'Y')
		{
			while (true)
			
			{
				cout << "How many projectors are in the classroom? ";
				cin >> cls.projectors;
				cout << endl;
				if (cls.projectors > 0)
				{
					break;
				}
				else
				{
					cout << "Error.  Try again. " << endl  << endl;
				}
			}
		}
	
		else
		{
			cls.projectors = 0;
		}
		
		return cls.projectors;
		}
	
	
	
	


I've created the functions for adding information to the attributes of the classroom.

I'm having trouble with the vector and adding the students name into an empty vector.
(1)
You left off the attribute for "available".

Your letter case is inconsistent ("Class_name" and "room_number", for example). Pick one or the other.

The "number of students" is duplicate information. Since you have a vector with the student names, you also know how many students there are -- it is the same as the size() of the vector.

I'll get back to that in a moment.

Watch your indentation.


(2)
I am always bothered by solutions where user input is mixed into modifiers, and also by the names you used for the operation. This is a personal issue, but it is due to what people understand about your code based upon naming.

A name like "get foo" suggests that you return foo from the struct/class -- it is surprising that it communicates to the user.


(3)
Your assignment does not ask for user input. So, IMHO, you should either get rid of it or move it to main:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int main()
{
	classroom my_classroom;
	string    s;
	int       n;

	cout << "What is your class name? ";
	getline( cin, s );
	set_class_name( my_classroom, s );

	cout << "What is your class room number? ";
	cin >> n;
	cin.ignore( 1000, '\n' );
	set_room_number( my_classroom, n );

	...
}
1
2
3
4
5
6
7
8
int main()
{
	classroom my_classroom;

	set_class_name(  my_classroom, "Biomechanics I" );
	set_room_number( my_classroom, 201 );
	...
}


(4)
To add multiple values to a vector, use a loop. Quit when an empty line is entered:

1
2
3
4
5
6
7
8
vector <string> names;
string s;
cout << "Enter student names, one per line. Press Enter twice to finish.\n> ";
while (getline( cin, s ) && !s.empty())
{
	names.push_back( s );
	cout << "> ";
}

When that is done, you have all the student names and you know how many students there are.


(5) You are required to compare two classrooms two different ways. That means you will need two classrooms.

1
2
3
4
5
int main()
{
	classroom classroom1;
	classroom classroom2;
	...
1
2
3
4
int main()
{
	classroom classrooms[ 2 ];
	...

You will need functions to compare the classrooms -- one for each method

1
2
3
4
5
bool is_classroom_larger( const classroom& A, const classroom& B )
{
	// return true if A has more chairs than B
	// return false otherwise
}
1
2
3
4
5
6
7
8
9
bool is_classroom_more_utilized( const classroom& A, const classroom& B )
{
	// By 'utilization' I assume your professor means that more chairs are 
	// filled. You will have to compute some percentages here:
	//   student_names.size() / (double)number_of_chairs

	// Don't forget that explicit cast to double -- it is important.
	// Compare the ratios. The larger number is more utilized.
}

Finally, don't forget to make a function to print your class information:

1
2
3
4
void print_classroom( const classroom& A )
{
	...
}

Hope this helps.
Thanks much.

The assignment does ask for the ability to add data to the attributes so I presume that is adding information to the functions.
Topic archived. No new replies allowed.