I am stuck with my current dilemma right now. I am trying to pass a vector of objects into my function that will print or save to a file. But whenever I try looping through my vector it says "C3867"
My code is:
#include <iostream>
#include <string>
#include <vector>
#include "Student.h"
using namespace std;
/* FUNCTIONS */
void programStart();
void saveClassInfo(vector<Student> &newClass);
void printClassInfo(vector<Student> &newClass);
int main()
{
programStart();
cout << "Press any key to close...";
cin.get();
cin.get();
return EXIT_SUCCESS;
}
void programStart()
{
int size_of_class;
vector<Student> my_class;
cout << "What is the size of the class: ";
cin >> size_of_class;
for (int i = 0; i < size_of_class; i++) {
string student_name;
char student_grade;
cout << "Name of student " << (i + 1) << ": ";
cin >> student_name;
cout << "Grade of student " << (i + 1) << ": ";
cin >> student_grade;
if (student_grade >= 'A' || student_grade <= 'F') {
Student newStudent(student_name, student_grade);
my_class.push_back(newStudent);
}
else {
while (student_grade < 'A' || student_grade > 'F') {
cout << "Input the correct grade: ";
cin >> student_grade;
}
Student newStudent(student_name, student_grade);
my_class.push_back(newStudent);
}
}
//saveClassInfo(my_class);
printClassInfo(my_class);
}
void saveClassInfo(vector<Student> &newClass)
{
ofstream newFile;
newFile.open("class_info.txt");
if (newFile.fail()) {
perror("The following error occured");
}
else {
for (int i = 0; i < newClass.size(); i++) {
newFile << newClass[i].getName << "\t\t"
<< newClass[i].getGrade << endl;
}
}
newFile.close();
cout << "Done saving.\n";
}
void printClassInfo(vector<Student> &newClass)
{
for (int i = 0; i < newClass.size(); i++) {
cout << newClass[i].getName << "\t\t" << newClass[i].getGrade << endl;
}
}
"Student.h"
#ifndef STUDENT_H
#define STUDENT_H
#include <string>
#include <fstream>
#include <stdio.h>
using namespace std;
class Student
{
public:
Student(string student_name, char student_grade);
// Accessors
string getName();
char getGrade();
// Modifiers
void setName(string student_name);
void setGrade(char student_grade);
private:
string _name;
char _grade;
};
#endif /* STUDENT_H */
Student.cpp
#include "Student.h"
Student::Student(string student_name, char student_grade)
{
setName(student_name);
setGrade(student_grade);
}
string Student::getName()
{
return _name;
}
char Student::getGrade()
{
return _grade;
}
void Student::setName(string student_name)
{
_name = student_name;
}
void Student::setGrade(char student_grade)
{
_grade = student_grade;
}
Can anybody tell me what is wrong with my code?
Aucun commentaire:
Enregistrer un commentaire