I have a bit of code that is intended to read a file ("students.txt") and calculate the average, highest, and lowest scores from given information. (students.txt reads: "Mark Thompson 72.50James Taylor 100.00Daniel Price 80.2Busy Phillips 63.0" ) I have tweaked it to output the highest and lowest but I cannot get it to read the file. I have saved the file into the directory of the program but nothing seems to be working. I am also stumped on the formatting of the calculations. If nothing else, since I cannot seem to get the file to open, I need these values included as well as the commands to calculate the noted answers (average, highest, and lowest) in the output. This is the code that I have so far:  ////////////////////////////////////////////#include <iostream>#include <fstream>#include <cmath>#include <string>#include <iomanip>using namespace std;int main() {ifstream inFile; // This is a declaration of the inFile that holds all the gradesstring fileName; // This is the filename that the user will enterconst int min = 63.0; // This is a constant min used to find the lowest scoreconst int max = 100.0; // This is the constant max used to find the highest scorefstream instream;instream.open("students.txt"); // Opens filecout << "Enter the input file name: "; // Prompts user to express desired file to be readcin >> fileName;if (!instream)cout << "File did not open correctly" << endl; // Alerts user that file did not open properly.return 1;cout << "The highest grade of " << max << " is held by James Taylor." << endl; // Articulates student with the highest gradecout << "The lowest grade of " << min << " is held by Busy Phillips." << endl; // Articulate student with the lowest gradeinstream.close();return 0;}

Question
Asked Sep 17, 2019
12 views

I have a bit of code that is intended to read a file ("students.txt") and calculate the average, highest, and lowest scores from given information.

(students.txt reads: "

Mark Thompson 72.50
James Taylor 100.00
Daniel Price 80.2
Busy Phillips 63.0

" )

I have tweaked it to output the highest and lowest but I cannot get it to read the file. I have saved the file into the directory of the program but nothing seems to be working. I am also stumped on the formatting of the calculations. If nothing else, since I cannot seem to get the file to open, I need these values included as well as the commands to calculate the noted answers (average, highest, and lowest) in the output.


This is the code that I have so far:

 

////////////////////////////////////////////

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

int main() {

ifstream inFile; // This is a declaration of the inFile that holds all the grades
string fileName; // This is the filename that the user will enter
const int min = 63.0; // This is a constant min used to find the lowest score
const int max = 100.0; // This is the constant max used to find the highest score


fstream instream;
instream.open("students.txt"); // Opens file

cout << "Enter the input file name: "; // Prompts user to express desired file to be read
cin >> fileName;

if (!instream)

cout << "File did not open correctly" << endl; // Alerts user that file did not open properly.
return 1;

cout << "The highest grade of " << max << " is held by James Taylor." << endl; // Articulates student with the highest grade
cout << "The lowest grade of " << min << " is held by Busy Phillips." << endl; // Articulate student with the lowest grade
instream.close();


return 0;
}

check_circle

Expert Answer

Step 1

Description:

The following program is to print the highest, lowest, and average of test scores by reading the information from “students.txt” file.

Note: The following program adds some of additional variables with the given code and then it is  used to calculate the highest, lowest, and average of test scores.The comment line in the program helps to understand the functionality of each line in the program.

Step 2

Program (Editable Code):

//Include the header file section
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
 
//Main function
int main()
{
//Variable to store student's grade
float grade;  
//  variable to hold all the grades
ifstream inFile; 
// Variable to store the file name
string fileName;   
// Variable to store the name of the student
string name;          
// Variable to keep the count of the number of grades 
int gradeCount=0;
// Assign the constant MIN to find the lowest score
double MIN = 63.0;  
double min; 
// Assign the constant MIN to find the highest score
double MAX = 100.00;
double max;
//Variable to sum all the grades
double sum =0.0; 
//Variable to average all the grades
double average;       
 
/* Prompt the user to input the file name and assigns it
to fileName*/
cout << "Enter the input file name: ";
cin >> fileName;
// Open the file with the grades
inFile.open(fileName.c_str());
// Check to make sure the file opened correctly
if (!inFile)
{
//Error message
cout << "File did not open correctly" << endl;
return 1;
}
 
//Execute the for loop four four records in file
for (int i = 0; i < 4; i++)
{
// Begin the end of file loop
while (!inFile.eof())
{
//Assign the values
min = MIN;
max = MAX;
//Read the name of student
inFile >> name;
// Read in the grade
inFile >> grade;
// Find the sum of the the grades
sum = sum + grade;
// check if the grade is less than MIN
if (grade < MIN)
//Assign the lowest grade
min = grade;
// check if the grade is less than MIN
if (grade > MAX)
//Assign the highest grade
max = grade;
//Increment the gradeCount
gradeCount++;
//Calculate the average
average = sum/gradeCount;
}
}
//Print the lowest grade
cout << "\nLowest grade:" << min;
//Print the highest grade
cout << "\nHighest grade:" << max;
// Print the average
cout << "\nAverage of grades:" << average << endl;
system("pause");
return 0;
}
Step 3

Screenshot of program:...

help_outline

Image Transcriptionclose

//Include the header file section #include <iostream> #include <fstream> #include <iomanip> #include <string using namespace std //Main function int main { //Variable to store student's grade float grade; /variable to hold all the grades ifstream inFile; 1Variable to store the file name string fileName; 1Variable to store the name of the student string name; 1Variable to keep the count of the number of grades int gradeCount=0; /Assign the constant MIN to find the lowest score double MIN = 63.0; double min; / Assign the constant MIN to find the highest score double MAX = 100.00 double max; //Variable to sum all the grades double sum =0.0; //Variable to average all the grades double average;

fullscreen

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: Using the following truth table, write the equivalent Boolean equations in Sum of Product and Produc...

A: Answer:Sum of Product (SOP) form:If the input is 1, then write an AND term for each input combinatio...

question_answer

Q: I was asked to add the three types of comments to this class, each consisting of the same statement ...

A: Java documentation comment: This comment is used to create documentation API. The syntax for this co...

question_answer

Q: Are climate change models accurate?

A: DescriptionClimate Models are basically an add-on of weather forecasting models. But these models ar...

question_answer

Q: Program is not run; error found in print(row) # Import the python CSV moduleimport csv# Create a pyt...

A: Explanation:To perform operations on CSV file, the user should import default module called “csv”.“c...

question_answer

Q: Complete the following programming assignment. Use good programming style and all the concepts previ...

A: Program Instructions:Create four methods named insertionSort(), bubblesort(), mergesort() and quicks...

question_answer

Q: Write a program to convert between KPH and MPH in either direction.If you are driving across the bor...

A: The algorithm for the program would be as below:Declare one int variable for accepting user menu cho...

question_answer

Q: Write a class AnalyzeNumbers which asks the user to enter the number of inputs andstores them in an ...

A: Program plan:Define the class “AnalyzeNumbers”.Define the “main()” method.Create object for “Scanner...

question_answer

Q: What can you show me about Python rules and methods?

A: Note:The following summary is about some syntax rules followed in python and rules to define a metho...

question_answer

Q: Part 1: Stack As mentioned in class, Python 3 provides the LifoQueue class as part of the Queue libr...

A: Import the given class in another file.Create the function postfix() which takes a string as its arg...