Topic: Files and Streams
Codes discussed will be posted here. Write your conclusion
File name: "Ch3_AverageTestScoreVersion2.ccp"
Code:
*Note: the topic is about Files and Streams and needs to be done and explained properly, do not give answers which is not even the right one
*Note: use the link below to view the codes
https://drive.google.com/file/d/1xts7g8JC9BAgZ8X362FMSGuu_poll9On/view?usp=sharing
//************************************************************
// Author: D.S. Malik
// Program to calculate the average test score.
// Given a student's name and five test scores, this program
// calculates the average test score. The student's name, the
// five test scores, and the average test score is stored in
// the file testavg.out. The data is input from the file
// test.txt.
//************************************************************
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
//1. Declare the variables.
ifstream inFile; //input file stream variable
ofstream outFile; //output file stream variable
double testScore;
double sum = 0;
string firstName;
string lastName;
//2. Open the input file.
inFile.open("test.txt");
//3. Open the output file.
outFile.open("testavg.out");
outFile << fixed << showpoint;
//4. To output the floating-point numbers in a fixed decimal format with a decimal point
outFile << setprecision(2);
cout << "Processing data" << endl;
//5. Read the student Full Name.
inFile >> firstName >> lastName;
//6. Output the student Full Name.
outFile << "Student name: " << firstName<< " " << lastName << endl;
outFile << "Test scores: ";
//7. Read the five test scores.
inFile >> testScore; //Read the first test score
//8. Output the five test scores.
outFile << setw(6) << testScore; //Output the test score
sum = sum + testScore; //Update sum
inFile >> testScore; //Read the second test score
outFile << setw(6) << testScore; //Output the test score
sum = sum + testScore; //Update sum
inFile >> testScore; //Read the third test score
outFile << setw(6) << testScore; //Output the test score
sum = sum + testScore; //Update sum
inFile >> testScore; //Read the fourth test score
outFile << setw(6) << testScore; //Output the test score
sum = sum + testScore; //Update sum
inFile >> testScore; //Read the fifth test score
outFile << setw(6) << testScore; //Output the test score
sum = sum + testScore; //Update sum
outFile << endl;
//9. Find the average test score.
//10. Output the average test score.
outFile << "Average test score: " << setw(6)<< sum / 5.0 << endl;
//11. Close the input and output files.
inFile.close();
outFile.close();
cin>>sum;
return 0;
}
Conclusion:
The objective of this program was to create a program that calculate s the average test score using data from the file "test.txt".
To read and write information I have used the header file <fstream>. It represents both output Stream and input Stream. So this standard library allows to read from files and write to files.
The folowing code "inFile.open("test.txt");" opens the input file "test.txt". The code "outFile.open("testavg.out");" opens the output file "testavg.out". Stream variable "inFile" reads information from the file and output file stream variable "outFile" saves data to the file.
These two lines of code close the input and output files: inFile.close();
outFile.close();
Comments
Leave a comment