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
|
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
struct Student{
string Name;
long ID;
char Grade;
int Total_points, Total_Units, Group_Units, Units, counter;
float GPA, Group_GPA, Score, points;
};
//Prototypes
void Process_Student(Student &Temp, ifstream & Fin);
void Display_Header(void);
bool Open_File( ifstream &in );
bool Open_File( ofstream &out );
float Assign_GPA( Student & Temp);
void Process(Student & Temp);
bool Verify (Student & Temp);
int main()
{
//Declare Variables
Student S1 = {"", 0, '\0', 0, 0, 0, 0, 0, 0, 0, 0, 0};// initializing the structure..Notice the order of all the zeros.
ifstream Fin ;
ofstream Fout ;
Display_Header();
if(Open_File(Fin))
{
if (Open_File(Fout))
{
Fout<< setprecision(2)<<fixed << showpoint ;
cout << left<<setw(27)<<"\n\nStudent Name" << setw(10)<<"ID "<<setw(10)<<"Units" <<setw(8)<<"GPA" <<endl;
cout <<"====================================================\n";
Fout << left<<setw(27)<<"\n\nStudent Name" << setw(10)<<"ID "<<setw(10)<<"Units" <<setw(8)<<"GPA" <<endl;
Fout <<"====================================================\n";
getline (Fin, S1.Name);
while(!Fin.eof())
{
Fin>> S1.ID ;
Process_Student(S1, Fin);
cout << setw(25) << S1.Name << setw(10) << S1.ID << setw(10) << S1.Total_Units << setw(8) << S1.GPA <<endl;
Fout << setprecision(2)<<fixed;
Fout << setw(25) << S1.Name << setw(10) << S1.ID << setw(10) << S1.Total_Units << setw(8) << S1.GPA <<endl;
S1.counter++ ;
Fin.ignore(10,'\n');
getline(Fin, S1.Name);
S1.Total_Units = 0;
S1.GPA = 0;
}
S1.Group_GPA /= S1.Group_Units ;
cout <<"==================================================\n";
cout <<"Group of "<<S1.counter<<setw(25)<<" Students Totals" <<setw(10)<< S1.Group_Units << setw(10)<< S1.Group_GPA << endl << endl ;
Fout <<"==================================================\n\n";
Fout <<"Group of "<<S1.counter<<setw(25)<<" Students Totals" <<setw(10)<< S1.Group_Units << setw(10)<< S1.Group_GPA << endl << endl ;
cout << endl << endl ;
}
}
return 0;
}
//===================================================================================================================================
void Display_Header(void)
{
cout<< setw(45)<< right<< "Sample Soultion"<< endl;
cout<< setw(45)<< right<< " CSCI 1 "<< endl;
cout<< setw(45)<< right<< " Assignment 6 "<< endl<< endl;
cout<< setprecision(2)<<fixed << showpoint ;
}
void Process_Student(Student & Temp, ifstream & Fin)
{
Fin >> Temp.Grade;
while(Temp.Grade != '*')
{
Fin >> Temp.Units;
Temp.Score = Assign_GPA(Temp);
Process(Temp);
Fin>> Temp.Grade;
}
Temp.Group_Units += Temp.Total_Units ;
Temp.Group_GPA += Temp.GPA ;
if( Temp.Total_Units > 0 )
Temp.GPA/= Temp.Total_Units;
}
bool Open_File( ifstream &in )
{
int counter = 0 ;
string File_name ;
do{
counter++ ;
in.clear();
cout<< "Enter the input file name: ";
getline(cin, File_name);
in.open(File_name.c_str());
}while (in.fail() && counter < 3);
if(counter > 3)
{
cout <<"Bad file name.\nProgram Terminated.\n\n";
return false;
}
return true ;
}
bool Open_File( ofstream &out )
{
int counter = 0 ;
string File_name ;
do{
counter++ ;
out.clear();
cout<< "Enter the output file name: ";
getline(cin, File_name);
out.open(File_name.c_str());
}while (out.fail() && counter < 3);
if(counter > 3)
{
cout <<"Bad file name.\nProgram Terminated.\n\n";
return false;
}
return true ;
}
float Assign_GPA( Student & Temp)
{
if(Temp.Grade == 'A' || Temp.Grade=='a')
Temp.points=4.0;
else if(Temp.Grade == 'B' || Temp.Grade == 'b')
Temp.points=3.0;
else if(Temp.Grade == 'C' || Temp.Grade == 'c')
Temp.points=2.0;
else if(Temp.Grade == 'D' || Temp.Grade == 'd')
Temp.points=1.0;
else if(Temp.Grade =='F' || Temp.Grade =='f')
Temp.points=0.0 ;
else
Temp.points=-1;
return Temp.points ;
}
void Process(Student & Temp)
{
if (Verify(Temp) && Temp.points >= 0)
{
Temp.GPA = (Temp.GPA+Temp.points*Temp.Units);
Temp.Total_Units += Temp.Units;
}
}
bool Verify (Student & Temp)
{
if (Temp.Units > 0 && Temp.Units <=5)
return true;
else
return false ;
}
|