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 138 139
|
#include <iostream>
#include <cstring>
#include <iomanip>
#include <fstream>
using namespace std;
struct Info{
char fName[11];
char lName[13];
char id[7];
double scores[7];
};
const int MAX_STUDENTS = 40;
void dropGrade (Info, double[], double[], double);
void calcAvg (double[], double[], double[], double);
void convertAvg (double[], char[], int);
int main ()
{
double numbStudents = 0;
double counter [MAX_STUDENTS];
double total[MAX_STUDENTS];
double avg[MAX_STUDENTS];
char ltrGrade[MAX_STUDENTS];
Info students[MAX_STUDENTS];
Info ptrStudents;
ifstream fin;
fin.open ("//Users//Ponno//Desktop//CSProjects//student_input.dat");
if (!fin)
cout << "input file failed to open\n";
for (int i = 0; i < MAX_STUDENTS && !fin.eof(); i++)
{
counter[i] = 0;
for (int j = 0; j < 10; j++) // ----store first name
{
if (isspace((char) fin.peek()) == 0)
fin >> students[i].fName[j];
else
students[i].fName[j] = '\0';
}
while (isspace((char) fin.peek()) != 0)
fin.ignore();
for (int j = 0; j < 12; j++) //------ store last name
{
if (isspace((char) fin.peek()) == 0)
fin >> students[i].lName[j];
else
students[i].lName[j] = '\0';
}
while (isspace((char) fin.peek()) != 0)
fin.ignore();
for (int j = 0; j < 6; j++) //-------- store ID number
{
if (isspace((char) fin.peek()) == 0)
fin >> students[i].id[j];
else
students[i].id[j] = '\0';
}
while (isspace((char) fin.peek()) != 0)
fin.ignore();
for (int j = 0; j < 6; j++) // ----- store grades
{
fin >> students[i].scores[j];
counter[i]++; // ------me kind of cheating to find avg
if((char) fin.peek() == '\n' && j == 4)
{
cout << "Student #: "<< i + 1 << " only has 5 exam grades."
<< " avg will be computed with only 5 grades\n\n";
}
if (students[i].scores[j] < 0)
{
cout << "Student has a negative test score.\n";
return 1;
}
}
if (isspace((char) fin.peek()) != 0)
fin.ignore();
if (fin.eof())
break;
numbStudents++;
}
//dropGrade (students, total, counter, numbStudents);
//calcAvg (total, avg, counter, numbStudents);
//convertAvg (avg, ltrGrade, numbStudents);
fin.close();
return 0;
}
void dropGrade (Info students[], double total[],int counter[],
int numbStudents)
{
double lowest[numbStudents];
for (int i = 0; i < numbStudents; i++)
{
total[i] = 0;
lowest[i] = students[i].scores[0];
for (int j = 0; j < 6; j++)
{
if (lowest[i] > students[i].scores[j])
lowest[i] = students[i].scores[j];
total[i] = total[i] + students[i].scores[j];
}
if (counter[i] == 6)
total[i] = total[i] - lowest[i];
}
}
void calcAvg (double total[], double counter[], double avg[], double numbStudents)
{
for (int i = 0; i < numbStudents; i++)
{
avg[i] = 0;
for (int k = 0; k < numbStudents; k++) //just to reset average
{
avg[i] = total[i] / counter[i];
}
}
}
void convertAvg (double avg[], char ltrGrade[], int numbStudents)
{
for (int i = 0; i < numbStudents; i++)
{
if (avg[i] >= 89.5)
ltrGrade[i] = 'A';
else if (avg[i] >= 79.5 && avg[i] < 89.5)
ltrGrade[i] = 'B';
else if (avg[i] >= 69.5 && avg[i] < 79.5)
ltrGrade[i] = 'C';
else
ltrGrade[i] = 'F';
}
}
|