filestreams and calculating averages

I wrote this for a homework problem but I don't know how to continue (the instructions are below). I'm sure there is a quicker way to do this. This works for the first line, but I can't figure out how to get it to work for all the lines. He said we don't need a while loop. The only other loop we learned was a for loop. Do I need that in this problem?
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

double avg(double,double,double,double,double,double,double,double);

int main ()
{
ifstream inFile;
inFile.open("scores.txt");

ofstream outFile;
outFile.open("summary.txt");

string type;
double score1, score2, score3, score4, score5, score6, score7, score8;

inFile >> type >> score1 >> score2 >> score3 >> score4 >> score5 >> score6 >> score7 >> score8;
outFile << setw(14) << left << type << " " << avg(score1,score2,score3,score4,score5,score6,score7,score8)*10 << endl;

return 0;
}

double avg(double s1, double s2, double s3, double s4, double s5, double s6, double s7, double s8)
{
return (s1+s2+s3+s4+s5+s6+s7+s8)/8;
}

Here is the exact problem:
Create a program which will read from a file named "scores.txt" containing a student's scores for this class. Assume the following about this fi le:
 The fi rst line contains 8 quiz scores, each out of 10.
 The second line contains 8 homework scores, each out of 10.
 The third line contains 8 classwork scores, each out of 10.
 The fourth line contains 3 project scores, each out of 20.
 The fifth line contains 2 exam scores, each out of 50.
Note: Because you know how many lines are in this file, and the number
and types of each score, you do not need a while loop to read in the data.
Your program should calculate: a quiz percentage, homework percentage,
classwork percentage, project percentage, and exam percentage out of the
total number of points possible for each category. Using the information
from our class syllabus on the weight of each category, use these percent-
ages to come up with a score from 0 to 100 representing the student's work
for the semester. Your program should place all of this information in a
table in a file named "summary.txt".
Last edited on
Well, I don't suppose you technically need a loop, but it sure would make things easier. In the context of the assignment, I suppose 'not needing' a while loop may mean he'd rather you used a for loop.

Using some sort of container to hold the scores may make things a bit cleaner, but you could do the sum of each line as you read them and avoid the container and all the individual score variables.

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
#include <fstream>


const unsigned n_quiz_scores     = 8 ;
const unsigned n_homework_scores = 8 ;
// ...


int main ()
{
	std::ifstream inFile("scores.txt");

	unsigned quiz_score_sum = 0 ;
	for (unsigned i = 0; i < n_quiz_scores; ++i )
	{
		unsigned score ;

		if ( !(inFile >> score) )
		{
			// input operation didn't go well - handle error.
		}

		quiz_score_sum += score ;
	}

	// repeat for other types of scores.

	// then do calculations and output to file.
}
Don't forget to close a file when you are done with it.
this is crucial with saving files and avoiding errors.

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

using namespace std;

double avg(double,double,double,double,double,double,double,double);

int main ()
{

string type;
double score1, score2, score3, score4, score5, score6, score7, score8;
ifstream inFile("scores.txt");
if(inFile.fail())
{
cout<<"I DO NOT EXIST";
return 0;
}


inFile >>type >> score1 >> score2 >> score3 >> score4 >> score5 >> score6 >> score7 >> score8;
inFile.close();
ofstream outFile;
outFile.open("summary.txt");

outFile << setw(14) << left << type << " " << avg(score1,score2,score3,score4,score5,score6,score7,score8)*10 << endl;

outFile.close();
return 0;
}

double avg(double s1, double s2, double s3, double s4, double s5, double s6, double s7, double s8)
{
return (s1+s2+s3+s4+s5+s6+s7+s8)/8;
}


Last edited on
Your welcome :).


Text File is set up this way

Quiz
100 100 100 100 100 100 100 100
HomeWork
100 100 100 100 100 100 100 100
Classwork
100 100 100 100 100 100 100 100
Projects
100 100 100
Exam
100 100


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

using namespace std;

int readAndWriteScores();

int main ()
{
	readAndWriteScores();
return 0;
}
int readAndWriteScores()


