Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,252 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,155 people online right now. Registration is fast and FREE... Join Now!




Making a Gradebook with Structures and Classes

 
Reply to this topicStart new topic

Making a Gradebook with Structures and Classes, My vectors are giving me problems inside my structures.

derbear
post 1 Apr, 2008 - 06:59 PM
Post #1


New D.I.C Head

*
Joined: 1 Apr, 2008
Posts: 3


My Contributions


I am currently trying to make a gradebook in c++ with structures and classes. I am using a gradbook.h, gradbook.cpp, and a mainprog.cpp for this to work. I am doing this program on leopard, so that is why I have to include to .h file in my .cpp files. My code and error is located below...

================================================================
//GRADBOOK.H

#ifndef GRADBOOK_H_
#define GRADBOOK_H_
#include <string.h>
#include <vector.h>

struct date
{
int day;
int month;
int year;
};

struct eachGrade
{
string assignment;
string type;
date dueDate;
int pointValue;
int pointsEarned;
};

struct oneStudent
{
string ID;
string lastName;
string firstName;
string middleInitial;
string gender;
string className;
string classHour;
int numGrades;
vector<eachGrade>theirGrades;
float gradeAverage;
string lettergrade;
};

class gradbook
{
public:

gradbook();
void loadBook();
void addStudent();
void saveBook();
void addAssignment();
void addGrade();

private:

vector<oneStudent>thisClass;

int numberOfStudents;
int numGrades;
char databaseName[60];
};

//#include "gradbook.cpp"
#endif // GRADBOOK_H_

================================================================
//GRADBOOK.CPP

using namespace std;
#include <iostream>
#include <vector>
#include <fstream>
#include <string.h>
#include "gradbook.h"


gradbook::gradbook()
{
numberOfStudents=0;
numGrades=0;
thisClass.resize(numberOfStudents);
}

void gradbook::loadBook()
{
cout<<"\nGradebook Name : ";
cin>>databaseName;
ifstream infile;
bool success=false;

if (!success)
{
infile.open (databaseName, ios::in);
if (infile.fail())
{
cout<<"\n\tFILE... '"<<databaseName<<"' ...could not be found."<<endl;
}
else
success=true;
}

infile>>numberOfStudents;
thisClass.resize(numberOfStudents);
for (int j=0; j<numberOfStudents; j++)
{
infile>>thisClass[j].ID;
infile>>thisClass[j].lastName;
infile>>thisClass[j].firstName;
infile>>thisClass[j].middleInitial;
infile>>thisClass[j].gender;
infile.ignore();
getline(infile, thisClass[j].className);
getline(infile, thisClass[j].classHour);
}

cout<<"\nThere is/are "<<numberOfStudents<<" student(s) in this class."<<endl;

for (int k=0; k<numberOfStudents; k++)
{
cout<<"\n"<<thisClass[k].ID;
cout<<" "<<thisClass[k].lastName;
cout<<" "<<thisClass[k].firstName;
cout<<" "<<thisClass[k].middleInitial;
cout<<" "<<thisClass[k].gender;
cout<<" "<<thisClass[k].className;
cout<<" "<<thisClass[k].classHour;
}
}


void gradbook::addStudent()
{
numberOfStudents+=1;
thisClass.resize(numberOfStudents);

cout<<"\n\n\t\tNEW STUDENT"<<endl;

cout<<"\nEnter student ID : ";
cin>>thisClass[numberOfStudents-1].ID;

cout<<"Enter student's last name : ";
cin>>thisClass[numberOfStudents-1].lastName;

cout<<"Enter student's first name : ";
cin>>thisClass[numberOfStudents-1].firstName;

cout<<"Enter student's middle initial : ";
cin>>thisClass[numberOfStudents-1].middleInitial;

cout<<"Enter student's gender : ";
cin>>thisClass[numberOfStudents-1].gender;

cin.ignore();

cout<<"Enter class name : ";
getline(cin,thisClass[numberOfStudents-1].className);

cout<<"Enter class hour : ";
getline(cin, thisClass[numberOfStudents-1].classHour);
}

void gradbook::saveBook()
{
cout<<"\n--Gradebook Save Name (include .txt)--"<<endl;
cin>>databaseName;
ofstream fout;
fout.open(databaseName, ios::out);
fout<<numberOfStudents<<endl;

for (int i=0; i<numberOfStudents; i++)
{
fout<<thisClass[i].ID<<endl;
fout<<thisClass[i].lastName<<endl;
fout<<thisClass[i].firstName<<endl;
fout<<thisClass[i].middleInitial<<endl;
fout<<thisClass[i].gender<<endl;
fout<<thisClass[i].className<<endl;
fout<<thisClass[i].classHour<<endl;
}

fout.close();

cout<<"\n\tGradebook "<<databaseName<<" updated!"<<endl;
}

