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.
If there are 10 students and 15 chairs then the utilization is 66.666 % Hint: vectors have .size()

5
Contributors
12
Replies
21
Views
5 Years
Discussion Span
Last Post by james.opdyckeii

Show us what you have done and we can help you if you state what problems you run into.

We will not just do your homework for you.

#include "iostream"
#include "string"
#include "vector"
#include "cmath"
#include "math.h"
#include "algorithm"
#include "stdafx.h"

using namespace std;

int main()
{
struct classroom
{
int room_number, number_of_chairs;
int number_of_students;
vector<string> student;
string students[number_of_students];
string Lecture_name;
} my_classroom;
cout << "What is your Classroom Number? ";
cin >> my_classroom.room_number;
cout << endl << endl;
cout << "How many chairs are in your classroom? ";
cin >> my_classroom.number_of_chairs;
cout << endl << endl;
cout << "How many students are there? ";
cin >> my_classroom.number_of_students;
cout << endl << endl;
//my_classroom.student.push_back(students[0]); This is what I am trying to do.
system("pause");
return 0;
}

Inline Code Example Here

First thing you should do is not create your structure within main(). Then you should look at what data types need to be inside of the structure, you have all but the three boolean variables that were listed above. Also you should choose if you want to use a vector or an array for holding the list of students, not both, I would suggest a vector because the number of students varies and if you were to do it with arrays you would need to allocate memory.

struct classroom
{
int room_number, number_of_chairs, number_of_students;
string Lecture_name;
vector<string> students;
bool window, projector, available;
};

To populate the array you need to make a for() loop that goes goes from 0 to the number of students.

Here is what you can do for getting the students names.

cout << "Please enter the name of each student: " << endl;
string student_name;

//used to ignore the newline char left by reading in numbers
cin.ignore(numeric_limits<streamsize>::max(), '\n'); //need to include the header <limits>
for( int i = 0; i < my_classroom.number_of_students; i++ )
{
cout << i+1 << ") ";
getline(cin, student_name);
my_classroom.students.push_back(student_name);
}

The functions that you need to write later are just going to be taking in two classrooms and then comparing values set in each class.

Is this correct?

#include "iostream"
#include "string"
#include "vector"
#include "cmath"
#include "math.h"
#include "algorithm"
#include "stdafx.h"

using namespace std;

