Help with C++ Reading and Displaying Input Txt file with while loop

Mushu911 (1)
I hope this is appropriate. I'm pretty dumb. I'm very new to programming and I still don't fully understand a few concepts. I've looked up several examples and explanations but I still am unsure of how to proceed. I'm trying to read a text file but can't get it to display correctly.
I know I have figure out how display the numbers to be read before even being able to find the lowest, highest, and average highest of the values for each person which isn't as hard to figure out for me.


Ex:
6
John Jacob Smith
92 94 84 80 93 83
Carl Darwin
76 62 46 93 81 41
Linux B Well
90 95 89 90 98 86
Ada Hatter
78 63 80 88 76 91

I was able to get it like this:
6

John Jacob Smith
92 94 84 80 93 83
Carl Darwin
76 62 46 93 81 41
Linux B Well
90 95 89 90 98 86
Ada Hatter
78 63 80 88 76 91

I don't know what to do with my code.

My code:

#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

int main()
{
unsigned int number_of_scores;
double score;
string name;
string dummy;
//open file
ifstream input_file;
input_file.open("scores_text.txt");

// read first line of txt file for number of scores
input_file >> number_of_scores;
cout << number_of_scores;

// display names and scores
do
{
cout << name << setw(3) << endl;
}
while (getline(input_file,name));
input_file.close();

return 0;
}

So the numbers are right but are currently only read in as part of the string identifier? Is that correct? I need them as double though.
Last edited on
sorry, got cut off.
you are writing name in the do-while before you read it. make that a while loop.
if there are exactly 6 scores you can do this
vector scores<double>(6);
for(i ...)
input_file >> scores[i];

and that gets them as doubles. its possible to read them as text and convert, but best avoided here.
Last edited on
If it weren't exactly 6 scores (the text file could be changed) what would I do? Increment?


for(unsigned int score_count = 0; score_count < number_of_scores; score_count++)
input_file >> score;

Last edited on
Also note, for the name, the extraction operator stops processing the string when it encounters a whitespace character.

Since you're reading the number of scores from the file you can use that value to initialize your vector.

Perhaps something like the following to start:
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
#include <fstream>
#include <iostream>
#include <vector>
#include <string>

// Let's use a structure to hold the information.
struct Student
{
    std::string name;
    std::vector<int> scores;
};

int main()
{
    //open file.  Use the constructor whenever possible.
    std::ifstream input_file("scores_text.txt");

    // Note you should be checking the state of the stream to insure it opened correctly.

    // Define your variables close to first use, not in one big glob at the beginning of some scope.
    // Using a size_t since that is what std::vector uses for it's size.
    size_t number_of_scores;

    // read first line of txt file for number of scores
    input_file >> number_of_scores;

    std::vector<Student> students;
    std::string name;
    while(std::getline(input_file, name))
    {
         // Now read the test scores.
        std::vector<int> scores(number_of_scores);
        for(auto& itr : scores)
            input_file >> itr;

        students.push_back({name, scores});
    }
}
Topic archived. No new replies allowed.