Dec 8, 2012 at 11:17am UTC
Ok I think I'm headed in the right direction, but still can't get this to work. I just can't get the right data to save into the arrays for some reason. I was able to calculate the correct letter grade and output to new file, but I want to then read all 3 rows into arrays so I can then be able to run menu options 2, 3 and 5. But the data it outputs is not right. :( why why why?
Sample "TestScores_Grades.txt" to read from:
10214 8 A-
19285 3 F
11753 5 B
11261 3 F
13893 -1 F
17611 7 A-
10587 3 F
15568 10 A
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 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <string>
using namespace std;
int main()
{
const char FILENAME[30] = "TestScores.txt" ;
ifstream inFile;
int id, score, avg, s = 0, numofstudents = 0, totalgrade = 0;
inFile.open(FILENAME);
while (!inFile.eof())
{
inFile >> id;
inFile >> score;
if (score >= 0 && score <= 10)
{
totalgrade = totalgrade + score;
numofstudents++;
s = s + 1;
}
}
avg = totalgrade/numofstudents;
inFile.close();
ifstream inFile2;
ofstream outFile1;
int idx;
string grade;
inFile2.open(FILENAME);
outFile1.open("TestScores_Grades.txt" );
for (idx = 0; idx < s; idx++)
{
inFile2 >> id >> score;
if (score == avg || score == avg +1)
grade = "B" ;
else if (score == avg + 2 || score == avg + 3)
grade = "A-" ;
else if (score == avg +4 || score == 10 )
grade = "A" ;
else if (score == avg - 1)
grade = "C" ;
else if (score <= (avg - 2))
grade = "F" ;
outFile1 << id << " " << score << " " << grade << endl;
}
inFile2.close();
ifstream inFile3;
int num, ids[idx], scores[idx];
char grades[idx];
inFile3.open("TestScores_Grades.txt" );
idx = 0;
while (idx <= s && !inFile3.eof())
{
inFile3 >> ids[idx] >> scores[idx] >> grades[idx];
idx++;
}
inFile3.close();
do
{
cout << setw(30) << "QUIZ RESULTS MENU " << endl;
cout << setw(30) << "***************************" << endl << endl;
cout << " 1. " << left << setw(30) << "List by Student ID" << endl;
cout << " 2. " << setw(30) << "List by Student ID and Grades" << endl;
cout << " 3. " << setw(30) << "List by Letter Grade" << endl;
cout << " 4. " << setw(30) << "List by Numeric Grade" << endl;
cout << " 5. " << setw(30) << "List Grades by Percentage" << endl << endl;
cout << right << "Enter an option: " ;
cin >> num;
cout << endl;
if (num == 1)
cout << "This option sorts the student list by ID and writes it to a file." ;
if (num == 2)
{
//Read the new file and search for the target ID that the user enters.
ifstream inFile4;
inFile4.open("TestScores_Grades.txt" );
int target;
for (idx = 0; idx < s; idx++)
{
cout << "Enter a student ID: " ;
cin >> target;
if (ids[idx] = target)
{
cout << "Result: " << ids[idx] << " " << scores[idx] << " " << grades[idx] << endl;
}
}
}
else if (num == 3)
//cout << "This option searches for students with a particular letter grade." << endl << endl;
{
//Read the file again and search for the target letter grade that the user enters.
ifstream inFile4;
inFile4.open("TestScores_Grades.txt" );
char targetGrade;
for (idx = 0; idx < s; idx++)
{
cout << "Enter a letter grade: " ;
cin >> targetGrade;
if (grades[idx] = targetGrade)
cout << "Result: " << ids[idx] << " " << scores[idx] << " " << grades[idx];
}
}
else if (num == 4)
cout << "This option sorts the list by numeric grade." ;
else if (num == 5)
cout << "This option calculates the % of students with a particular grade." ;
else if (num < 1 || num > 5)
cout << "This is an invalid entry. Please rerun the program." ;
cout << endl << endl << endl;
}
while (num > 0 && num <= 5);
system("PAUSE" );
return EXIT_SUCCESS;
}
Last edited on Dec 9, 2012 at 3:27pm UTC
Dec 8, 2012 at 11:58am UTC
your coding very blur. please arrange to code using the code button
and i roughly see for few lines.
i saw that for your first inFile
you does not save it into array
while (!inFile.eof() )
try change and post problem here again
Dec 9, 2012 at 3:28pm UTC
Oops. sorry about that Felicia. Not sure how I didn't notice that when I submitted. I just edited. How is it now?
Dec 10, 2012 at 3:45am UTC
Thanks for responding SamuelAdams!
The first file "TestScores.txt" is the original file with just 2 columns (ID's and raw number scores). The 2nd file "TestScores_Grades.txt" is created when I calculate the letter grades into the 3rd column and then I read from that file the rest of the time.
Th -1 is supposed to mean the student didn't take the test. Yea I have to reconcile those scores. Forgot those were in there, but they shouldn't be calculated into the average and I think I have that covered. Let me work on that.
Thanks for the inFile.fail!