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 140 141 142 143 144 145 146 147 148 149 150 151 152
|
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
struct StudentType
{
std::string first_name;
std::string last_name;
char grade;
int test_score;
// If you don't initialize 'grade' and 'test_score', their initial values
// are unknown (= garbage).
};
int readData(std::ifstream& input_file, StudentType* students, std::size_t size);
void getGrade(StudentType students[], int list_size);
void highestScore(std::ofstream& output_file, StudentType students[], int n);
void displayData(std::ofstream& output_file, StudentType students[], int n);
int main ()
{
std::ifstream input_file( "Ch9_Ex2Data.txt");
if (! input_file) {
std::cout << "Cannot open Ch9_Ex2Data.txt.\nExiting now.\n";
return 1;
}
constexpr int Students_Max { 20 }; // just to avid magic numbers
StudentType students[Students_Max]; // use std::vector instead!!
// students[] is uninitialized here!
// It means *all* the 'grades' and the 'test_score' might contain garbage
// values.
// You are neglecting the function return value!
// This will cause several errors, since you don't need to analyze the
// entire C-style array "students[]", but only the part you have assigned
// values to.
// Remember: the part you have neither initialized nor assigned might
// contain garbage values.
readData (input_file, students, Students_Max);
// Neither inilialized nor assigned any good value; see previous comment:
int n; // bad name! Do use a meaningful name
// getGrade will take into consideration an unknown number of elements,
// since we cannot now what's there in 'n':
getGrade (students, n);
std::ofstream output_file( "Ch9_Ex2Out.txt");
// You're NOT checking if 'output_file' has been properly opened!!
// Let's check this part later...
// But read the comments inside the functions body.
highestScore (output_file, students, n);
displayData (output_file, students, n);
system("pause");
return 0;
}
// Data example:
// Duckey Donald 85
// Goof Goofy 89
// Brave Balto 93
// Snow Smitn 93
// Alice Wonderful 89
// Samina Akthar 85
// Simba Green 95
// Donald Egger 90
// Brown Deer 86
// Johny Jackson 95
// Greg Gupta 75
int readData(std::ifstream& input_file, StudentType* students, std::size_t size)
{
for (int n = 0; n < size && input_file; ++n)
{
input_file >> students[n].first_name
>> students[n].last_name
>> students[n].test_score;
}
return n;
}
// What should this funtion do?
// Shouldn't it loop through the C-style array and assign values to ... ?
// Re-read it: as it is now, it does pretty nothing:
void getGrade(StudentType students[], int list_size)
{
int score; // uninitialised local variable
char grade; // uninitialized local variable
if (score >= 90) {
grade = 'A';
} else if (score >= 80) {
grade = 'B';
} else if (score >= 70) {
grade = 'C';
} else if (score >= 60) {
grade = 'D';
} else {
grade = 'F';
}
}
// Bad parameter name: give it a meaningful name-------------\/
void highestScore(std::ofstream& output_file, StudentType students[], int n)
{
int max = 0;
for (int i = 1; i < n; ++i)
{
if (students[i].test_score > students[max].test_score) {
output_file << "Higest test score: "
<< students[max].test_score << '\n';
}
// Are your sure this part should be inside the for loop?
output_file << "Student names having the highest test score: " << '\n';
// Aren't you missing something here? ------------------------^
// Aren't you going to do anything with the 'max' value?
}
for (i = 0; i < n; ++i)
{
if (students[i].test_score == students[max].test_score) {
output_file << students[i].last_name << ", "
<< students[i].first_name << '\n';
}
}
}
// Bad parameter name: give it a meaningful name-------------\/
void displayData(std::ofstream& output_file, StudentType students[], int n)
{
for (int i = 0; i < n; i++)
{
output_file << left << students[i].last_name << ","
<< students[i].first_name << ' '
<< students[i].test_score << ' '
<< students[i].grade << '\n';
}
}
|