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
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.
#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});
}
}