{

ifstream inFile("scores.txt");
ofstream outFile;
outFile.open("summary.txt");
if (inFile.fail())
{
	cout<<"Dose Not Exist";
	return 0;
	
}

string type ="";
double scores=0;
double number=0;
double totavg=0;
for(int i =0 ; i < 3; i++)
{
inFile >>type;
cout <<type<<endl;
for (int j =0;j<8;j++)
{
inFile >>number;
cout << number<< " ";
scores+=number;
}
cout<<endl;
totavg+=(scores/8)*.1 ;
outFile << setw(14) << left << type << " " <<((scores/8)*.1)<<"%"  << endl;
scores =0;
}
cout <<type<<endl;
inFile >>type;
for (int j =0;j<3;j++)
{
inFile >>number;
cout << number<<" ";
scores+=number;
}
cout<<endl;
totavg+=(scores/3)*.2 ;
outFile << setw(14) << left << type << " " <<((scores/3)*.2)<<"%"  << endl;

scores =0;
cout <<type<<endl;
inFile >>type;
for (int j =0;j<2;j++)
{
inFile >>number;
cout << number<<" ";
scores+=number;
}
cout<<endl;
totavg+=(scores/2)*.5 ;
outFile << setw(14) << left << type << " " <<((scores/2)*.5) <<"%" << endl;

outFile << setw(14) << left << "The total average is " << " " <<totavg <<"%" << endl;

inFile.close();
outFile.close();




return 0;
}

Last edited on
Thank you so much. I had some questions: In the for loops you sometimes used 'i' and sometimes 'j'. Is there a reason for that? And what were these lines for in your code?
1
2
3
4
5
6
if (inFile.fail())
{
	cout<<"Dose Not Exist";
	return 0;
	
}

This is what I ended up with.
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
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
#include <cmath>

using namespace std;

int readandwritescores();

int main()
{
	readandwritescores();
	return 0;
}

int readandwritescores()
{
    ifstream inFile;
	inFile.open("scores.txt");
	
	ofstream outFile;
	outFile.open("summary.txt");
	
	string type;
	double scores=0;
	double number=0;
	double totavg=0;
	
	for (int i=0;i<3;i++)
	{
	inFile >> type;
		
	for (int i=0;i<8;i++)
	{
		inFile >> number;
		scores+=number;
	}
	totavg+=(scores/8)*10;
		
	outFile << fixed << setprecision(1) << setw(14) << left << type << ((scores/8)*10) << "%" << endl;
	scores=0;
	}
	
	inFile >> type;
	for (int i=0;i<3;i++)
	{
		inFile >> number;
		scores+=number;
	}
	totavg+=((scores/3)*5);
	
	outFile << fixed << setprecision(1) << setw(14) << left << type << ((scores/3)*5) << "%" << endl;
	scores=0;
	
	inFile >> type;
	for (int i=0;i<2;i++) 
	{
		inFile >> number;
		scores+=number;
	}
	
	totavg+=(scores/2)*2;
		
	outFile << fixed << setprecision(1) << setw(14) << left << type << (scores/2)*2 << "%" << endl;
	scores=0;
	
	outFile << fixed << setprecision(1) << setw(14) << left << "Course Score" << totavg/5 << "%" << endl;
}

The program works, but at the end there's a little warning thing that says "Control reaches end of non-void function". Do you know what that means?
I left some comments in the code.


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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172

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

using namespace std;
//used int because 
//i dont like 
//straight up crashing the
//program
//this corresponds
//to not using exit(0);
//when i am checking the file
//to see if it exists
//and retrun 0;
//to let the program
//end its self.
int readAndWriteScores();

int main ()
{
	readAndWriteScores();
return 0;
}
int readAndWriteScores()


{
//files for streaming
ifstream inFile("scores.txt");
ofstream outFile("summary.txt");
//This checks to see if
//the file exists
//if not it returns a 0 
// which in main will continue to
//exit the progam
if (inFile.fail())
{
	cout<<"Dose Not Exist";
	return 0;
	
}
//initialize variables.
string type ="";
double scores=0;
double number=0;
double totavg=0;

//this i loop here
//runs 3 times
//for the
//Quiz 8 scores at 10% total grade
//Homework 8 scores at 10% total grade
//Classwork 8 scores at 10% total grade
//used it to loop three
//times because
//they all have same number
//of scores and percentage
for(int i =0 ; i < 3; i++)
{
//gets the string from file
inFile >>type;
cout <<type<<endl;
//this gets the eight scores
//3 times this for loop will
//be executed to get the
//Quiz
//Homework
//Classwork
for (int j =0;j<8;j++)
{
//take in number 
//from file
inFile >>number;
cout << number<< " ";
//use that number total
//the scores
scores+=number;
}
cout<<endl;
//find average of that percentage
//add it up find total score
totavg+=(scores/8)*.1 ;

//prints to file type and percentage accquired
//of that type
outFile << setw(14) << left << type << " " <<((scores/8)*.1)<<"%"  << endl;

//set scores to 0 
//so you can find the
//average of the next
//scores
scores =0;
}
cout <<type<<endl;
//gets string from file
inFile >>type;
//loop executed once
//to extract the 
//three grades
//for projects
for (int i =0;i<3;i++)
{
//take in number 
//from file
inFile >>number;
cout << number<<" ";
//use that number total
//the scores
scores+=number;
}
cout<<endl;
//find average of that percentage
//add it up find total score
totavg+=(scores/3)*.2 ;
//prints to file type and percentage accquired
//of that type to file
outFile << setw(14) << left << type << " " <<((scores/3)*.2)<<"%"  << endl;
//set scores to 0 
//so you can find the
//average of the next
//scores
scores =0;
cout <<type<<endl;
//gets string from file
inFile >>type;
//loop executed once to 
//extract two scores
//for Exam
for (int i =0;i<2;i++)
{
//take in number 
//from file
inFile >>number;
cout << number<<" ";
//use that number total
//the scores
scores+=number;
}
cout<<endl;
//find average of that percentage
//add it up find total score
totavg+=(scores/2)*.5 ;
//prints to file type and percentage accquired
//of that type to file
outFile << setw(14) << left << type << " " <<((scores/2)*.5) <<"%" << endl;
//prints out total average to file
outFile << setw(14) << left << "The total average is " << " " <<totavg <<"%" << endl;

//close infile.
inFile.close();
//close outfile
//this is important this 
//saves your data in your 
//output file.
outFile.close();

//you must return 
//somthing
return 0;


}







