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" ) This is the code I have: 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.  ////////////////////////////////////////////#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 16, 2019
18 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

" )

This is the code I have:

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.

 

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

#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

As you are not able to read the file students.txt, here is the source code with little modification in your code.

Step 2

Source Code

#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

char fileName[20]; // 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

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

cin >> fileName;

inFile.open(fileName);

if (!inFile)

...

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

Object Oriented Programming

Related Computer Science Q&A

Find answers to questions asked by student like you

Show more Q&A add
question_answer

Q: Weighted Intervals Problem EXAMPLE:Suppose we have the following weighted intervals. start   finish ...

A: Instructions for solving the problem.Sort the given intervals by finish time, and the intervals with...

question_answer

Q: In C++   Create a function called SuperRandomNumber. It should accept a number called top as the arg...

A: Note:The following program creates a function “SuperRandomNumber()” which accepts “top” as argument ...

question_answer

Q: For this program you need to use Eclipse. Carefully read the instructions and write a program that r...

A: The program has the following algorithm: Step 1: Create object for “Scanner” class to read the user ...

question_answer

Q: write a program in c++. You work for a box company, and have been asked to write a program to compar...

A: Click to see the answer

question_answer

Q: This program should compound interest continuously. It asks the user for the number of times they wa...

A: Formula for calculating compound interest=P(1+R/N)NTP – Principle amountR – Rate of the interest, an...

question_answer

Q: Select Invoice_number, invoice_due_date, payment_date and the number of days late for those invoices...

A: Note: The given query has been compiled and executed in "sqlfiddle" online compiler. Creating an “In...

question_answer

Q: 6. Write a class with a constructor that accepts a String object as its argument. The class should h...

A: Two java.util classes are used – Scanner and Arrays. Scanner is used to read input string and Arrays...

question_answer

Q: You are not required to execute the queries. However, your syntax must by accurate.You must give at ...

A: Since the full table structure is not given we make suitable assumptions to write the query:For ques...

question_answer

Q: (The Rectangle class) Following the example of the Circle class in Section 8.2,design a class named ...

A: Note: The given program is compiled and executed in "NetbeansIDE"