Problem with File I/O using Nested For Loops

I'm trying to read the first and last names, and six separate scores out of an input text file for seven different individuals (see attached 'input.txt' with fairy tale character's s names). The first three scores are supposed to be considered 'program' scores and the last three are supposed to be 'test' scores (scores on program assignments and class tests) . We are using those numbers to calculate a program average, a test average, and a course average. I'm using one function containing an end of file while loop to execute three functions (input the data, calculate the averages, and output the data- all using their own 'for' loops). You'll have to excuse some things being out of place, the code is messy because I've been trying so many different things to make the calculations work. The file input function works fine. The problem is doing calculations on the input data. I've been trying to use a for loop to do the calculations for the program and test averages, but I can't figure out the right way to do it for the life of me. Either I get error messages or an improper output. Perhaps there's another type of loop I could be using. Or maybe I should declare two separate arrays for program and test scores. I know I could just do the long form, counting the spots in the array individually and just doing all the calculations and output inside of one EOF while loop, but I'm trying to be fancy and learn how to use for loops with functions. I'm really confused:

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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116

#include<iostream>
#include<fstream>
#include<iomanip>
using namespace std;

//Global Infile and Outfile
ifstream fin;
ofstream fout;

//Function Prototypes
void eofwhileloopfn(ifstream &fin, ofstream &fout,
string name[2],float score[6],float avg[3]);
void inputfn(ifstream &fin,string name[2], float score[6]);
void calcfn(float score[6],float avg[3]);
void outputfn(ifstream &fin, ofstream &fout,
string name[2],float score[6], float avg[3]);

int main ()
{
system ("color f0");
string name[2];
float score[6], avg[3];
char inputfile[51];//stores name of the input file
char outputfile[51];//variable to store the name of the output file 
cout<<left<<fixed<<showpoint<<setprecision(2);
cout<<"Enter the input file name: ";
cin>>inputfile;
cout<<endl;
fin.open(inputfile);
if (!fin)
{
cout<<"Cannot open the input file. "<<endl;
system("pause");
return 1;
}
cout<<"Enter the output file name: ";
cin>>outputfile;
cout<<endl;
fout.open(outputfile);

// The End-of-File While Loop Function 
eofwhileloopfn (fin,fout,name,score,avg);

fin.close();
fout.close();
system ("pause");
return 0;
}//end of main fn


//***********************************************************************

void eofwhileloopfn(ifstream &fin, ofstream &fout,
string name[2],float score[6], float avg[3])
//float sum
{

//End-Of-File Controlled While Loop
//Call Other Functions Inside of Loop
while (!fin.eof())
{
inputfn(fin,name,score);
calcfn (score,avg);
outputfn(fin,fout,name,score,avg);
} 
}//End of Loop Function 

//************************************************************************
void inputfn(ifstream &fin, string name[2], float score[6])
{
//File Input
fin>>name[0]>>name[1];
for (int col=0;col<6;col++)
{fin>>score[col];}//col represents index
}

//***********************************************************************

void calcfn(float score[6],float avg[3])
//int sum [2]
{ 
cout<<left<<fixed<<showpoint<<setprecision(2);
//Program Average
//Test Average
for (int col=0;col<6;col++)
{
while (col<3)    
//for (int col=0;col<3;col++)
{
avg[0]=float(score[col]+score[col]+score[col]);
}
while (col>3||col<6)
//for (int col=0;col<6;col++)
{
avg[1]=float(score[col]+score[col]+score[col]);
}
}
//Course Average
avg[0]=avg[0]/3.0;
avg[1]=avg[1]/3.0;
avg[2]=(avg[0]+avg[1])/2.0;
}//end of calculation function 

//***********************************************************************
void outputfn(ifstream &fin, ofstream &fout,
string name[2],float score[6],float avg[3])
{
cout<<left<<setw(20)<<name[0]+" "+name[1];
for (int col=0;col<3;col++)
{cout<<left<<setw(5)<<avg[0]<<setw(5)<<avg[1]<<setw(5)<<avg[2];}
cout<<endl;
}//End of Output Function 

//***********************************************************************




[input.txt]

Snow White 77 87 76 82 92 96
Jack Frost 67 77 87 88 86 89
Sleeping Beauty 90 89 78 88 86 85
Prince Charming 92 87 88 89 90 91
Rapunzel Repunzel 66 76 73 81 85 70
Santa Claus 90 89 95 86 88 94
Miss Horner 65 76 72 50 69 75

[/input.txt]



Please ignore the "//float sum" 's, I was just trying to use those to play around with the calculations.
Topic archived. No new replies allowed.