Skip to content

Instantly share code, notes, and snippets.

@artulloss
Last active October 10, 2018 01:32
Show Gist options
  • Save artulloss/454e689d8b03fc6bf171c3bb0ab2646f to your computer and use it in GitHub Desktop.
Save artulloss/454e689d8b03fc6bf171c3bb0ab2646f to your computer and use it in GitHub Desktop.
My first C++ project!
/* Adam
10-3-18
Basic Math
Does some maths! */
#include <iostream>
#include <string>
#include <algorithm>
#include <math.h>
using namespace std;
string prompt(string type)
{
/*
_____ _
| __ \ | |
| |__) | __ ___ _ __ ___ _ __ | |_
| ___/ '__/ _ \| '_ ` _ \| '_ \| __|
| | | | | (_) | | | | | | |_) | |_
|_| |_| \___/|_| |_| |_| .__/ \__|
| |
|_|
Here I made a function that returns the
users input as well as prompts them for
it.
*/
string returned; // Initilize variable
// Logic for this function, I was going to use switch but then it said it expected an int, and I didn't want to make it confusing.
if (type == "company") {
cout << "Enter company name: "; // Prompt the user
}
if (type == "method") {
cout << "Enter a payment method (cash, finance): "; // Prompt the user
}
if (type == "inDoors") {
cout << "Enter number of interior doors: "; // Prompt the user
}
if (type == "exDoors") {
cout << "Enter number of exterior doors: "; // Prompt the user
}
if (type == "custom") {
cout << "Set custom sales tax, discount percent, finance charge, and cost of each type of door? (y,n): "; // Prompt the user
}
if (type == "tax") {
cout << "Enter the tax as a decimal. For example, .075 would be 7.5%: "; // Prompt the user
}
if (type == "discount") {
cout << "Enter the discount percent (for using cash, won't matter otherwise) as a decimal. For example, .1 would be 10%: "; // Prompt the user
}
if (type == "charge") {
cout << "Enter the finance charge (for using finance instead of cash) as a decimal. For example, .15 would be 15%: "; // Prompt the user
}
if (type == "inPrice") {
cout << "Enter price of an interior door: "; // Prompt the user
}
if (type == "exPrice") {
cout << "Enter price of an exterior door: "; // Prompt the user
}
if (type == "again") {
cout << "Would you like to do another calculation? (y,n) "; // Prompt user
}
// If I were to use cin >> returned, it would end the string at " ", and I don't want that.
getline(cin, returned);
return returned; // This function outputs the input of the user
}
void output(string doorCompany, int doorCostInterior, int doorCostExterior, int inDoorsPurchased, int exDoorsPurchased, double salesTax, double discountPercent, double financeCharge, string payMethod) {
/*
_ _
| | (_)
| | ___ __ _ _ ___
| | / _ \ / _` | |/ __|
| |___| (_) | (_| | | (__
|______\___/ \__, |_|\___|
__/ |
|___/
Here I wrote code to do all the math, assigning
the outputs to variables.
*/
int DoorsPurchased = inDoorsPurchased + exDoorsPurchased;
double inCost = doorCostInterior * inDoorsPurchased;
double exCost = doorCostExterior * exDoorsPurchased;
// Cash
double cSubtotal = round((inCost + exCost) * (1 - discountPercent) * 100) / 100;
double cTax = round((cSubtotal * salesTax) * 100) / 100;
double cBill = round((cSubtotal + cTax) * 100) / 100;
double cAverage = round((cBill / DoorsPurchased) * 100) / 100;
// Finance
double fSubtotal = round((inCost + exCost) * (1 + financeCharge) * 100) / 100;
double fTax = round((fSubtotal * salesTax) * 100) / 100;
double fBill = round((fSubtotal + fTax) * 100) / 100;
double fAverage = round((fBill / DoorsPurchased) * 100) / 100;
/*
_____ _ _ ____ _ _
| __ \(_) | | / __ \ | | | |
| | | |_ ___ _ __ | | __ _ _ _ | | | |_ _| |_ _ __ _ _| |_
| | | | / __| '_ \| |/ _` | | | | | | | | | | | __| '_ \| | | | __|
| |__| | \__ \ |_) | | (_| | |_| | | |__| | |_| | |_| |_) | |_| | |_
|_____/|_|___/ .__/|_|\__,_|\__, | \____/ \__,_|\__| .__/ \__,_|\__|
| | __/ | | |
|_| |___/ |_|
This displays the output of the logic part, I rewrote this from scratch
after seeing the output you wanted us to have.
*/
const string endm = ".00 \n"; // endl is short for end line, endm is short for end math, add .00 and new line
const string SPACING = " - ";
cout << endl << "Invoice for " << doorCompany << endl << endl; // I could also use "\n"
cout << "Cost for " << inDoorsPurchased << " interior doors is $" << inCost << endl;
cout << "Cost for " << exDoorsPurchased << " exterior doors is $" << exCost << endl << endl;
// If the payment method is cash, but then check if it has a decimal point, and if it doesn't use endm instead of endl.
if (payMethod == "cash") {
cout << SPACING << "If paying in cash for this purchase" << endl;
if (remainder(cSubtotal, 2) == 0) {
cout << SPACING << "The subtotal would be $" << cSubtotal << endm;
}
else {
cout << SPACING << "The subtotal would be $" << cSubtotal << endl;
}
if (remainder(cTax, 0) == 0) {
cout << SPACING << "The tax would be $" << cTax << endm;
}
else {
cout << SPACING << "The tax would be $" << cTax << endl;
}
if (remainder(cBill, 2) == 0) {
cout << SPACING << "The total bill would be $" << cBill << endm;
}
else {
cout << SPACING << "The total bill would be $" << cBill << endl;
}
cout << SPACING << "The average cost per door would be " << cAverage << endl << endl;
}
// If the payment method is finance, but then check if it has a decimal point, and if it doesn't use endm instead of endl.
if (payMethod == "finance") {
cout << SPACING << "If financing this purchase" << endl;
if (remainder(fSubtotal, 2) == 0) {
cout << SPACING << "The subtotal would be $" << fSubtotal << endm;
}
else {
cout << SPACING << "The subtotal would be $" << fSubtotal << endl;
}
if (remainder(fTax, 0) == 0) {
cout << SPACING << "The tax would be $" << fTax << endm;
}
else {
cout << SPACING << "The tax would be $" << fTax << endl;
}
if (remainder(fBill, 2) == 0) {
cout << SPACING << "The total bill would be $" << fBill << endm;
}
else {
cout << SPACING << "The total bill would be $" << fBill << endl;
}
cout << SPACING << "The average cost per door would be " << fAverage << endl << endl;
}
double saving = fAverage - cAverage;
if (remainder(saving, 2) == 0) {
cout << "By paying cash you would save $" << saving << endm << endl;
return;
}
cout << "By paying cash you would save $" << saving << endl << endl;
}
void error() {
cout << "Invalid input" << endl;
}
void input() {
// Initilizing some variables and constants, these were originally hard coded constants but I made them changeable, they still default to these.
int doorCostInterior = 500 /* The cost per each interior door */, doorCostExterior = 1200; // The cost per each exterior door
double salesTax = .075 /* Sales tax */, discountPercent = .10 /* Discount percent */, financeCharge = .15; // Finance charge
/*
_____ _
|_ _| | |
| | _ __ _ __ _ _| |_
| | | '_ \| '_ \| | | | __|
_| |_| | | | |_) | |_| | |_
|_____|_| |_| .__/ \__,_|\__|
| |
|_|
Here I ask for input and filter
it to make sure it's valid, it
asks for it repeatedly until it
recieves a valid response.
*/
// First input
string doorCompany = prompt("company"); // Prompt and set name of company selling the doors
// Filter Method
bool promptMethod = true;
string paymentMethod = "ThisDoesn'tMatter";
do {
paymentMethod = prompt("method");
std::transform(paymentMethod.begin(), paymentMethod.end(), paymentMethod.begin(), ::tolower); // https://notfaq.wordpress.com/2007/08/04/cc-convert-string-to-upperlower-case/
if (paymentMethod == "cash" or paymentMethod == "finance") {
promptMethod = false;
}
else {
error();
}
} while (promptMethod == true);
bool promptIn = true;
int inDoorsPurchased; // Initialize variable
do {
// Error Handling Interior Doors
try {
string strNumInDoors = prompt("inDoors"); // Prompt and set number of interior doors as string
inDoorsPurchased = stoi(strNumInDoors); // Set int val if valid input
promptIn = false;
}
catch (invalid_argument) {
error();
}
} while (promptIn == true);
bool promptEx = true;
int exDoorsPurchased;
do {
string strNumExDoors = prompt("exDoors"); // Prompt and set number of exterior doors as string
// Error Handling Exterior Doors
try {
exDoorsPurchased = stoi(strNumExDoors); // Set as int value
promptEx = false;
}
catch (invalid_argument) {
error();
}
} while (promptEx == true);
bool promptCustom = true;
do {
string custom = prompt("custom");
if (custom == "y" or custom == "yes") {
promptCustom = false;
bool promptTax = true;
do {
try {
string str = prompt("tax"); // Optimization
if (stod(str) > 1 or stod(str) < 0) { error(); }
salesTax = stod(str);
promptTax = false;
}
catch (invalid_argument) {
error();
}
} while (promptTax == true);
bool promptDiscount = true;
do {
try {
string str = prompt("discount"); // Optimization
if (stod(str) > 1 or stod(str) < 0) { error(); }
discountPercent = stod(str);
promptDiscount = false;
}
catch (invalid_argument) {
error();
}
} while (promptDiscount == true);
bool promptCharge = true;
do {
try {
string str = prompt("charge"); // Optimization
if (stod(str) > 1 or stod(str) < 0) { error(); }
financeCharge = stod(str);
promptCharge = false;
}
catch (invalid_argument) {
error();
}
} while (promptCharge == true);
bool promptInPrice = true;
do {
try {
doorCostInterior = stod(prompt("inPrice"));
promptInPrice = false;
}
catch (invalid_argument) {
error();
}
} while (promptInPrice == true);
bool promptExPrice;
do {
try {
doorCostExterior = stod(prompt("exPrice"));
promptExPrice = false;
}
catch (invalid_argument) {
error();
}
} while (promptExPrice == true);
}
else if (custom == "n" or custom == "no") { promptCustom = false; }
else {
error();
}
} while (promptCustom == true);
// Send the data to the output
output(doorCompany, doorCostInterior, doorCostExterior, inDoorsPurchased, exDoorsPurchased, salesTax, discountPercent, financeCharge, paymentMethod);
bool final = true;
do {
// Ask if user would like to do another calculation
string again = prompt("again");
std::transform(again.begin(), again.end(), again.begin(), ::tolower); // https://notfaq.wordpress.com/2007/08/04/cc-convert-string-to-upperlower-case/
if (again == "y" or again == "yes") {
cout << endl;
input();
return;
}
else if (again == "n" or again == "no") {
exit(0); // Exit, says online that system("anyArgument") is bad as it is system specific, slow, disliked by antivirus software etc.
}
error();
} while (final == true);
}
int main() {
input(); // Call function
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment