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
|
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
#include <cctype>
using namespace std;
enum level {FRESHMAN, SOPHOMORE, JUNIOR, SENIOR};
const string STUD_REC = "CREDITS.TXT";
const string FILE_SORT = "SORTED.TXT";
const int MAX_NUM = 999;
void ifUnder(int hrs, string idNum, string idArray1[MAX_NUM], int classLevel1[MAX_NUM]); //if undergraduate, this will fill the arrays with the data
// and apply the enumerated value to it.
void sortArrays(string array1[MAX_NUM], int array2[MAX_NUM], int count1);
int sortData(string idArray[MAX_NUM], int classLevel[MAX_NUM]);//goes through data and calls the ifUnder function if necessary
void writeData(string array1[MAX_NUM], int array2[MAX_NUM], int count1);
int main()
{
string id[MAX_NUM];
int claLevel[MAX_NUM];
int counter;
cout << "This program will convert hours taken to class level for all "
"Undergraduate students. You will be able to search for each student by "
"the student ID number." << endl << endl;
counter = sortData(id, claLevel);
sortArrays(id, claLevel, counter);
writeData(id, claLevel, counter);
cout << endl;
return 0;
}
int sortData(string idArray[MAX_NUM], int classLevel[MAX_NUM])
{
string id;
char stuType;
double hours;
ifstream studData;
int count = 0;
studData.open (STUD_REC.c_str()); // open the file for reading
if(studData) //If the file opens successfully
{
studData >> id >> stuType >> hours; //priming read
while(studData) // while an id was successfully read
{
if((stuType == 'U') || (stuType == 'u'))
{
ifUnder(hours, id, idArray, classLevel);
count++;
}
studData >> id >> stuType >> hours; //attempt to read next line
}
}
return count;
}
void ifUnder(int hrs, string idNum, string idArray1[MAX_NUM], int classLevel1[MAX_NUM])
{
int array1Count = 0;
int array2Count = 0;
level idLevel;
if(0 <= hrs < 32)
{
idLevel = FRESHMAN;
}
else if(32 <= hrs <= 63)
{
idLevel = SOPHOMORE;
}
else if(64 <= hrs <= 95)
{
idLevel = JUNIOR;
}
else if(96 <= hrs)
{
idLevel = SENIOR;
}
idArray1[array1Count] = idNum;
array1Count++;
classLevel1[array2Count] = idLevel;
array2Count++;
}
void sortArrays(string array1[MAX_NUM], int array2[MAX_NUM], int count1)
{
int i, j, first;
string temp;
for (i = count1 - 1; i > 0; i--)
{
first = 0; // initialize to subscript of first element
for (j = 1; j <= i; j++) // locate smallest between positions 1 and i.
{
if (array1[j] < array1[first])
first = j;
}
temp = array1[first]; // Swap smallest found with element in position i.
array1[first] = array1[i];
array2[first] = array2[i];
array1[i] = temp;
}
}
void writeData(string array1[MAX_NUM], int array2[MAX_NUM], int count1)
{
ofstream sortData("SORTED.TXT");
if (sortData.is_open())
{
for(int i = 0; i < count1; i++)
{
sortData << array1[i] << " " << array2[i] << endl;
}
}
else
{
cout << "Could not open file." << endl;
}
sortData.close();
}
|