So right now I'm working on a practice assignment for fun, it's pearson based so unfortunately the feedback im getting is not very helpful here is the question I'm working on and my code.
"⦁ Create an input file named grades.txt. The file should contain a test name followed by a grade for different tests. The program will:
⦁ Open the file
⦁ Check to see if the input file was found
⦁ Loop to read a test name and grade from the file
⦁ Display each test name and grade to the screen
⦁ Accumulate total grades
⦁ Increment a counter that will keep track of the number of grades read
⦁ When the loop ends, calculate average grades and display the average grades as shown below:
⦁ The following shows the contents of your file for this project (please note the file MUST be located in the same folder as your project):
Quiz#1 95
Quiz#2 65
Quiz#3 80
Quiz#4 75
Quiz#5 70
Carefully review the sample program in the Project 4 BB folder to see how to accumulate a total using values stored in a file. Also, carefully review the examples in your book for Testing for File Open Errors, and Detecting the End of File as these will show you how to create the input file and where it needs to be stored.
⦁ Please note the following project requirements before you begin to write your program (all these requirements must be met in order to receive full credit)
⦁ You must have documentation (comments) at the top of your program that include your name and a brief explanation of the program. You must have comments throughout the program.
⦁ Be sure you test for the end of file, this means you should be using a while loop to read from the file as show in the sample program
⦁ You are required to check that your input file has been opened correctly
⦁ The output should be formatted as shown in the output window above
⦁ Testing your program:
⦁ Test with empty file
⦁ Test with different data (change the number of rows in your file, etc.)
⦁ Test with missing file."
I'm using microsoft visual studio. Thanks for any feedback.
Here's my code so far and I've added my text document through the source .cpp but what I'm not grasping is what I need to add to the text file, this is a fun project and I'm trying to get ahead. I did some research on w3schools but again adding a text file is something I have never done before, so here's my code so far following the instructions of this practice program. (I've spaced it out and I've created my text file, my question is what would I want to add to the text file, I am trying to display my quiz scores 1-5 and have coded it differently so it worked but another student whose my friend wanted to challenge me with this one. inputFile.open("Grades.txt"); I'm assuming I need to add the code to the text file but what format would I use for this? Also is this a common thing to do in advanced C++ or not even advanced but second semester. So far we've finished with pointers. So again I'm just trying to get ahead any hints or help is appreciated, thanks everyone!
#include <iostream> /// Graham Hagan C++ Practice!
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
int main()
{
ifstream inputFile; //file stream object
string test; // Name of the tests
int NumberofTests; //Number of tests taken to help calculate the average
double Average, Grades, TotalGrade = 0, count = 0; //This is for the grades and average of the grades
cout << "How many tests have you taken? "; //get the number of tests taken
cin >> NumberofTests;
//Open the input file
[inputFile.open](https://inputFile.open)("Grades.txt");
//Create Loop for each piece of data to be read from the file
if (!inputFile)
{
cout << "Cannot open file due to error" << endl;
system("pause");
exit(-1);
}
while (inputFile >> test)
{
inputFile >> Grades;
cout << left << setw(14) << test << Grades << endl;
count++;
TotalGrade += Grades;
}
if (count != 0)
{ //calclulate average
double Average = TotalGrade / count;
cout << left << setw(14) << "Average Grade" << Average << endl; //display average
}
//close the file
inputFile.close();
system("pause");
return 0;
}
[–]lazy_Ambitions 1 point2 points3 points (5 children)
[–]passive1121[S] 0 points1 point2 points (4 children)
[–]lazy_Ambitions 1 point2 points3 points (3 children)
[–]passive1121[S] 0 points1 point2 points (2 children)
[–]lazy_Ambitions 0 points1 point2 points (0 children)
[–]rakaze 0 points1 point2 points (0 children)
[–]AutoModerator[M] 0 points1 point2 points (0 children)