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 <fstream>
#include <string>
#include <iomanip>
#include <cctype>
using namespace std;
#define MAX 1000
int getData(ifstream& inFile, string uni[], string state[], string city[], double yearlytuit[], int enroll[], double null[], double fresh[], double gradsix[]);
void output(ofstream& outFile, string uni[], string state[], double yearlytuit[], int enroll[], double fresh[], double gradsix[], int number);
double avgTuit(double yearlytuit[], int number);
void max(double yearlytuit[], string uni[]);
void collegesearch(ofstream& outFile, string uni[], string state[], string city[], double yearlytuit[], int enroll[], double fresh[], double gradsix[], int number);
int lowTuit(string uni[], double yearlytuit[], int number);
void sortselect(ofstream& outFile, string uni[], string state[], double yearlytuit[], int enroll[], double fresh[], double gradsix[], int number);
int main()
{
string uni[MAX], state[MAX], city[MAX];
int enroll[MAX], number, loc;
//Null here is actually percent of students accepted but will not be used in anything hence nickname of null
double yearlytuit[MAX], null[MAX], fresh[MAX], gradsix[MAX];
double avgTuition, lowest;
ifstream inFile;
ofstream outFile;
inFile.open("universities.txt");
outFile.open("unicomp.txt");
if(inFile.fail())
{
cout << "File does not exist" << endl;
exit(100);
}
number = getData(inFile, uni, state, city, yearlytuit, enroll, null, fresh, gradsix);
output(outFile, uni, state, yearlytuit, enroll, fresh, gradsix, number);
avgTuition = avgTuit(yearlytuit, number);
cout << "The average tuition for all universities is: " << avgTuition << endl;
max(yearlytuit, uni);
collegesearch(outFile, uni, state, city, yearlytuit, enroll, fresh, gradsix, number);
loc = lowTuit(uni, yearlytuit, number);
cout << uni[loc] << yearlytuit[loc];
sortselect(outFile, uni, state, yearlytuit, enroll, fresh, gradsix, number);
outFile << "\nUniversity " << "State " << "Tuition " << "Enrollment " << "%Fresh " << "%Graduate\n";
outFile << " " << "Succeed " << "in six years\n";
for (int i = 0; i < number; i ++)
{
outFile << left << setw(40) << uni[i] << setw(6) << state[i] << setw(10) << setprecision(2) << fixed << yearlytuit[i];
outFile << right << setw(6) << enroll[i] << setw(10) << fresh[i] * 100 << "%" << setw(10) << gradsix[i] *100 << "%" << endl;
}
}
int getData(ifstream& inFile, string uni[], string state[], string city[], double yearlytuit[], int enroll[], double null[], double fresh[], double gradsix[])
{
int count = 0;
while (count < MAX && !inFile.eof())
{
getline(inFile, uni[count]);
inFile >> state[count];
getline(inFile, city[count]);
//cout << uni[count] << endl << state[count] << " " << city[count] << endl;
inFile >> yearlytuit[count] >> enroll[count] >> null[count] >> fresh[count] >> gradsix[count];
//cout << enroll[count] << " " << unk[count] << " " << yearlytuit[count] << " " << fresh[count] << " " << gradsix[count] << endl;
string tempstr; getline(inFile, tempstr);
count++;
}
return count;
}
void output(ofstream& outFile, string uni[], string state[], double yearlytuit[], int enroll[], double fresh[], double gradsix[], int number)
{
int count = 0;
while (count < number)
{
outFile << uni[count] << endl << state[count] << endl << yearlytuit[count] << " " << enroll[count] << " " << fresh[count] << " " << gradsix[count] << " " << endl;
count++;
}
}
double avgTuit(double yearlytuit[], int number)
{
double avg = 0;
for (int i = 0; i < number; i++)
{
avg = avg + yearlytuit[i];
}
avg = avg / number;
return avg;
}
void max(double yearlytuit[], string uni[])
{
double maxtuit;
cout << "What is the maximum tuition you can afford? ";
cin >> maxtuit;
cout << "The colleges you can afford to attend are:\n";
for (int i = 0; i < yearlytuit[i]; i++)
{
if (maxtuit > yearlytuit[i])
{
cout << uni[i] << endl;
}
}
}
void collegesearch(ofstream& outFile, string uni[], string state[], string city[], double yearlytuit[], int enroll[], double fresh[], double gradsix[], int number)
{
string statename;
cout << "Enter a two-letter abbreviation for a state for all information for colleges within that state: ";
cin >> statename;
for (int i = 0; i < number; i ++)
{
if (statename == state[i])
{
outFile << uni[i] << "\n" << state[i] << " " << city[i]<< "\n";
outFile << yearlytuit[i] << " " << enroll[i] << " " << fresh[i] << " " << gradsix[i] << "\n";
}
}
}
int lowTuit(string uni[], double yearlytuit[], int number)
{
int loc;
string univ;
double lowest = yearlytuit[0];
for (int i = 0; i < number; i++)
{
if (lowest > yearlytuit[i])
{
lowest = yearlytuit[i];
loc = i;
}
}
return loc;
}
void sortselect(ofstream& outFile, string uni[], string state[], double yearlytuit[], int enroll[], double fresh[], double gradsix[], int number)
{
int i; int current;
int smallestEnroll, tempEnroll;
for (current = 0; current < number - 1; current ++)
{
smallestEnroll = 0;
for (i = current + 1; i < number; i++)
{
if (enroll[i] < enroll[smallestEnroll])
smallestEnroll = i;
}
tempEnroll = enroll[current];
enroll[current] = enroll[smallestEnroll];
enroll[smallestEnroll] = tempEnroll;
}
}
|