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.
21 messages · Page 1 of 1 (latest)
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.
#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;
}
The relevant part of the error box is "vector subscript out of range". Do you know what that means?
hint: [] is the subscript opterator
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.
so the I is going further than is in the vector im assuming
Yep that sounds right
so all i gotta do is just change it from 10 to whatever the size of it is
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.
thanks for the help
@harsh mirage Has your question been resolved? If so, type !solved :)
!solved
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
✅
it still doesnt paste in order
for (const auto& student : students) {
cout << student.studentNumber << " , " << student.studentName << endl;
}
@harsh mirage
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.
!solved