[go: up one dir, main page]

0% found this document useful (0 votes)
18 views3 pages

Exame One Program v2

This document outlines a C++ program that calculates the average of scores read from a file named 'scores.txt' and writes the formatted results to 'scores.out'. It includes functions for calculating the average, formatting data, and managing input/output streams. The program is designed for educational purposes, specifically as a reference for an exam.

Uploaded by

Daniel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

Exame One Program v2

This document outlines a C++ program that calculates the average of scores read from a file named 'scores.txt' and writes the formatted results to 'scores.out'. It includes functions for calculating the average, formatting data, and managing input/output streams. The program is designed for educational purposes, specifically as a reference for an exam.

Uploaded by

Daniel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

1: //*******************************************************

2: //This program computes the average of a group of scores.


3: //The scores are read from a file named scores.txt, formatted and written to an out
4: //along with the average
5: //File Name: exam_one_program.cpp
6: //Description: Program used as reference for questions on Exam One
7: //Last Changed: June 2014
8: //*****************************************************************
9:
10: #include <iostream>
11: #include <iomanip>
12: #include <fstream>
13: #include <cstdlib>
14: using namespace std;
15:
16: int calculateStudentAverage(ifstream& inp, double& courseAvg);
17: //calculate the average of a group of scores and number of entries
18: void formatData(istream& inp, ostream& oup, int precision, double avg);
19: // Read numeric values from the original stream and write those numbers to the
20: // updated stream with the precision defined by precision. Return the count of
21: // numbers read and the average value of the numbers
22: void setOutputFormat(ostream out, int decimal_places);
23: // Set the number of decimal places in an output stream
24: void waitForUser();
25: // Hold the program on the screen until the user hits any character
26:
27: int main( )
28: {
29: ifstream inF;
30: ofstream outF;
31: double avgValue;
32: int valueCount = 0, blankCount = 0;
33:
34: cout << "CSC-160 Exam One Support Program" << endl;
35: setOutputFormat(cout, 3);
36:
37: inF.open("scores.txt");
38: if (!inF.fail())
39: {
40: outF.open("scores.out");
41: if (!outF.fail())
42: {
43: valueCount = calculateStudentAverage(inF, avgValue);
44: formatData(inF, outF, 3, avgValue);
45:
46: cout << "\n\nFormatting Completed: "
47: << "\nInput File had "
48: << valueCount
49: << " values, with an average value of "
50: << avgValue;
51: }
52: else
53: cout << endl << endl << "Unable to open output file";
54: }
55: else
56: cout << endl << endl << "Unable to open input file";
57:
58: inF.close();
59: outF.close();
60: waitForUser();
61: return 0;
62: }
63:
64: void setOutputFormat(ostream out, int decimal_places)
65: {
66: out.setf(ios::fixed);
67: out.setf(ios::showpoint);
68: out.precision(decimal_places);
69: }
70:
71:
72: void formatData(istream& in, ostream& out, int decimalPlaces, double avg)
73: {
74: double value = 0;
75:
76: setOutputFormat(out, decimalPlaces);
77:
78: in >> value;
79: while (!in.eof())
80: {
81: out << value << endl;
82: in >> value;
83: }
84: out << "Average is: " << avg <<endl;
85:
86: return;
87: }
88:
89:
90: int calculateStudentAverage(ifstream& inp, double& courseAvg)
91: {
92: double totalScore = 0.0;
93: int numberOfStudents = 0;
94: int score;
95:
96: do
97: {
98: inp >> score;
99: totalScore = totalScore + score;
100: numberOfStudents++;
101: inp >> score;
102: }while (!inp.eof());
103:
104: if (totalScore > 0.0 && !(numberOfStudents == 0))
105: courseAvg = totalScore / numberOfStudents;
106: else
107: courseAvg = 0;
108:
109: return numberOfStudents;
110: }//end calculate Average
111:
112: void waitForUser()
113: {
114: cout << endl << endl;
115: system("PAUSE");
116: }
117:

You might also like