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
|
#include <cmath> // Using mathematics
#include <fstream> // fin and fout
#include <iostream> // <-- missing header
#include <iomanip> // Setprecision
#include <sstream>
#include <string>
bool manageLiberalArt(double gpa, int math, int verb, char alumn,
int already_in, std::string& reason);
bool manageMusic(int math, int verb, int already_in, std::string& reason);
int main() {
// Here I use a while statement followed by multiple if statements to
// ensure that each response is returned and either read as true or false
// in regards to this assignments specific required categories for each
// school and applicant
std::ifstream inputfile("source.txt");
if(!inputfile) {
std::cout << "Can't open file 'source.txt'.\n Exiting now.\n";
std::exit(1);
}
std::ofstream fout("mp2output.txt");
if(!fout) {
std::cout << "Can't create file 'mp2output.txt'.\n Exiting now.\n";
std::exit(1);
}
fout << "Acceptance to College by Michael Matteis\n\n";
// Loop: a) read a line of text inside a std::string; get rid of '\n'
// (to perform such a task, std::getline() is recommended);
// b) create a std::istringstream from that std::string;
// c) use that std::istringstream to initialize my variables.
int app_num {1},
lib_art_accept {},
music_accept {};
for(std::string line; std::getline(inputfile, line); ++app_num) { // will loop until EOF
std::istringstream ss(line);
// Let's assume source.txt is done this way:
// school gpa math verb alumn
// L 4.0 600 650 N
// (1 char) (double) (int) (int) (char)
char school {},
alumn {};
double gpa {};
int math {},
verb {};
ss >> school >> gpa >> math >> verb >> alumn;
std::string reason;
switch(school) { // switch is usually easier to read
case 'L': {
if(manageLiberalArt(gpa, math, verb, alumn, lib_art_accept, reason)) {
lib_art_accept++;
}
break;
}
case 'M': {
if(manageMusic(math, verb, music_accept, reason)) {
music_accept++;
}
break;
}
default:
std::cout << "Error while reading file.\n Exiting now.\n";
exit(1);
break;
}
std::string print_school;
if (school == 'L') print_school = "***Applying to Liberal Arts";
if (school == 'M') print_school = "***Applying to School of Music";
// ===================================================================
// the following statements will read/output the data that was inputed
// by I or the user after it has ran through the if statements to
// provide the correct format expected of this assignment
fout << "Applicant #: " << app_num
<< std::fixed << std::setprecision(1)
<< " GPA = " << gpa
<< " Math = " << math
<< " Verbal = " << verb
<< " Alumnus = " << alumn
<< '\n' << print_school
<< '\n' << reason << '\n';
}
// =======================================================================
// Below will read/output the final totals including; applicants, those
// accepted into Music or Lberal Arts.
fout << "\n\nTotal Applicants: " << app_num
<< "\nTotal accepted into music: " << lib_art_accept
<< "\nTotal accepted into Liberal Arts: " << music_accept << "\n\n";
return 0;
}
bool manageLiberalArt(double gpa, int math, int verb, char alumn,
int already_in, std::string& reason)
{
if(already_in > 4) {
reason = "Sorry, no more applicants accepted.\n";
return false;
}
if(alumn == 'Y') {
if(math + verb < 1000) {
reason = "Rejected - SAT scores are too low.\n";
return false;
}
if(gpa < 3.0) {
reason = "Rejected - GPA is too low.\n";
return false;
}
}
if(alumn == 'N') {
if(math + verb < 1200) {
reason = "Rejected - SAT scores are too low.\n";
return false;
}
if(gpa < 3.5) {
reason = "Rejected - GPA is too low.\n";
return false;
}
}
reason = "Congratulations you have been accepted into Liberal Arts!\n";
return true;
}
bool manageMusic(int math, int verb, int already_in, std::string& reason)
{
if(already_in > 2) {
reason = "Unfortunately admissions are full at this time";
return false;
}
if(math < 500 || verb < 500) {
reason = "Rejected - SAT scores are too low.\n";
return false;
}
reason = "Congratulations you have been accepted into Music!\n";
return true;
}
|