I am making a program for my week 1 programming II class. 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.

this is what I have so far...

#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <math.h>
#include <algorithm>

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;
}

my error is in my struct classroom class. It is not allowing me to have string students[number_of_students];

Can anyone explain to me why I can't have that or can someone show me a way to have this code working?

Thank You for your time. Merry Late Christmas and Happy New Year.

Recommended Answers

All 3 Replies

The reason you can't declare the students array the way you have it is because number_of_students is not constant; a statically-allocated array needs to have a constant size at declaration time.

Probably the easiest solution is the one you've already got in the structure. which is to use a vector<string> for the list of students. You can simply eliminate the existing students member, and use the one you currently have under the name student . The vector will automatically keep track of it's own size, which would be the same as the number of students in the class.

As an additional piece of advice, you're going to want to move the structure type declaration out of main() , as part of the requirements of the project is to write functions that manipulate the structure. You'll also need to be able to declare more than one variable of the structure type, as well.

This is what I have so far...

#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <math.h>
#include <algorithm>

using namespace std;

int main()
{
	struct classroom
	{
		int room_number, number_of_chairs, number_of_students;
		vector<string> student;
		string students, teacher, Lecture_Name;
		int windows, projectors, seats_available_for_new_students;
		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.teacher;
	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.projectors = 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.projectors;
		cout << endl;

		if (my_classroom.projectors <= 0)
		{
			cout << "Error. Try Again." << endl << 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 << 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.student.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_for_new_students = 0;
		}

		else if (chairs_minus_number_of_students > 0)
		{
			cout << "You have " << chairs_minus_number_of_students << " extra seats in the classroom." << endl << 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_for_new_students = 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 << endl;
					cout << "You have " << chairs_minus_number_of_students << " extra seats in the classroom." << endl << endl;
					cout << "Try Again." << endl << 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.student.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_minus_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 << 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 << 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.student.push_back(my_classroom.students);
			}
		}
	}

	cout << "You have " << my_classroom.student.size() << " students in your classroom." << endl << endl;

	system("pause");
	return 0;
}

Hoo boy, you have a bit of a mess here. Where to begin...

Well, first off, let's review the data structure itself. Right now you have a few things that are redundant, and some others that seem to indicate a misunderstanding of the problem. Let's simplify things a little by cutting the redundant information out:

struct classroom
{
	unsigned room_number, number_of_chairs;
	vector<string> students;
	string teacher, Lecture_Name;
	bool windows, projectors;
};

You didn't need a separate students string in addition to the vector of the students names; that gets cut, and student gets renamed to students for clarity. The number_of_students is the same as students.size() , so there is no need to keep a separate variable for it - you just risk running into a problem of the two getting out of sync, so it is better to use the existing functionality for this purpose. You can get whether there are seats available by comparing the number of seats to the number of students, so that doesn't need s separate variable, either. Finally, the problem statement given indicates that whether there are windows and whether there is a projector are both yes/no values, so it makes more sense to use a Boolean variable for each of those.

Next, let me repeat something I said earlier: you need to separate the structure declaration from the main() function. Not only is this just a good practice, it is necessary if you are to share the structure with other functions, something the problem statement specifically mentions. You need to be able to make functions like this one:

bool seatAvailable(classroom& cr)
{
    return (cr.number_of_chairs > cr.students.size());
}

Which brings us to the next issue: having the whole code in the main() function, and tightly coupled to a single structure variable ( myclassroom ). Again, this is not only a bad practice, it goes against the problem statement, which explicitly says that you will need to have at least two classroom variables which you'll need to be able to compare later on. You'll want to have the code for reading in the information separated out into one or more different functions.

I won't even comment on the excessive use of goto , save to say that each of those cases could be replaced with a while() loop, and be a lot clearer for it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.