mercredi 29 avril 2020

Test Results exersise

Exercise: Test results are saved in a text file as below. The first line indicates the maximum scoring for individual tasks. Each subsequent line contains the student's one-part surname and points gained by him for subsequent tasks. The number of assignments or students is not known in advance. Write program "Test" , which takes the name of such a file as an argument and prints to standard output the combined results of each student and the average results of each assignment as in the example below. The program only includes the fstream, iostream, sstream, string and vector header files.


Sample input file input.txt
                 5.0 5.0 5.0
Einstein         1.5 3.0 0.5
Chopin           0.5 3.5 2.5
Sklodowska-Curie 5.0 5.0 5.0


Sample implementation:

Linux: ./colloquium input.txt
Windows: colloquium.exe input.txt
Out: Einstein 5
Out: Chopin 6.5
Out: Sklodowska-Curie 15
Out: 1 2.33333
Out: 2 3.83333
Out: 3 2.66667

This is what I have done so far :

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>


int main(int argc, char *argv[])
{
std::ifstream input(argv[1]);
double sum = 0.;
for (std::string line; std::getline(input, line);)
    {
    std::string word;
    for (std::istringstream stream(line); stream >> word;);
    double grade;
    std::cout<<line<<std::endl;
        if (std::istringstream(word) >> grade)
        {
        sum += grade; 
        }
    }
std::cout << sum << std::endl;

input.close(); 
}

And I'm kind of stuck because it's only reads numbers from last column. And I dont know what to do further

Aucun commentaire:

Enregistrer un commentaire