I am new to programming and am having trouble with a school assignment. I need to make a class planner that prints out information after the user enters it and then askes for specific class numbers. The user enters the following information (in this order):
1. Name of Class
2. Class number
3. Meets on
4. Start time
5. End time
6. Teacher Name
7. Number of Students in Class

My problem is that the class has to be identified by the class number. I'd like to have the 'x' in the structured array be the class number or at least have the class information stored under the class number. Is this even possible? My code is as follows.

I greatly appreciate any help

cout<<"Class Name: ";
	cin>>planner[x][0].ClassName;
	cout<<endl;
	
	cout<<"Class Number: ";
	cin>>planner[x][1].ClassNum;
	cout<<endl;

	cout<<"Meets on: ";
	cin>>planner[x][2].ClassMeet;
	cout<<endl;

	cout<<"Start Time: ";
	cin>>planner[x][3].ClassStart;
	cout<<endl;

	cout<<"End Time: ";
	cin>>planner[x][4].ClassEnd;
	cout<<endl;

	cout<<"Teacher Name: ";
	cin>>planner[x][5].TeacherName;
	cout<<endl;

	cout<<"Number of Students in Class: ";
	cin>>planner[x][6].StudentNum;
	cout<<endl;

Recommended Answers

All 4 Replies

You're looking for something like this:

struct Class {
    string name;
    int    num;
    Day    meet;  // For Day use whatever is appropriate
    Time   start; // Ditto for Time
    Time   end;
    string teacher;
    int n  umStudents;
};

cin >> planner[x].name;
cin >> planner[x].num;

As for making the array index equal to the class number, that depends on how the classes are numbered. You don't want a bunch of missing elements in the array wasting space.

Thanks for replying so fast! The classes are supposed to be numbered as the user enters them. I just gave the code that I thought I was having trouble with. The reason I used two stacks was because I wanted the seven areas of information to stay together when it prints. The only way I could think of storing the class number in an easily retrievable way was by making 'x' the class number. My whole code so far is:

#include<iostream>
#include<string>
#include<stdlib.h>
#include<conio.h>
#include"myStructs.h"
using namespace std;

int Print();
int Enter(int x);

int x = 0;
bool errorCapture;
struct classPlanner
{
	string ClassName;
	int ClassNum;
	string ClassMeet;
	int ClassStart;
	int ClassEnd;
	string TeacherName;
	int StudentNum;
};

classPlanner planner[100][6];

int main()
{ 
	bool errorCapture = true;
	int iChoice;
	while(errorCapture == true)
	{
		cout<<"What do you want to do?"<<endl;
		cout<<"1. Enter New Data"<<endl;
		cout<<"2. Print Data to the Screen"<<endl;
		cout<<"3. Quit"<<endl;
		cin>>iChoice;
		switch(iChoice)
		{
		case 1:
			Enter(x);
			break;
		case 2:
			Print();
			break;
		case 3:
			errorCapture = false;
			break;
		default:
			errorCapture = true;
			cout<<"Please re-enter number"<<endl;
		}
	}
	cout<<"Press any key to quit...";
	_getch();
	return 0;
}
int Enter(int x)
{	
	cout<<"Class Name: ";
	cin>>planner[x][0].ClassName;
	cout<<endl;
	
	cout<<"Class Number: ";
	cin>>planner[x][1].ClassNum;
	cout<<endl;

	cout<<"Meets on ('MWF' if it meets on Monday, Wednesday, and Friday): ";
	cin>>planner[x][2].ClassMeet;
	cout<<endl;

	cout<<"Start Time: ";
	cin>>planner[x][3].ClassStart;
	cout<<endl;

	cout<<"End Time: ";
	cin>>planner[x][4].ClassEnd;
	cout<<endl;

	cout<<"Teacher Name: ";
	cin>>planner[x][5].TeacherName;
	cout<<endl;

	cout<<"Number of Students in Class: ";
	cin>>planner[x][6].StudentNum;
	cout<<endl;
	
	errorCapture = true;
	x++;
	return 1;
}
int Print()
{
	string NumOrAll;
	cout<<"Enter your class number or enter 'all' to print all: ";
	cin>>NumOrAll;
	if(NumOrAll == "all")
	{
		for(int i = 0; i <= x; i++)
		{
			cout<<"*****************************"<<endl;
			cout<<"Class Name: "<<planner[i][0].ClassName<<endl;
			cout<<"Class Number: "<<planner[i][1].ClassNum<<endl;
			cout<<"Meets on Day: "<<planner[i][2].ClassMeet<<endl;
			cout<<"Start Time: "<<planner[i][3].ClassStart<<endl;
			cout<<"End Time: "<<planner[i][4].ClassEnd<<endl;
			cout<<"Teacher Name: "<<planner[i][5].TeacherName<<endl;
			cout<<"Number of Students in Class: "<<planner[i][6].StudentNum<<endl;
		}
	}
	else
	{	
		int i = atoi(NumOrAll.c_str());
		cout<<"*****************************"<<endl;
		cout<<"Class Name: "<<planner[i][0].ClassName<<endl;
		cout<<"Class Number: "<<planner[i][1].ClassNum<<endl;
		cout<<"Meets on: "<<planner[i][2].ClassMeet<<endl;
		cout<<"Start Time: "<<planner[i][3].ClassStart<<endl;
		cout<<"End Time: "<<planner[i][4].ClassEnd<<endl;
		cout<<"Teacher Name: "<<planner[i][5].TeacherName<<endl;
		cout<<"Number of Students in Class: "<<planner[i][6].StudentNum<<endl;
	}
	errorCapture = true;
	return 1;
}

You don't need to do this: planner[x][2].ClassMeet Just this: planner[x].ClassMeet And planner should not be defined like this: classPlanner planner[100][6]; but like this: classPlanner planner[100]; Also, Print and Enter don't return anything, so make them void. Get rid of the global x variable, define it in main, and change Enter to accept a reference as well as change Print and Enter to accept a classPlanner array.

Also, if the class number is in fact equal to x, then don't ask for the class number (and get rid of the entry in the structure). Just store the other information at position x in the array, like you are doing.

So here's a skeleton:

void Print (classPlanner planner[], int x) {
}

void Enter (classPlanner planner[], int & x) {
}

int main() {
    classPlanner planner[100];
    int x = 0;
    //...
    Enter (planner, x);
    //...
    Print (planner, x);
    //...
}

Thanks! That helped a ton. I think I misunderstood things about arrays, but now I understand thanks to you.

It works with just a few modifications (I still had to ask for class number because the assignment mandated it; I just used a for loop to search the array).

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.