void gradbook::addAssignment()
{
cout<<"\nGradebook Name : ";
cin>>databaseName;
ifstream infile;
int student_choice;
bool success=false;

if (!success)
{
infile.open (databaseName, ios::in);
if (infile.fail())
{
cout<<"\n\tFILE... '"<<databaseName<<"' ...could not be found."<<endl;
}
else
success=true;
}

infile>>numberOfStudents;
thisClass.resize(numberOfStudents);

for (int j=0; j<numberOfStudents; j++)
{
infile>>thisClass[j].ID;
infile>>thisClass[j].lastName;
infile>>thisClass[j].firstName;
infile>>thisClass[j].middleInitial;
infile>>thisClass[j].gender;
infile.ignore();
getline(infile, thisClass[j].className);
getline(infile, thisClass[j].classHour);
}

cout<<"\nThere is/are "<<numberOfStudents<<" student(s) in this class."<<endl;

cout<<"\nWhich Student would you like to add an assignment to?"<<endl;

for (int k=0; k<numberOfStudents; k++)
{
cout<<"\n"<<k+1<<". "<<thisClass[k].ID;
cout<<" "<<thisClass[k].lastName;
cout<<" "<<thisClass[k].firstName;
cout<<" "<<thisClass[k].middleInitial;
cout<<" "<<thisClass[k].gender;
cout<<" "<<thisClass[k].className;
cout<<" "<<thisClass[k].classHour;
}

cout<<"\n\nChoice : ";
cin>>student_choice;

cout<<"\n"<<thisClass[student_choice-1].ID;
cout<<" "<<thisClass[student_choice-1].lastName;
cout<<" "<<thisClass[student_choice-1].firstName;
cout<<" "<<thisClass[student_choice-1].middleInitial;
cout<<" "<<thisClass[student_choice-1].gender;
cout<<" "<<thisClass[student_choice-1].className;
cout<<" "<<thisClass[student_choice-1].classHour;

theirGrades.resize(numGrades);
numGrades+=1;

cout<<"\n\nAssignment Name : ";
cin>>thisClass[student_choice-1].theirGrades[numGrades].assignment;

cout<<"\nAssignment Type : ";
cin>>thisClass[student_choice-1].theirGrades[numGrades].type;

}

void gradbook::addGrade()
{

}

==============================================================
MAINPROG.CPP

#include <iostream>
using namespace std;
#include "gradbook.h"

void menu1();
void menu2();

int main (void)
{
menu1();
}

void menu1()
{
int choice;
gradbook roster;

cout<<"\n\n\t\t\t==> MAIN MENU <==\n\n";
cout<<"1. Load a Gradebook\n";
cout<<"2. Make a New Gradebook\n";
cout<<"3. Add a Student to an Existing Gradebook\n";
cout<<"4. View and Edit Student Assignments\n";
cout<<"5. Exit\n";
cout<<"\nChoice : ";
cin>>choice;

switch (choice)
{
case 1:
roster.loadBook();
menu1();

case 2:
roster.saveBook();
menu1();

case 3:
roster.loadBook();
roster.addStudent();
roster.saveBook();
menu1();

case 4:
menu2();


case 5:
break;

default:
cout<<"\nNo Option Available"<<endl;
menu1();

}
}

void menu2()
{
int choice2;
gradbook assignment;

cout<<"\n\n\t==> Student Assignments Menu <==\n\n";
cout<<"1. Add Assignment for a Student\n";
cout<<"3. Go to Main Menu\n";
cout<<"4. Exit\n";
cout<<"\nChoice : ";

cin>>choice2;

switch (choice2)
{
case 1:
assignment.addAssignment();
menu2();


case 3:
menu1();

case 4:
break;
}
}

==============================================================
ERROR

I put the part that gives me an error in bold. The error says that the vector theirGrades was not declared in the scope, although the vector thisClass doesn't have a problem. If I remove that line, then the program runs, but if I enter an assignment for a student, the debugger stops and gives me some kind of (gdb) message! I really have no idea what could be the problem. If you're wondering, I put the theirGrades vector in the oneStudent structure so I can add assignments directly to individual students.

Just let me know if you have any ideas! EMAIL me at minime5050@gmail.com or reply to this post. Thank you in advance!!!
User is offlineProfile CardPM

Go to the top of the page

pertheusual
post 1 Apr, 2008 - 07:40 PM
Post #2


D.I.C Head

**
Joined: 26 Jan, 2008
Posts: 230



Thanked 2 times
My Contributions


Please post your code inside [code] tags in the future. It is much easier to read code that way.

theirGrades is a member vector in oneStudent, not gradbook, so you need to specify which student the "theirGrades" variable is part of.

So
CODE

thisClass[student_choice-1].theirGrades.resize(numGrades);


I think that should work.

EDIT:
Also, you have a "numGrades" in both the gradbook and student objects. That doesn't quite make sense, at least in the way you implemented it.
Right below the bolded part, you use "numGrades" when you should really be using "thisClass[studend_choice-1].numGrades", otherwise you are just going to keep increasing a value in the gradbook, then you are going try to access outside of your vectors and you'll get errors.

You just need to work on remembering what classes have what properties.


Per

This post has been edited by pertheusual: 1 Apr, 2008 - 07:47 PM
User is offlineProfile CardPM

Go to the top of the page

derbear
post 2 Apr, 2008 - 12:06 PM
Post #3


New D.I.C Head

*
Joined: 1 Apr, 2008
Posts: 3


My Contributions


QUOTE(pertheusual @ 1 Apr, 2008 - 08:40 PM) *

Please post your code inside [code] tags in the future. It is much easier to read code that way.

theirGrades is a member vector in oneStudent, not gradbook, so you need to specify which student the "theirGrades" variable is part of.

So
CODE

thisClass[student_choice-1].theirGrades.resize(numGrades);


I think that should work.

EDIT:
Also, you have a "numGrades" in both the gradbook and student objects. That doesn't quite make sense, at least in the way you implemented it.
Right below the bolded part, you use "numGrades" when you should really be using "thisClass[studend_choice-1].numGrades", otherwise you are just going to keep increasing a value in the gradbook, then you are going try to access outside of your vectors and you'll get errors.

You just need to work on remembering what classes have what properties.


Per


Thanks for the tip. The whole thing about numGrades not being by itself worked. It was supposed to be thisClass[variable].theirGrades[variable].numGrades. Adding an assignment works now. Thanks a lot!!!
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/21/08 10:42PM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month