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.

So far what i've done.
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 answer1, answer2, answer3, answer4;				// String variables for calculations
		int answer5, answer6;									// Integer variables
	};
	
	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
	int get_windows(classroom& cls);			// Windows Function
	int 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 #? ";
			cin >> cls.room_number;
			cout << endl;
			
			return cls.room_number;
		}
		
	string get_Class_name(classroom& cls)
		{
			cout << "What is your Class Name? ";
			cin >> cls.Class_name;
			cout << endl;
			
			return cls.Class_name;
		}
	
	int get_number_of_chairs(classroom& cls)
		{
		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)
		{
		cout << "How many students are in the class? ";
		cin >> cls.number_of_students;
		cout << endl;
		
		return cls.number_of_students;
		}
			
	int get_windows(classroom& cls)
		{
		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;
		}
		int 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 presume I want to make a function to continue adding or stopping?
One thing I immediately noticed is that the properties windows and projectors are both boolean values and yet you treat them as integers
I see you wish to create functions to do this, but are you not allowed to do this with methods instead (i.e. functions that are members of 'classroom' itself)? In the meantime, consider changing the top of your file to something like this. Basically, I've updated the #includes and your struct definition to remove extraneous variables (and add one for Available)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>

using namespace std;

struct classroom // builds the structure of the classroom
{
    int room_number, number_of_chairs;          //Integer Variables for Classroom Structure
    string Class_name;                          // Lecture Name Variable
    vector<string> student_names;               // Collection of student names
    bool hasWindow, hasProjector, isAvailable;  // Variables for window, projector, room availability
};


If you are going to use functions, they are all going to look pretty similar. Actually, your user input code looks the same in a lot of places. This "smells" like a spot where we could generalize stuff into functions. Check out this code:
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
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>

using namespace std;

struct classroom // builds the structure of the classroom
{
    int room_number, number_of_chairs;          //Integer Variables for Classroom Structure
    string Class_name;                          // Lecture Name Variable
    vector<string> student_names;               // Collection of student names
    bool hasWindow, hasProjector, isAvailable;  // Variables for window, projector, room availability
};

// prompt the user and get an integer input from cin
// (warning: no error checking in here!)
int getIntegerInput(string prompt)
{
    int returnValue;
    cout << prompt;
    cin >> returnValue;
    return returnValue;
}

// set the room number of classroom c to newRoomNumber
void changeClassRoomNumber(classroom &c, int newRoomNumber)
{
    c.room_number = newRoomNumber;
}

// set the number of chairs in classroom c to newNumChairs
void changeNumberOfChairs(classroom &c, int newNumChairs)
{
    c.number_of_chairs = newNumChairs;
}

int main()
{
    classroom myClassRoom;

    int newClassNumber = getIntegerInput("What is your Class #? ");
    changeClassRoomNumber(myClassRoom, newClassNumber);
    int newNumOfChairs = getIntegerInput("How many chairs are in the classroom? ");
    changeNumberOfChairs(myClassRoom, newNumOfChairs);
    //or, alternatively
    changeClassRoomNumber(myClassRoom, getIntegerInput("What is your Class #? "));
    changeNumberOfChairs(myClassRoom, getIntegerInput("How many chairs are in the classroom? "));
    return 0;
}
Last edited on
Topic archived. No new replies allowed.