Hi,
Wondering if I could get some help. This program is supposed to :
-Input student data(firstname,lastname,student#, and a vector of Grades(up to 10 grades) with a grade and assignment name(notworking)) from a text file and store it in a vector
-Have a menu system for navigation
-add a student ---Working
-delete a student --- not working
-modify a student --- not working, dont know how to access student and modify the non-working struct vector of Grades, i need help with this
-quit (save vector in text file on quit--this is not working, im not using ofstream right)
Vectors/iterators must be used
Here is my bug ridden code, I know it's crappy, but any help, suggestions, comments, would be greatly appreciated.
Thanks, Dev
//****************
//STUDENT.H
//***************
#ifndef _Student_HG_
#define _Student_HG_
#include <string>
#include <vector>
using std::string;
class Student
{
public:
Student( const string &, const string &, const string &, vector <Grade> &);//i should have an int and assign it to a vector instead of vector here?
~Student(); //Destructor
void setFirstName( const string & );//set first name
string getFirstName() const;
void setLastName( const string & );
string getLastName() const;
void setStudentNumber( const string & );
string getStudentNumber() const;
void printGrades( vector <Grade> &);
//void addGrade
vector <Grade> vecGrade;
private:
string firstName;
string lastName;
string studentNumber;
};
#endif
//**************************
//STUDENT.CPP
//*********************
#include <iostream>
using namespace std;
#include "Student.h"
//, const int &grade
struct Grade()
{
string assignmentName;
int assignmentGrade;
}
Student::Student( const string &first, const string &last, const string &snum , vector <Grade> &vG)
: firstName( first ), lastName( last ), studentNumber( snum )
{
Grade tempGrade = new Grade;
//start loop to input grades into vector<Grade> NOT WORKING
/*tempGrade.assignmentName = ;
tempGrade.assignmentGrade = ;
vecGrade.push_back(tempGrade);*/
}
Student::~Student()
{
}
void Student::setFirstName( const string &first)
{
firstName = first;
}
string Student::getFirstName() const
{
return firstName;
}
void Student::setLastName( const string &last)
{
lastName = last;
}
string Student::getLastName() const
{
return lastName;
}
void Student::setStudentNumber(const string &snum)
{
studentNumber = snum;
}
string Student::getStudentNumber() const
{
return studentNumber;
}
void Student::printGrades()
{
for (vector<Grade>::iterator it = vecGrade.begin();
it != vecGrade.end(); it++)
{
cout << (*it).assignmentName << " "
<< (*it).assignemtnGrade << endl;//DONT KNOW what todo...This doesnt work
}
}
//**************
//Main
//***********
#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <fstream>
#include "Student.h"
using namespace std;
int enterChoice()
{
cout << "\nEnter your choice" << endl
<< "1 - Add a student" << endl
<< "2 - Modify a student" << endl
<< "3 - Delete a student" << endl
<< "0 - Exit\n" << endl << endl << endl;
int menuChoice;
cin >> menuChoice;
return menuChoice;
}
int enterModChoice()
{
cout << "\nEnter your choice" << endl
<< "1 - Change student's first name" << endl
<< "2 - Change student's last name" << endl
<< "3 - Change student's student number" << endl
<< "4 - change students assignment/grades" << endl
<< "0 - Exit\n" << endl << endl << endl;
int menuChoice;
cin >> menuChoice;
return menuChoice;
}
void printOut(vector<Student> &vecStud)
{
cout << "Student" << setw(25) << "Grade" <<endl;
for (vector<Student>::iterator it = vecStud.begin();
it != vecStud.end(); it++)
{
cout << (*it).getFirstName() << " " <<
(*it).getLastName() << " :" <<
endl;
}
}
int main()
{
//Load up the student data text file into vector
vector < Student > vecStudent;
ifstream myFile("studentData.txt");
string fname;
string lname;
string ssn;
while (myFile >> fname >> lname >> ssn)
{
Student tempStu(fname,lname,ssn);
vecStudent.push_back(tempStu);
}
myFile.close;
printOut(vecStudent);
int choice;
int choice2;
string result;
while ( ( choice = enterChoice() ) != 0)
{
switch ( choice)
{
case 1:
{//add a student
cout << endl << endl << endl;
cout << "Adding student...." << endl << endl << endl;
cout << "What is the student's first name? : ";
cin >> fname;
cout << endl << "What is the student's last name? : ";
cin >> lname;
cout << endl << "What is the student's student number? : ";
cin >> ssn;
Student tempStu(fname,lname,ssn);
vecStudent.push_back(tempStu);
//How many assignments/grades do you want to put in?
//loop inputting grades into Student grade vector
printOut(vecStudent);
break;
}
case 2:
{
//modify and student
cout << "Modifing a student......" << endl << endl << endl;
cout << "What is the student's student number\n who you want to modify?" << endl << endl;
cin >> ssn;
//search for student
for (vector<Student>::iterator it = vecStudent.begin();
it != vecStudent.end(); it++)
{
//THIS DOESNT WORK*******
if (!strcmp((*it).getStudentNumber, ssn))
{
cout << "Student found......" << endl;
//display menu for changing a student's stats
while ( ( choice2 = enterModChoice() ) != 0)
{
switch ( choice2 )
{
case 1:
//set/change students first name
cout << "Enter student's new first name: ";
cin >> result;
(*it).setFirstName( result );
break;
case 2:
//set/change student's last name
cout << "Enter student's new last name: ";
cin >> result;
(*it).setLastName( result );
break;
case 3:
//set/change student's student number
cout << "Enter student's new student number: ";
cin >> result;
(*it).setStudentNumber( result );
break;
case 4:
//print off assignment list (vector of struct Grade's inside Student) w/grades
(*it).printGrades();
//Bring up another menu for what assignment to change
//enter assignemnt # 1-10
// change 1: name or 2: grade
// if
break;
case 0:
break;
default:
cout << "You entered a wrong number" << endl;
break;
}
}
}
else
{
cout << "Sorry, either the number was incorrectly inputted"
<< "\n or the student does not exist." << endl;
}
}
break;
}
case 3:
{
//delete a student
cout << "Deleting a student......" << endl << endl << endl;
cout << "What is the student's student number\n who you want to delete?" << endl << endl;
cin >> ssn;
for (vector<Student>::iterator it = vecStudent.begin();
it != vecStudent.end(); it++)
{
//THIS DOESNT WORK
if ( !strcmp((*it).getStudentNumber, ssn))
{
cout << "Sorry, either the number was incorrectly inputted"
<< "\n or the student does not exist." << endl;
}
else
{
cout << "Student found......" << endl;
cout << "Deleting...." << endl;
//DOESNT WORK, i guess i have to delete it from the vector somehow aswell?
delete (*it);
}
}
break;
}
case 0:
{
//exit
//save Student vector to file, outputting everything
ofstream myFile("studentData.txt");
myFile.trunc;
for (vector<Student>::iterator it = vecStudent.begin();
it != vecStudent.end(); it++)
{
myFile >> (*it).getFirstName() >> " " >>
(*it).getLastName() >> " :" >> endl;
//print out grades
myFile >>(*it).printGrades() >> endl;
}
myFile.close;
return 0;
}
default:
{//error msg,
cout << "You have put in an incorrect choice" << endl;
printOut();
}
break;
}
}
return 0;
}