file

I have made this code and it does not let me put in my file name. The compiler automatically goes away.

#include <iostream> // for input and output
#include <iomanip> // for formatting output
#include <string> // for class string
#include <fstream> // for file input/output
#include <cstdlib> // for exit(EXIT_FAILURE)

using namespace std;

const int MAXSTUDENT = 100;
const int BAR = 60;
enum GRADE {F, D, CMINUS, C, CPLUS, BMINUS, B, BPLUS, AMINUS, A};

class Student
{
private:
string nameFirst;
string nameLast;
double rawScore;
double percentRank;
GRADE pointGrade;
GRADE curveGrade;
string letterGrade;
public:
void setFirstName(string s)
{
nameFirst = s;
}
void setLastName(string s)
{
nameLast = s;
}
void setRawScore(double s)
{
rawScore = s;
}
void setPercent(double p)
{
percentRank = p;
}
void setPointGrade(GRADE g)
{
pointGrade = g;
}
void setCurveGrade(GRADE g)
{
curveGrade = g;
}
void setLetterGrade()
{
GRADE g;
if ((rawScore >= BAR) && (curveGrade >= pointGrade))
g = curveGrade;
else
g = pointGrade;
switch (g)
{
case F: letterGrade = "F"; break;
case D: letterGrade = "D"; break;
case CMINUS: letterGrade = "C-"; break;
case C: letterGrade = "C"; break;
case CPLUS: letterGrade = "C+"; break;
case BMINUS: letterGrade = "B-"; break;
case B: letterGrade = "B"; break;
case BPLUS: letterGrade = "B+"; break;
case AMINUS: letterGrade = "A-"; break;
case A: letterGrade = "A"; break;
}
}
string getFirstName()
{
return nameFirst;
}
string getLastName()
{
return nameLast;
}
double getRawScore()
{
return rawScore;
}
double getPercent()
{
return percentRank;
}
string getLetterGrade()
{
return letterGrade;
}

bool operator>(const Student& s)
{
return rawScore > s.rawScore;
}
bool operator>=(const Student& s)
{
return rawScore >= s.rawScore;
}
bool operator==(const Student& s)
{
return rawScore == s.rawScore;
}
bool operator<(const Student& s)
{
return rawScore < s.rawScore;
}
bool operator<=(const Student& s)
{
return rawScore <= s.rawScore;
}

};

