I need to test the program with the text file I made called scores.txt, but I am having trouble doing so. Seems like the program can't find it. The text file contains this information:
76, 89, 150, 135, 200, 76, 12, 100, 150, 28, 178, 189, 167, 200, 175, 150, 87, 99, 129, 149, 176, 200, 87, 35, 157, 189
Any help would be greatly appreciated. Thank You.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
|
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
int score;
int scoreRange[8] = {0};
ifstream ifile;
string fileName;
cout << "Enter the name of the file containing the students' test scores: ";
cin >> fileName;
ifile.open(fileName.c_str());
while (!ifile)
{
cout << "Error opening the file " << fileName << endl;
cout << "Please try again: ";
cin >> fileName;
ifile.clear();
ifile.open(fileName.c_str());
}
while (ifile >> score)
{
if (score < 25)
scoreRange[0]++;
else if (score < 50)
scoreRange[1]++;
else if (score < 75)
scoreRange[2]++;
else if (score < 100)
scoreRange[3]++;
else if (score < 125)
scoreRange[4]++;
else if (score < 150)
scoreRange[5]++;
else if (score < 175)
scoreRange[6]++;
else if (score <= 200)
scoreRange[7]++;
else
cout << "Invalid score found" << endl;
}
cout << endl << "Number of students in each score range:"<< endl;
for (size_t i = 0 ; i < 8; i++)
{
cout << "Number of scores between " << i * 25 << " - " << (i+1)*25 << ": "<< scoreRange[i] << endl;
}
}
|