This is HW -- I am having a problem parsing a line from a .txt file into 2 separate arrays. This is the sample .txt file with data:
Aquaman,51,58,42,69,25
Diana Prince,25,91,87,68,78
Batman,58,78,96,68,77
Captain America,78,69,78,65,48
Iron Man,14,78,96,85,78
Spiderman,47,96,87,56,96
Wolverine,14,98,78,87,96
Captain Atom,14,74,75,76,98
I am trying to put the name (Aquaman, Diana Prince, etc...) into my names[ ] and then each individual score into my avg_scores[ ]. Once those scores are in the indScores[ ], I'll take the average of them and put them into the avg_scores [ ].
I think I'm using the getline() and str.find() correctly? Inside my
while (!eof()) things SEEM to be working but when I try to check print each array, I'm only getting the last name (Captain Atom) and the first number (14)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
|
#include <iostream>
#include <cstdlib>
#include <fstream>
#include <sstream> //convert string to a number
#include <iomanip> //setw()
using namespace std;
int ReadScores(string fileName, string names[], float avg_scores[], int array_size){
int linesCounted = 0;
float indScores[array_size];
int lineCounter = 0;
string myLine, nameSubString, scoreSubString;
float scoreConvert;
//declaring the stream variable myFile(what I want it to read from, the open mode I want)
ifstream myFile;
//open the file
myFile.open("SampleInput.txt", ios::in);
//check to see if file was opened correctly
if (myFile.fail()){
cout << "Error opening "<< fileName << endl;
return 0;
}
int index = 0;
//read the file with a while loop until the end of file is reached
while (!myFile.eof()){
//getline(what I want to read from, where I want to store it)
//getline(myFile, myLine, ',');
getline(myFile, myLine);
//this should grab the the names at the beginning of each string on each new line
nameSubString = myLine.substr(0, myLine.find(','));
names[index] = nameSubString;
for (int index = 0; index < array_size; index++){
//grab the first number and store it the scoreSubString variable
scoreSubString = myLine.substr(myLine.find(',') + 1, myLine.find(','));
///convert string to number
stringstream(scoreSubString) >> scoreConvert;
///store number in float array
indScores[index] = scoreConvert;
}
cout << myLine << endl;
//increment my overall lineCounter
lineCounter++;
}
//test print statment to validate arrays
for (int index = 0; index < array_size; index++){
cout << names[index] << endl;
cout << indScores[index] << endl;
}
myFile.close();
return lineCounter;
}
int main(){
int array_size = 12;
float avg_scores[array_size];
string names[array_size];
string fileName = "SampleInput";
//populate the arrays
for (int index = 0; index < array_size; index++){
names[index] = " ";
avg_scores[index] = -1;
}
int linesRead = ReadScores(fileName, names, avg_scores, array_size);
cout << endl;
cout << linesRead << endl;
return 0;
}
|