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 score fstream instream;instream.open("students.txt"); // Opens file cout << "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;}
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;
}
Trending now
This is a popular solution!
Step by step
Solved in 7 steps with 5 images