mercredi 4 novembre 2015

How to declare a text file in c++ and display the average score of a student by reading the data from the file.?

I have already created a file called ( student.txt) and i put the following information inside the file

Will Smith 99
Sarah Johnson 100 
Tim Howard 70 
Francesco Totti 95 
Michael Jackson 92

I want to ask the user to enter the file name,once they enter the file name, I want to displays the average score by reading the data from the file.

Note : Each line in the file contains the first name, last name, and test score of a student.

Here's what i did so far and this could only display what inside the file Please help me to declare the file name and to display the average score of a student

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

using namespace std;
int main() {
    string studentFile;

    ifstream file("student.txt");
    if (file.is_open()) {
        while (getline(file,studentFile))
        {
            cout << studentFile << '\n';

        }
        file.close();

    }
system("pause");
return 0:
}

Aucun commentaire:

Enregistrer un commentaire