Doing this assignment where I parse information into a struct and I keep recieving an error saying "string subscript out of range" when trying to test. Cant figure out where I went wrong.
here is the struct
struct baby_type {
std::string name;
std::vector<int> yearsCount;
int total;
} ;
here is my function
baby_type Parse_Line(const std::string line )
{
baby_type baby;
std::string name;
std::string year;// = "0000";
std::string total;
int i = 0;
int k = 1;
int LengthOfOccurences = 0;
int DigitOfOccurences = 0;
while (line.at(i) != ',') {
name[i] = line.at(i);
i++;
}
baby.name = name;
i++;
while (k < 100) {
if (line.at(i) == ',') {
year.resize(LengthOfOccurences);
baby.yearsCount.at(k) = std::stoi(year);
//year = "0000";
i++;
k++;
DigitOfOccurences = 0;
LengthOfOccurences = 0;
}
else {
year.at(DigitOfOccurences) = line.at(i);
i++;
LengthOfOccurences++;
}
}
int m = 0;
int LengthOfLine = line.length();
while (i < LengthOfLine) {
total.at(m) = line.at(i);
m++;
i++;
}
baby.total = std::stoi(total);
return baby;
}