Last edited on
you would have to return something for


this means returning integer ->(int) readandwritescores();

int readandwritescores()
{
//code

return INTEGER;


}
this means returning nothing->(void) readandwritescores();
void readandwritescores()
{
//code
//dont need to return nothing

}

i rewrote the code to a void.
this is how the code should work

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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

using namespace std;

void readAndWriteScores();

int main ()
{
	readAndWriteScores();
return 0;
}
void  readAndWriteScores()


{
//files for streaming
ifstream inFile("scores.txt");
ofstream outFile("summary.txt");
//This checks to see if
//the file exists
//exit using exit (0);
if (inFile.fail())
{
	cout<<"Dose Not Exist";
	exit(0);
	
}
//initialize variables.
string type ="";
double scores=0;
double number=0;
double totavg=0;

//this i loop here
//runs 3 times
//for the
//Quiz 8 scores at 10% total grade
//Homework 8 scores at 10% total grade
//Classwork 8 scores at 10% total grade
//used it to loop three
//times because
//they all have same number
//of scores and percentage
for(int i =0 ; i < 3; i++)
{
//gets the string from file
inFile >>type;
cout <<type<<endl;
//this gets the eight scores
//3 times this for loop will
//be executed to get the
//Quiz
//Homework
//Classwork
for (int j =0;j<8;j++)
{
//take in number 
//from file
inFile >>number;
cout << number<< " ";
//use that number total
//the scores
scores+=number;
}
cout<<endl;
//find average of that percentage
//add it up find total score
totavg+=(scores/8)*.1 ;

//prints to file type and percentage accquired
//of that type
outFile << setw(14) << left << type << " " <<((scores/8)*.1)<<"%"  << endl;

//set scores to 0 
//so you can find the
//average of the next
//scores
scores =0;
}
cout <<type<<endl;
//gets string from file
inFile >>type;
//loop executed once
//to extract the 
//three grades
//for projects
for (int i =0;i<3;i++)
{
//take in number 
//from file
inFile >>number;
cout << number<<" ";
//use that number total
//the scores
scores+=number;
}
cout<<endl;
//find average of that percentage
//add it up find total score
totavg+=(scores/3)*.2 ;
//prints to file type and percentage accquired
//of that type to file
outFile << setw(14) << left << type << " " <<((scores/3)*.2)<<"%"  << endl;
//set scores to 0 
//so you can find the
//average of the next
//scores
scores =0;
cout <<type<<endl;
//gets string from file
inFile >>type;
//loop executed once to 
//extract two scores
//for Exam
for (int i =0;i<2;i++)
{
//take in number 
//from file
inFile >>number;
cout << number<<" ";
//use that number total
//the scores
scores+=number;
}
cout<<endl;
//find average of that percentage
//add it up find total score
totavg+=(scores/2)*.5 ;
//prints to file type and percentage accquired
//of that type to file
outFile << setw(14) << left << type << " " <<((scores/2)*.5) <<"%" << endl;
//prints out total average to file
outFile << setw(14) << left << "The total average is " << " " <<totavg <<"%" << endl;

//close infile.
inFile.close();
//close outfile
//this is important this 
//saves your data in your 
//output file.
outFile.close();





}



Topic archived. No new replies allowed.