class Course
{
private:
Student s[MAXSTUDENT];
int classSize;
double maxScore;
double minScore;
double avgScore;
public:
void grabData()
{
cout << "Enter the file name: ";
string filename;
cin >> filename; //get name of the class data file

// open file for reading and convert filename to C-string for robust run
ifstream ifs(filename.c_str());
// check whether the file is open properly
if (!ifs.is_open())
{
cout << "Error opening file: " << filename << endl;
exit(EXIT_FAILURE);
}

// set variables for reading file
string nameFirst;
string nameLast;
double rawScore;

classSize = 0; //initial number of students
// read the file until any error happened or EOF is reached
while (ifs >> nameFirst) //while ifs >> nameFirst is OK
{ //i.e. ifs.good() returns true
ifs >> nameLast >> rawScore; //get data
// store data in array Students
s[classSize].setFirstName(nameFirst);
s[classSize].setLastName(nameLast);
s[classSize].setRawScore(rawScore);
++classSize; //increase number of students in array
}
}
void setRank()
{
//sorting array using Bubble sort
for (int last = classSize - 1; last > 0; --last)
for (int cur = 0; cur < last; ++cur)
{
int cur1 = cur + 1;

if (s[cur] < s[cur1]) //swap students
{
Student tmp = s[cur];
s[cur] = s[cur1];
s[cur1] = tmp;
}

}
// first element of ordered array
maxScore = s[0].getRawScore();
// last element of ordered array
minScore = s[classSize - 1].getRawScore();

// calculate average score
avgScore = 0.0;
for (int i = 0; i < classSize; ++i)
avgScore += s[i].getRawScore();

double inverseSize = 1.0 ; // used for percentile calculations
if (classSize > 0)
{
inverseSize /= classSize;
avgScore /= classSize;
}

//calculate percentile rank: pr = (less + 0.5* same)/total
// using ordered array of students
for (int i = 0; i < classSize; ++i)
{
//count less values
int less = classSize - 1 - i;

//count same values
int same = 0;
//count same values before current
while (i > 0 && s[i - 1] == s[i])
{
++same;
--i;
}
//count same values after current
while (i < (classSize - 1) && s[i + 1] == s[i])
{
++same;
--less;
++i;
}

s[i].setPercent(inverseSize * (less + 0.5 * same));
}
}
void assignPointGrade()
{
for (int i = 0; i < classSize; ++i)
{
if (s[i].getRawScore() > 93.0)
s[i].setPointGrade(A);
else if (s[i].getRawScore() > 90.0)
s[i].setPointGrade(AMINUS);
else if (s[i].getRawScore() > 87.0)
s[i].setPointGrade(BPLUS);
else if (s[i].getRawScore() > 83.0)
s[i].setPointGrade(B);
else if (s[i].getRawScore() > 80.0)
s[i].setPointGrade(BMINUS);
else if (s[i].getRawScore() > 77.0)
s[i].setPointGrade(CPLUS);
else if (s[i].getRawScore() > 73.0)
s[i].setPointGrade(C);
else if (s[i].getRawScore() > 67.0)
s[i].setPointGrade(CMINUS);
else if (s[i].getRawScore() >= 60.0)
s[i].setPointGrade(D);
else
s[i].setPointGrade(F);
}
}
void assignCurveGrade()
{
for (int i = 0; i < classSize; ++i)
{
if (100.0 * s[i].getPercent() > 90.0)
s[i].setCurveGrade(A);
else if (100.0 * s[i].getPercent() > 75.0)
s[i].setCurveGrade(AMINUS);
else if (100.0 * s[i].getPercent() > 60.0)
s[i].setCurveGrade(BPLUS);
else if (100.0 * s[i].getPercent() > 45.0)
s[i].setCurveGrade(B);
else if (100.0 * s[i].getPercent() > 35.0)
s[i].setCurveGrade(BMINUS);
else if (100.0 * s[i].getPercent() > 25.0)
s[i].setCurveGrade(CPLUS);
else if (100.0 * s[i].getPercent() > 15.0)
s[i].setCurveGrade(C);
else if (100.0 * s[i].getPercent() > 10.0)
s[i].setCurveGrade(CMINUS);
else if (100.0 * s[i].getPercent() >= 5.0)
s[i].setCurveGrade(D);
else
s[i].setCurveGrade(F);
}
}
void assignLetterGrade()
{
for (int i = 0; i < classSize; ++i)
{
s[i].setLetterGrade();
}
}
void displayGradeRoster()
{
//header
cout << "\n Grade Roster" << endl;
int w = 14; //width of column (for tuning output look)
cout << left << setw(16) << "First Name";
cout << setw(16) << "Last Name" << setw(w) << "Raw Score";
cout << setw(w) << "Percentile" << setw(w) << "Final Grade";
cout << endl;
// output students
for ( int i = 0; i < classSize; ++i)
{
cout << left << setw(16) << s[i].getFirstName();
cout << setw(16) << s[i].getLastName();
cout << setw(w) << s[i].getRawScore();
cout << setw(w) << s[i].getPercent();
cout << setw(w) << s[i].getLetterGrade();
cout << endl;
}
}
};

int main()
{
Course c;
c.grabData();
c.setRank();
c.assignPointGrade();
c.assignCurveGrade();
c.assignLetterGrade();
c.displayGradeRoster();

return 0;
}
@sonia123

I downloaded your program and created the following file, called "input.txt"
Tom   Brown     10
Dick  Turbin    20
Harry Hotspur   30


When I ran your program it produced the following output:
Enter the file name: input.txt

 Grade Roster
First Name      Last Name       Raw Score     Percentile    Final Grade   
Harry           Hotspur         30            0.666667      F             
Dick            Turbin          20            0.333333      F             
Tom             Brown           10            0             F  


I can assure you that my compiler did not run away.


Please:
(1) Be more explicit about the problem you are facing: your description made no sense.
(2) Please always post code with code tags around it: you can find the <> item in the format menu to the right.
@sonia123

Please specify your problem, as your description made no sense in accordance to my findings, or at all as a matter of fact. There were no problems with your program at least from the description of what you specified it was supposed to do?? As @lastchance mentioned, the compiler does not "go away" Please note on your end that a file name is not allowed to contain character ":". Best of luck.
Topic archived. No new replies allowed.