vendredi 25 janvier 2019

Structure nested inside another structure as a vector

So basically I am making a program that stores student information and the students scores both inside of structures. I need help figuring out how to store values into the vector grades for each student, given some have 2-3 test scores each and all of the information is coming from an input file.

#include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <fstream>
using namespace std;

// Structures
struct Scores
{
int id;
string course = "";
int credit;
int score;
};


struct Person
{
int id;
string name = "";
string phone = "";
vector<Scores> grades;
};


// Function prototypes
void displayOne(vector<Person>, int);


int main()
{
ifstream inputFile;
ifstream inputFile2;
inputFile.open("StudentInfo.txt");
inputFile2.open("StudentScores.txt");

Scores tempScore;
Person tempStudent;
vector<Person> students;

if (inputFile)
{
    int value = 0;
    string name = "";
    string phone = "";
    while (inputFile >> value >> name >> phone)
    {

        tempStudent.id = value;
        tempStudent.name = name;
        tempStudent.phone = phone;
        students.push_back(tempStudent);
    }
}
else
    cout << "Error Opening StudentInfo.txt.. Try again." << endl;


displayOne(students, 12546);
displayOne(students, 15667);
displayOne(students, 14388);



inputFile.close();
inputFile2.close();

//displayOne(15667);

//displayAll();

return 0;

}


// Function definitions
void displayOne(vector<Person> students, int verifyID)
{
bool foundID = false;
int index = 0;
for (int i = 0; i < students.size(); i++)
{
    if (students[i].id == verifyID)
    {
        foundID = true;
        index = i;
    }

}
    if (students[index].id == verifyID)
    {
        cout << "Student ID: " << students[index].id << " ";
        cout << "Student Name: " << students[index].name << " ";
        cout << "Student Phone: " << students[index].phone << " ";
        cout << "=================================";
        cout << endl;
        cout << students[index].grades[].course << endl;
    }
}

I need to be able to call a function that lets me cout << students[0] - number of students but also display each class they take and the scores they achieved and so forth.

Aucun commentaire:

Enregistrer un commentaire