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 #include #include #include #include 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; }
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
////////////////////////////////////////////
#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;
}
Step by step
Solved in 2 steps