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.
#include "iostream"
#include "string"
#include "vector"
#include "math.h"
#include "algorithm"
usingnamespace 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?
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>
usingnamespace 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:
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
usingnamespace 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;
}