Reading scores from a file

Hello :) I need someone to check my code for errors, thank you.

Write a program that reads a student name. and 2 test scores from a file and prints the three column report of Name. Score1, and Score2 to a file if the scores are valid otherwise skip the student. The program reads input and output file name from keyboard and then opens the file and reads the information one student at a time, verify the scores are between 0 and 100 and prints them to a file in a tabular format. The process continues until eof is reached. Assume all the data is correctly formatted in the file.

Input file format:
90 80 Jack Smith
100 – 90 Nancy Johnson
80 60 Joe Brown

Output file format:
Name Score1 Score2
Jack Smith 90 80
Joe Brown 80 80


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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>

using namespace std;
int main()
{
string output_name, name;
int num1, num2;
ifstream fin;
ofstream fout;
bool file_open, valid_score;
cout << "Please type in the name for the outputfile: ";
getline (cin, output_name);
fout.open(output_name.c_str());
if (file_open)
{
fout << left << setw(20) << "Name" << setw(15) << "Score1" << setw(15) << "Score2" << endl;
fin >> num1;
while (!fin eof()) 
{
fin >> num2;
valid_score = verify (num1, num2);
getline (fin, name);
if (valid_score)
{
fout << setw(20) << name << setw(15) << num1 << setw(15) << num2 << endl;
}
else 
{
getline (fin, name);
}
fin >> num1;
}

Last edited on
line 20: fin is not opened.
line 21: A . is missing.
line 24: verify(...) doesn't exist.
line 25: Due to line 23 there might be a new line in the stream. If so use ignore(...) to remove it.
line 36: closing braces missing.
Topic archived. No new replies allowed.