Click here to Skip to main content
15,891,248 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 << line << '\n';

		}
		file.close();

	}
Posted
Updated 4-Nov-15 12:57pm
v5

1 solution

You should be using ifstream for input.

Read this: http://www.cplusplus.com/doc/tutorial/files/[^]
and:http://www.cplusplus.com/reference/fstream/ifstream/open/[^]

Then:

C++
ifstream studentfile;
string studentfilename;

cout << "Please enter the student file name = ";
cin >> studentfilename;
studentfile.open(studentfilename);
std::string argument in open() is legal in C11 only.
 
Share this answer
 
v2
Comments
[no name] 4-Nov-15 18:53pm    
You have completely changed the question. It may be good to carefully read the first tutorial link given and my answer.
[no name] 4-Nov-15 19:29pm    
You did not ask that in question. What is your actual question?
[no name] 4-Nov-15 19:38pm    
Slow down - read the tutorial again and make another effort. Its all there.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900