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
|
#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>
#include "Student.h"
using namespace std;
int menu();
void displayAll(Student *, int);
void sortStudent(Student *, int);
void exitSave(Student *, int);
int main()
{
Student *student;
int studentSize,
gradeNum,
ID,
menuOption;
string firstLast;
double *Grades;
cout<<"Enter the number of students: ";
cin>>studentSize;
while(studentSize<0)
{
cout<<"ERROR."<<endl;
cout<<"Enter positive amount: ";
cin>>studentSize;
}
student=new Student[studentSize];
for(int i=0;i<studentSize;i++)
{
cout<<"Enter the information for Student "<<i+1<<": "<<endl;
cout<<"First Name: ";
cin.ignore();
getline(cin,firstLast);
student[i].setFirstN(firstLast);
cout<<"Last Name: ";
getline(cin,firstLast);
student[i].setLastN(firstLast);
cout<<"Student ID: ";
cin>>ID;
while(ID<0)
{
cout<<"ERROR. Enter positive ID: ";
cin>>ID;
}
student[i].setStudentID(ID);
cout<<"Enter the number of grades: ";
cin>>gradeNum;
while(gradeNum<0)
{
cout<<"ERROR."<<endl;
cout<<"Enter a positive amount: ";
cin>>gradeNum;
}
Grades=new double[gradeNum];
for(int count=0;count<gradeNum;count++)
{
cout<<"Grade "<<count+1<<": ";
cin>>Grades[count];
while(Grades[count]<0 || Grades[count]>100)
{
cout<<"ERROR. Enter a valid grade: ";
cin>>Grades[count];
}
}
student[i].setGrades(gradeNum,Grades);
delete [] Grades;
Grades=0;
system("CLS");
}
do
{
menuOption=menu();
//Running fine here
switch(menuOption)
{
case 1: displayAll(student,studentSize);
cin.ignore();
cin.get();
//Running fine here
break;
case 2: //Running fine here
sortStudent(student,studentSize); //Runs sorting function
//Not running fine anymore, some grades for gibberish
break;
case 3: exitSave(student,studentSize);
break;
}
}while(menuOption!=3);
delete [] student;
student=0;
delete [] Grades;
Grades=0;
return 0;
}
int menu()
{
int option;
cout<<"MENU"<<endl;
cout<<"1. Display All"<<endl;
cout<<"2. Sort students"<<endl;
cout<<"3. Exit and save"<<endl;
cout<<"\nEnter choice: ";
cin>>option;
while(option<1 || option>3)
{
cout<<"ERROR. Enter 1-3: ";
cin>>option;
}
return option;
}
void displayAll(Student *students, int size)
{
for(int i=0;i<size;i++)
{
cout<<(i+1)<<". ";
cout<<students[i].getFirstN()<<" ";
cout<<students[i].getLastN()<<endl;
cout<<" ID: "<<students[i].getStudentID()<<endl;
cout<<" Grades: ";
students[i].displayGrades();
cout<<endl;
}
}
void sortStudent(Student *students,int size)
{
int scan,
minInd;
Student holder;
double min;
for(scan=0;scan<(size-1);scan++)
{
minInd=scan;
min=students[scan].calcAverage();
holder=students[scan];
for(int i=(scan+1);i<size;i++)
{
if(students[i].calcAverage()>min)
{
minInd=i;
min=students[i].calcAverage();
holder=students[i];
}
}
students[minInd]=students[scan];
students[scan]=holder;
}
//Sorted grades are how they are supposed to be
}
void exitSave(Student *students, int size)
{
ofstream writeFile;
writeFile.open("studentinfo.txt");
double *grade;
for(int i=0;i<size;i++)
{
writeFile<<i+1<<". ";
writeFile<<students[i].getFirstN()<<" ";
writeFile<<students[i].getLastN()<<endl;
writeFile<<" ID: "<<students[i].getStudentID()<<", ";
writeFile<<" Grades: ";
grade=students[i].getGrades();
for(int count=0;count<size;count++)
{
writeFile<<grade[count]<<" ";
}
writeFile<<endl;
}
writeFile.close();
}
|