int main()
{
struct classroom
{
int room_number, number_of_chairs, number_of_students;
string Lecture_name;
vector<string> students;
bool windows, projector,seats_available;
string answer1, answer2, answer3, answer4;
int answer5, answer6;

} my_classroom;

cout << "What is your Classroom Number? ";
cin >> my_classroom.room_number;
cout << endl;

cout << "who is your teacher?" ;
cin >> my_classroom.room_number;
cout << endl;

cout << "what is the name of today's lecture?" ;
cin >> my_classroom.Lecture_name;
cout << endl;

cout << "how many chairs are in your classroom?";
cin >> my_classroom.number_of_chairs;
cout << endl;

cout << "are there any windows in your classroom? (y/n)";
cin >> my_classroom.answer1;
cout << endl;

if ( my_classroom.answer1 == "no", my_classroom.answer1 == "No", my_classroom.answer1 == "NO", my_classroom.answer1 == "n", my_classroom.answer1 == "N")
{
my_classroom.windows = 0;
}

else if (my_classroom.answer1 == "yes", my_classroom.answer1 == "Yes", my_classroom.answer1 == "YES", my_classroom.answer1 == "y", my_classroom.answer1 == "Y")

{
a: cout << "How many windows do you have in the classroom?";
cin >> my_classroom.windows;
cout << endl;

if (my_classroom.windows <= 0)
{
cout << "Error. Try Again." << endl << endl;
system ("pause");
system ("cls");
goto a;
}

cout << "Are there any projectors in your classroom? (y/n)";
cin >> my_classroom.answer2;
cout << endl;

if (my_classroom.answer2 == "no", my_classroom.answer2 == "No", my_classroom.answer2 == "NO", my_classroom.answer2 == "n", my_classroom.answer2 == "N")
{
my_classroom.projector = 0;
}

else if (my_classroom.answer2 == "yes", my_classroom.answer2 == "Yes", my_classroom.answer2 == "YES", my_classroom.answer2 == "y", my_classroom.answer2 == "Y")
{
b: cout << "How many projectors are there?" ;
cin >> my_classroom.projector;
cout << endl;

if (my_classroom.projector <= 0)
{
cout << "Error. Try Again." << endl;
system ("pause");
system ("cls");
goto b ;
}

}
c: cout << "how many students are there?" ;
cin >> my_classroom.number_of_students;
cout << endl;

if (my_classroom.number_of_students > my_classroom.number_of_chairs)
{
cout <<"you have" << my_classroom.number_of_chairs << "number of seats available in the classroom" << endl;
cout << "Try Again" << endl;
system ("pause");
system ("cls");
goto c;
}
else if (my_classroom.number_of_students <= my_classroom.number_of_chairs)
{
for (int i = 0; i < my_classroom.number_of_students; i++)
{
cout << "student name:";
cin >> my_classroom.students;
cout << endl;
my_classroom.students.push_back (my_classroom.students);
}
}
cout << "Are there any seats available for extra students?";
cin >> my_classroom.answer3;
cout << endl;

if (my_classroom.answer3 == "no", my_classroom.answer3 == "No", my_classroom.answer3 == "NO", my_classroom.answer3 == "n", my_classroom.answer3 == "N")
{
int chairs_minus_number_of_students = (my_classroom.number_of_chairs - my_classroom.number_of_students) ;
if (chairs_minus_number_of_students == 0)
{
my_classroom.seats_available = 0;
}

else if (chairs_minus_number_of_students > 0)
{
cout << "you have" << chairs_minus_number_of_students << "extra seats in the classroom." << endl;
cout << "Do you want to add any students?";
cin >> my_classroom.answer4;
cout << endl;

if (my_classroom.answer4 == "no", my_classroom.answer4 == "No", my_classroom.answer4 == "NO", my_classroom.answer4 == "n", my_classroom.answer4 == "N")
{
my_classroom.seats_available = chairs_minus_number_of_students;
}

else if (my_classroom.answer4 == "yes", my_classroom.answer4 == "Yes", my_classroom.answer4 == "YES", my_classroom.answer4 == "y", my_classroom.answer4 == "Y")
{
d: cout << "How many students do you want to add?";
cin >> my_classroom.answer5;
cout << endl;

if (my_classroom.answer5 > chairs_minus_number_of_students)
{
cout << "Too many students. Not enough seats available." << endl;

cout << "Try Again." << endl;
system ("pause");
system ("cls");
goto d;
}
else if (my_classroom.answer5 <= chairs_minus_number_of_students)
{
for (int i = my_classroom.answer5; i <chairs_minus_number_of_students; i++)
{
cout << "Student Name:";
cin >> my_classroom.students;
cout << endl;
my_classroom.students.push_back(my_classroom.students);
}
}
}
}
else if (my_classroom.answer3 == "yes", my_classroom.answer3 == "Yes", my_classroom.answer3 == "YES", my_classroom.answer3 == "y", my_classroom.answer3 == "Y")
{
int chairs_minuse_number_of_students = (my_classroom.number_of_chairs - my_classroom.number_of_students);
e: cout << "You have" << chairs_minus_number_of_students << "available seats left in the classroom."<< endl;

cout << "how many students do you wish to add?";
cin >> my_classroom.answer6;
cout << endl;

if (my_classroom.answer6 <= 0)
{
cout << "Error. Try Again." << endl;
system ("pause");
system ("cls");
goto e;
}
else if (my_classroom.answer6 > 0)
{
for (int i = my_classroom.answer6 ; i < chairs_minus_number_of_students; i++)
{
cout << "Student Name:" ;
cin >> my_classroom.students;
cout << endl;
my_classroom.students.push_back (my_classroom.students);
}
}
}
cout << "You have" << my_classroom.students.size () << "students in your classroom." << endl ;

system ("pause");
return 0;
}

You've definitely created logic of sorts for doing the tasks in the question, but the logic isn't encapsulated in any functions. The question definitely says that you should write functions to add data to the class room's attributes, etc.

Also, you should avoid the use of goto in your code---you certainly don't need it here. In general, you never need to use goto, except in the case, maybe, of some highly speed-optimised code.

I do not know how to do that. Can you help

I do not know how to do that. Can you help

Certainly.

You may not realize it yet, but you do know how to write functions, or at least one special function, the main() function. main() is a special case, however, as it does a lot of extra things behind the scenes when the program begins, things which are system and compiler dependent. Also, the main() function has a specific signature, which should always be used with it.

You have also used many functions in the past. In C++, much of what may seem to be part of the language - system(), cout, cin, the string class - are actually part of the standard library rather than the core language. Any time you use one of the functions or operators associated with them, you are calling a function. So the principle is, at least somewhat, familiar to you.

As for writing functions to work with your structure type, the fist thing you need to do (as sfuo pointed out earlier) is move the struct definition out of the main() function. This will make the structure type visible to other functions, allowing them to use it themselves.

Then next step is to break the exsting program down into pieces. For example, we can take the part where you are getting the roo number, and make a simple function out of it:

int get_room_number(classroom& cls)
{
cout << "What is your Classroom Number? ";
cin >> cls.room_number;
cout << endl;

return cls.room_number;
}

You can then call this function in your main() function like so:

get_room_number(my_classroom);

You can do the same for each of the other simple cases where you are getting values from the user. A slightly more complex example would be reading in the number of windows:

int get_windows(classroom& cls)
{
string answer;

cout << "are there any windows in your classroom? (y/n)";
cin >> answer;
cout << endl;

if (toupper(answer[0]) == 'Y')
{
while (true)
{
cout << "How many windows do you have 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;
}

You will note a number of changes I have made to your code, such as replacing the goto and label with a while() statement. At this point, we would have a good beginning for your program:

#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <cctype>

using namespace std;

struct classroom
{
int room_number, number_of_chairs, number_of_students;
string teacher, Lecture_name;
vector<string> students;
bool windows, projector;
};

int get_room_number(classroom& cls);
string get_teacher(classroom& cls);
string get_lecture_name(classroom& cls);
int get_chairs(classroom& cls);
int get_windows(classroom& cls);

int main()
{
classroom my_classroom;

get_room_number(my_classroom);
get_teacher(my_classroom);
get_lecture_name(my_classroom);
get_chairs(my_classroom);
get_windows(my_classroom);

return 0;
}

int get_room_number(classroom& cls)
{
cout << "What is your Classroom Number? ";
cin >> cls.room_number;
cout << endl;

return cls.room_number;
}

string get_teacher(classroom& cls)
{
cout << "who is your teacher? " ;
cin >> cls.teacher;
cout << endl;

return cls.teacher;
}

string get_lecture_name(classroom& cls)
{
cout << "what is the name of today's lecture? " ;
cin >> cls.Lecture_name;
cout << endl;

return cls.Lecture_name;
}

int get_chairs(classroom& cls)
{
cout << "how many chairs are in your classroom? ";
cin >> cls.number_of_chairs;
cout << endl;

return cls.number_of_chairs;
}

int get_windows(classroom& cls)
{
string answer;

cout << "are there any windows in your classroom? (y/n) ";
cin >> answer;
cout << endl;

if (toupper(answer[0]) == 'Y')
{
while (true)
{
cout << "How many windows do you have 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;
}

Hey I still have trouble understanding it alittle and I still have to put add projectors and student name how do I do that?

Does anyone know about Memory Maps?

Does anyone know about Memory Maps?

This is a different question; make a new thread for it. You might also want to include a little more detail in the new thread!

Hey I still have trouble understanding it a little and I still have to
put add projectors and student name how do I do that?

What parts are you confused by? Can you try to work it out from what I've given you? I can explain more if you need me to, but I would like to know what you are having trouble with first.

As for the projectors function, it should work almost the same as the function for gtting the number of windows. Look over that function carefully and try to see how you cuold modify a copy of it to work for the projectors question.

I am working on the same assignment and I am having an issue where the code compiles with no errors but immediately after the very last function I have listed, which is the one for getting the available seats, the program ends and closes. I modified the code listed here just a little to suit my needs. It asks for room number first, then class name, then seats available and closes after that.

#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <cctype>

using namespace std;

struct classroom
{
int roomNumber, numberStudents, numberChairs;
string className, projector;
vector<string> students;
bool window, available;
};

int getRoomNumber(classroom& cls);
string getClassName(classroom& cls);
string getProjector(classroom& cls);
int getAvailable(classroom& cls);
int getChairs(classroom& cls);
int getWindows(classroom& cls);

int main()
{
classroom myClassroom;

getRoomNumber(myClassroom);
getClassName(myClassroom);
getAvailable(myClassroom);
getChairs(myClassroom);
getWindows(myClassroom);
getProjector(myClassroom);

return 0;
}

int getRoomNumber(classroom& cls)
{
cout << "Enter room number: ";
cin >> cls.roomNumber;
cout << endl;

return cls.roomNumber;
}

string getClassName(classroom& cls)
{
cout << "Enter class name: ";
cin >> cls.className;
cout << endl;

return cls.className;
}

string getProjector(classroom& cls)
{
cout << "Is room available? Enter Y or N: ";
cin >> cls.projector;
cout << endl;

return cls.projector;
}

int getChairs(classroom& cls)
{
cout << "Enter number of chairs: ";
cin >> cls.numberChairs;
cout << endl;

return cls.numberChairs;
}

int getWindows(classroom& cls)
{
string answer;

cout << "Are there windows? Enter Y or N: ";
cin >> answer;
cout << endl;

if (toupper(answer[0]) == 'Y')
{
while (true)
{
cout << "Enter number of windows: ";
cin >> cls.window;
cout << endl;
if (cls.window > 0)
{
break;
}
else
{
cout << "Invalid entry. Try again. " << endl << endl;
}
}
}
else
{
cls.window = 0;
}

return cls.window;
}

int getAvailable(classroom& cls)
{
string answer;

cout << "Are there available seats? Enter Y or N: ";
cin >> answer;
cout << endl;

if (toupper(answer[0]) == 'Y')
{
while (true)
{
cout << "Enter number of available seats: ";
cin >> cls.available;
cout << endl;
if (cls.available > 0)
{
break;
}
else
{
cout << "Invalid entry. Try again. " << endl << endl;
}
}
}
else
{
cls.available = 0;
}

return cls.available;
}

I also need to add a bit where it compares two classrooms based on their size (size being the number of chairs) and then output which is larger. They also need to be compared based on utilization. Example: If there are 10 students and 15 chairs then the utilization is 66.666%

Thanks for any help possible!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.