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
|
#include <iostream>
#include <string>
#include <limits>
// <--- One place to find and change when needed.
constexpr int FRESH{ 1500 };
constexpr int SOPHOMORE{ 1800 };
constexpr int JUNIOR{ 2000 };
constexpr int SENIOR{ 2300 };
constexpr double DPRATE{ 0.30 };
// <--- This could be done in the if/else if statements with a constant string like: "Year Name : Freshman" in the function call.
const std::string YEARNAMEF{ "Year Name : Freshman" };
const std::string YEARNAMES{ "Year Name : Sophomore" };
const std::string YEARNAMEJ{ "Year Name : Junior" };
const std::string YEARNAMESR{ "Year Name : Senior" };
// <--- Prototypes for the functions.
void GetInput(std::string& stName, std::string& progCourse, int& yrLevel, double& unitEn);
void DisplayResults(const std::string stName,
const std::string progCourse,
const std::string yearName,
const double unitEn,
const int tf,
const double dp,
const double bal);
int main()
{
std::string stName, progCourse;
int yrLevel{}; // <--- Initializes the variable to zero.
double unitEn{}; //<--- Initializes the variable to 0.0.
// <--- These only need to be defined once.
int tf{};
double dp{};
int bal{};
GetInput(stName, progCourse, yrLevel, unitEn);
std::cout << "\n";
if (yrLevel == 1)
{
// <--- Notice the use of "FREASH" and "DPRATE".
tf = unitEn * FRESH;
dp = tf * DPRATE;
int bal = tf - dp;
DisplayResults(stName, progCourse, YEARNAMEF, unitEn, tf, dp, bal);
// <--- Or
//DisplayResults(stName, progCourse, "Year Name : Freshman", unitEn, tf, dp, bal);
// <--- Notice the constant string in place of "YEARNAMEF"
}
else if (yrLevel == 2)
{
// <--- Do not need to redefine these next 3 lines. Only needs defined once.
tf = unitEn * SOPHOMORE;
dp = tf * DPRATE;
bal = tf - dp;
DisplayResults(stName, progCourse, YEARNAMES, unitEn, tf, dp, bal);
std::cout << "\n";
}
else if (yrLevel == 3)
{
tf = unitEn * JUNIOR;
dp = tf * DPRATE;
bal = tf - dp;
DisplayResults(stName, progCourse, YEARNAMEJ, unitEn, tf, dp, bal);
std::cout << "\n";
}
else if (yrLevel == 4)
{
tf = unitEn * SENIOR;
dp = tf * DPRATE;
bal = tf - dp;
DisplayResults(stName, progCourse, YEARNAMESR, unitEn, tf, dp, bal);
std::cout << "\n";
}
else if (yrLevel == 5)
{
tf = unitEn * SENIOR;
dp = tf * DPRATE;
bal = tf - dp;
DisplayResults(stName, progCourse, YEARNAMESR, unitEn, tf, dp, bal);
std::cout << "\n";
}
// The next line may not be needed. If you have to press enter to see the prompt it is not needed.
//std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
std::cout << "\n\n Press Enter to continue";
std::cin.get();
return 0;
}
void GetInput(std::string& stName, std::string& progCourse, int& yrLevel, double& unitEn)
{
std::cout << "Student Name : ";
std::getline(std::cin, stName);
std::cout << "Program/Course : ";
std::getline(std::cin, progCourse);
std::cout << "Year Level : ";
// <--- You might consider:
std::cout << "Year Level (Sophomore = 1): ";
// <--- To show the user what need to be entered. Because some user may try to enter a string instead of a number.
std::cin >> yrLevel;
std::cout << "Number of Units : ";
std::cin >> unitEn;
// <--- Used after the last "cin" to clear the input buffer. And needed before the next "std::getline(...)".
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // <--- Requires header file <limits>.
}
void DisplayResults(const std::string stName, // <--- All are "const" so they can not be changed.
const std::string progCourse,
const std::string yearName,
const double unitEn,
const int tf,
const double dp,
const double bal)
{
std::cout << "\n";
std::cout << "***ENROLLMENT SLIP***";
std::cout << "\n";
std::cout << "\n";
std::cout << "Student Name : " << stName;
std::cout << "\n";
std::cout << "Program/Course : " << progCourse;
std::cout << "\n";
std::cout << yearName; // <--- Changed. Acquired from the parameters in the function definition.
std::cout << "\n";
std::cout << "Number of Units : " << unitEn;
std::cout << "\n";
std::cout << "Tuitio Fee : " << tf;
std::cout << "\n";
std::cout << "Downpayment : " << dp;
std::cout << "\n";
std::cout << "balance : " << bal;
std::cout << "\n";
}
|