#issues with outputting a vector

21 messages · Page 1 of 1 (latest)

rich elbowBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

harsh mirage
#
#include <string>
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
using namespace std;

struct Students {//structure for the students 
    string studentName;
    string studentNumber;
    string course1Mark;
    string course2Mark;
    string course3Mark;
    string examMark;
    

};


int main() {
    int totalStudents;
    int i = 0;
    int userChoice;
    vector<Students> students = {};//

    ifstream inFile("studentMarks.txt");
    if (inFile.is_open()) {

        
        inFile >> totalStudents;

        while (!inFile.eof())//adds the information from the file into the vector
        {
            Students student{};
            getline(inFile, student.studentName, ',');
            getline(inFile, student.studentNumber, ',');
            getline(inFile, student.course1Mark, ',');
            getline(inFile, student.course2Mark, ',');
            getline(inFile, student.course3Mark, ',');
            getline(inFile, student.examMark, ',');
            students.push_back(student);
        }
    }
    else {
        cout << "Error File not found. Program non functional" << endl;
        cout << "Ending Program" << endl;
        return 0;
    }


    for (int i = 0; i < 10; ++i) {
        cout << students[i].studentNumber << " , " << students[i].studentName << " , " << endl;
    }

    return 0;
}
winter mortar
#

The relevant part of the error box is "vector subscript out of range". Do you know what that means?

harsh mirage
#

no

#

im new to using vectors

#

well new to c++ really

sonic sphinx
#

hint: [] is the subscript opterator

winter mortar
#

yeah, "subscript" is when you use the square brackets to access an element in the vector. you're accessing a position in the vector that's out of range.

harsh mirage
#

so the I is going further than is in the vector im assuming

winter mortar
#

Yep that sounds right

harsh mirage
#

so all i gotta do is just change it from 10 to whatever the size of it is

winter mortar
#

Yeah exactly. C++ also has a convenient shortcut for when you want to iterate over every item called "range-based for loop" that you could look into.

harsh mirage
#

thanks for the help

rich elbowBOT
#

@harsh mirage Has your question been resolved? If so, type !solved :)

harsh mirage
#

!solved

rich elbowBOT
#

Thank you and let us know if you have any more questions!

This thread is now set to auto-hide after an hour of inactivity

#

harsh mirage
#

it still doesnt paste in order

#
for (const auto& student : students) {
    cout << student.studentNumber << " , " << student.studentName << endl;
}
rich elbowBOT
#

@harsh mirage

Please Do Not Delete Posts!

Please don't delete forum posts. They can be helpful to refer to later and other members can learn from them. In the future you can use !solved to close a post and mark a post as solved.

harsh mirage
#

!solved