[HW] - Reading/Parsing a .txt file into an array

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;
}
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
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>

auto constexpr SIZE = 8; //#students
auto constexpr SUBJECTS = 5; //#subjects

int main()
{
    std::ifstream inFile("F:\\test.txt");

    std::string names[SIZE]{};
    double scores[SIZE]{};

    if(inFile)
    {
        std::string line{};
        size_t i {};
        while (getline(inFile, line) && i < SIZE)//first read an entire line until all lines have been read
        {
            if(line.find(',') != std::string::npos)//then search for the ,
            {
                names[i] = line.substr(0, line.find(','));//assign the name to the corresponding names array element
                std::istringstream stream{line.substr(line.find(',') + 1)};//construct istringstream object with rest of the line
                double subject_scores[SUBJECTS];//temporary array to hold scores to calculate average
                size_t j{};
                while (stream)//while the stream is good
                {
                    std::string temp_score;
                    getline(stream, temp_score, ',');//peel off the scores from stream using the , delimiter
                    std::istringstream stream_double(temp_score);//each peeled off score is converted to double via istringstream
                    stream_double >>  subject_scores[j];
                    ++j;
                }
                for (size_t j = 0; j < SUBJECTS; ++j)
                {
                    scores[i] += subject_scores[j] / SUBJECTS;//average score
                }
                ++i;
            }
        }
    }
    for (size_t i = 0; i < SIZE; ++i)std::cout << names[i] << ", " << scores[i] << "\n";
}
Topic archived. No new replies allowed.