I'm reading a csv file into c++ to make an multiple parallel arrays and print reports from it. I'm able to cout the strings, no problem, but when I try to create indices, conditions, or translate into int, the strings return no values. There are no characters when I try to atoi, so that is not the issue. I've looked everywhere and can't find any other similar questions. The logic of my code is also not the issue because it works when I populate the tempArray2 manually. Also, I'm a first semester student, so there's going to be some kludge in my code. Does the getline eliminate the value of the string?
(Because this was flagged as answered: This is not a duplicate question, I am not trying to parse the csv into vectors, I'm using parallel arrays and this worked for generating my first report because those values were not strings. Using a parser someone else programed would cause me to fail this assignment and doesn't answer my question why values become 0.)
Here's a snippet of the .csv; the lines are not double spaced on the notepad.
"LOCATION","INDICATOR","SUBJECT","MEASURE","FREQUENCY","TIME","Value","Flag Codes"
"GBR","FERTILITY","TOT","CHD_WOMAN","A","2019",1.63,
"USA","FERTILITY","TOT","CHD_WOMAN","A","1970",2.48,
"USA","FERTILITY","TOT","CHD_WOMAN","A","1971",2.27,
Here's the readFile function. You can see the comment where I tried to atoi the year column (replacing yr[x] with temp like in column 7, which works) and it returns 0 value, even though that column clearly has only ints in the string.
bool ReadFile(string loc[], string yr[], float rates[])
{
ifstream input{FILENAME.c_str()};
if (!input)
{
return false;
}
//init trash and temp vars
string trash, temp;
//trash header line
for (int i{ 0 }; i < 1; ++i)
{
getline(input, trash);
}
for (int x{ 0 }; x < SIZE; ++x)
{
getline(input, loc[x], ',');//read column 1 country
getline(input, trash, ','); // column 2 trash
getline(input, trash, ','); // column 3 trash
getline(input, trash, ','); // column 4 trash
getline(input, trash, ','); // column 5 trash
getline(input, yr[x], ',');// read column 6 year
//yr[x] = atoi(temp.c_str());// would always return value 0 even though no char interfering
getline(input, temp, ',');//read column 7 rates
rates[x] = atof(temp.c_str());
cout << yr[x];
cout << loc[x];
cout << rates[x];
}
return true;
}
And the analyze function, where the issue also occurs in the tempArray2 block. It cannot read "USA" for the boolean, but works if I set it to !=, it will also include indices that clearly have "USA". The first tempArray works as expected.
void Analyze(string loc[], string yr[], float rates[], FertilityResults& result)
{
// array for highest and lowest fertrates
float tempArray[SIZE];
for (int i{ 0 }; i < SIZE; ++i)
{
tempArray[i] = rates[i];
}
result.highestRate = 0;
result.lowestRate = 20;
for (int i{ 1 }; i < SIZE; ++i)
{
if (tempArray[i] >= result.highestRate)
{
result.highestRate = tempArray[i];
result.highestRateIndex = i;
}
else if (tempArray[i] > 0 && tempArray[i] < result.lowestRate)
{
result.lowestRate = tempArray[i];
result.lowestRateIndex = i;
}
}
//2nd array to retrieve USA data
string tempArray2[SIZE];
for (int i{ 0 }; i < SIZE; ++i)
{
tempArray2[i] = loc[i];
//cout << tempArray2[i];
if (tempArray2[i] == "USA")
{
cout << "hi";
result.usaIndex = i;
cout <<result.usaIndex<<endl;
}
}
}
Please let me know if you need anything else or if it runs on your terminal as is somehow. Thanks, this is my final project and the first time I've had to ask for help.