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 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
|
#include <iostream>
#include <string>
#include<fstream>
#include <iomanip>
#define CAP 25
using namespace std ;
struct Student{
string Course_Name,Course_Id, Course_Location;
string FirstName, LastName;
float Quiz, Test[6], Average;
int Id;
char Grade;
} ;
bool Open_File(ofstream &fout);
float Find_Lowest(Student List[], float&Min);
bool Open_File(ifstream &fin);
void Read_Student(Student & Temp, ifstream & fin);
int Read_list(Student List[], int Max_Size);
void Print_List(Student & Temp, Student List[], int Size);
float calcAverage(Student List[],float Min);
void Print_Course(Student List[],ofstream&fout, int Size);
int Search(Student List[], int Size, string Target);
void LookUp_Student(Student List[], int Size);
void Menu();
void Process_Menu(Student List[], int Size, float Min);
void Print_Student(Student & Temp);
void Print_Search(Student Temp);
Student Read_Student();
Student Process_Grade(Student & Temp, Student List[], float Min, int Size);
char Grade_it(Student List[], Student & Temp);
int main()
{
ifstream fout;
float Min=0;
Student Temp;
Student stu[CAP];
int Logical_Size = 0 ;
Logical_Size = Read_list(stu,CAP);
if (Logical_Size > 0 ){
Process_Menu(stu,Logical_Size,Min);
}
else
cout << "Empty file\n\n";
return 0 ;
}
int Search(Student List[], int Size, string Target){
for(int i = 0 ; i < Size ; i++)
if (Target == List[i].LastName)
return i;
return -1;
}
void LookUp_Student(Student List[], int Size){
string Target;
int Index;
cout << "\nEnter a name to search for (Last Name e.g. Smith): ";
getline(cin,Target);
Index = Search(List,Size,Target);
if (Index == -1)
cout << Target <<" is not on the list.\n" ;
else
Print_Search(List[Index]);
}
void Read_Student(Student & Temp , ifstream & fin){
fin >> Temp.FirstName;
fin >> Temp.LastName;
fin >> Temp.Id;
fin >> Temp.Quiz;
for (int i = 0 ; i < 6 ; i++)
fin >> Temp.Test[i];
}
int Read_list(Student List[], int Max_Size)
{
Student Temp;
ifstream fin;
int i = 0;
if (Open_File(fin))
{
getline(fin, List[i].Course_Name);
getline(fin, List[i].Course_Id);
getline(fin, List[i].Course_Location);
Read_Student(List[i],fin);
while(!fin.eof())
{
i++ ;
if (i == Max_Size){
cout << "\nArray is full.\n" ;
break;
}
Read_Student(List[i],fin);
}
}
else
{
cout <<"\nBad file. Did not open. Program terminated.\n";
exit(0);
}
return (i);
}
void Print_List(Student & Temp, Student List[], int Size){
cout <<left << fixed << setprecision(2) <<showpoint <<endl ;
cout << setw(19) << "Name" << setw(18) << "ID" << setw(18) << "Average" << setw(16)
<< "Grade" << endl;
cout << "=============================================================" << endl;
for(int i = 0; i < Size;i++)
cout << setw(19) << List[i].LastName +", "+ List[i].FirstName << setw(18) << List[i].Id << setw(20) << List[i].Average << List[i].Grade << endl;
}
void Print_Course(Student List[],ofstream&fout, int Size){
int i =0;
fout << "Course Name: " << List[i].Course_Name << endl;
fout << "Course ID: " << List[i].Course_Id << endl;
fout << "Course Location: " << List[i].Course_Location << endl;
fout <<left << fixed << setprecision(2) <<showpoint <<endl ;
fout << setw(19) << "Name" << setw(18) << "ID" << setw(18) << "Average" << setw(16)
<< "Grade" << endl;
fout << "=============================================================" << endl;
for(int i = 0; i < Size;i++)
fout << setw(19) << List[i].LastName +", "+ List[i].FirstName << setw(18) << List[i].Id << setw(20) << List[i].Average << List[i].Grade << endl;
fout.close();
}
float Find_Lowest(Student List[], float&Min)
{
Student Temp;
Min = Temp.Test[0];
for(int i = 0; i < 6; i++)
{
if (Temp.Test[i] > Min)
{
Min = Temp.Test[i];
}
}
return (Min);
}
float calcAverage(Student List[],float Min)
{
Student Temp;
float Total = 0;
for(int i = 0; i < 6; i++)
{
Total+=Temp.Test[i];
}
Temp.Average = Total + Temp.Quiz;
return(Temp.Average);
}
char Grade_it(Student List[], Student & Temp)
{
float Min = 0;
calcAverage(List,Min);
Min = Find_Lowest(List,Min);
if (Temp.Average >= 0 && Temp.Average <= 59.9)
{
Temp.Grade = 'F';
}
else if (Temp.Average >= 60 && Temp.Average <= 69.9)
{
Temp.Grade = 'D';
}
else if (Temp.Average >= 70 && Temp.Average <= 79.9)
{
Temp.Grade = 'C';
}
else if (Temp.Average >= 80 && Temp.Average <= 89.9)
{
Temp.Grade = 'B';
}
else if (Temp.Average >= 90 && Temp.Average <= 100)
{
Temp.Grade = 'A';
}
return (Temp.Grade);
}
bool Open_File(ifstream &fin)
{
string File_Name;
cout <<"Enter file name: ";
getline(cin, File_Name);
fin.open(File_Name.c_str());
if(fin.fail())
return false ;
else
return true ;
}
bool Open_File(ofstream &fout)
{
string File_Name;
cout <<"Enter file name: ";
getline(cin, File_Name);
fout.open(File_Name.c_str());
if(fout.fail())
return false ;
else
return true ;
}
void Print_Student(Student & Temp){
cout << endl;
cout << "Here is the student data you entered: " << endl;
cout << "Name: " << Temp.FirstName << ", " << Temp.LastName << endl;
cout << "ID Number: " << Temp.Id << endl;
cout << "Grades for Quiz and Six Test scores " << endl;
cout << "Quiz Score: " << Temp.Quiz << endl;
for (int i=0; i < 6; i++)
{
cout << setw(7) << " Test Score # " << i+1 << ": " << Temp.Test[i] << " " << endl;
}
cin.ignore(10,'\n');
}
void Print_Search(Student Temp){
cout << left << fixed << setprecision(2) << showpoint;
cout << setw(20) << "\nName: " << Temp.FirstName << " " << Temp.LastName << endl;
cout << setw(20) << "ID: " << Temp.Id << endl;
cout << setw(20) << "Average: " << Temp.Average << endl;
cout << setw(20) << "Grade: " << Temp.Grade << endl;
}
Student Read_Student(){
Student Temp ;
cout << "Enter the following student data:" << endl;
cout <<"Student Name(e.g. Bob Smith): " ;
cin >> Temp.FirstName >> Temp.LastName;
cout <<"Student ID Number(without dashes): ";
cin >> Temp.Id;
cout << "Student Quiz Score: ";
cin >> Temp.Quiz;
if(Temp.Quiz < 0 || Temp.Quiz > 100)
{
cout << "Enter a valid quiz score between 0 to 100 for"
<< "\nQuiz Score: ";
cin >> Temp.Quiz;
}
cout << setw(20) <<"Scores for Six Tests:" << endl ;
for (int i = 0 ; i < 6 ; i++)
{
cout <<setw(7)<< "Test # " << i+1 << ": " ;
cin >> Temp.Test[i];
while (Temp.Test[i] < 0 || Temp.Test[i] > 100)
{
cout << "Enter a valid test score between 0 to 100 for"
<< "\nTest # " << i+1 << ": ";
cin >> Temp.Test[i];
}
}
return Temp ;
}
}
|