The program is supposed to open a .csv file and input the data into a vector. Later I'm supposed to implement a priority queue. However I'm getting this error when I try to run it.
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include "Prisoner.h"
#include "Jailed.h"
using namespace std;
int main()
{
string line, ID, sentenceIn, servedIn, lastName, firstName;
int sentence, served;
vector<string> idNum;
vector<int> sentenceLen;
vector<int> servedTime;
vector<string> lastNameIn;
vector<string> firstNameIn;
ifstream data("prisoner_data.csv");
if (!data.is_open())
{
exit(EXIT_FAILURE);
}
while (getline(data, line))
{
getline(data, ID, ',');
cout << ID << " ";
idNum.push_back(ID);
getline(data, sentenceIn, ',');
cout << sentenceIn << " ";
istringstream(sentenceIn) >> sentence;
sentenceLen.push_back(sentence);
getline(data, servedIn, ',');
cout << servedIn << " ";
istringstream(servedIn) >> served;
servedTime.push_back(served);
getline(data, lastName, ',');
lastNameIn.push_back(lastName);
cout << lastName << " ";
getline(data, firstName, ',');
firstNameIn.push_back(firstName);
cout << firstName << " ";
}
Prisoner p[100]; // Initializing Prisoner class
for (int i = 0; i <= idNum.size(); i++)
{
p[i].setIdNum(idNum[i]);
p[i].setSentence(sentenceLen[i]);
p[i].setTimeServed(servedTime[i]);
p[i].setLastName(lastNameIn[i]);
p[i].setFirstName(firstNameIn[i]);
}
data.close();
system("pause");
return 0;
}
I can also include the prisoner class if needed to figure out the issue.
for (int i = 0; i <= idNum.size(); i++)
应该
for (int i = 0; i < idNum.size(); i++)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.