More makefile stuff, improve cent overflow logic

This commit is contained in:
Tyler Beckman 2024-09-21 16:40:38 -06:00
parent 0755838f32
commit ca03d93f04
Signed by: Ty
GPG key ID: 2813440C772555A4
6 changed files with 228 additions and 74 deletions

43
.clang-format Normal file
View file

@ -0,0 +1,43 @@
# Tyler's personal code-style formatting file, make sure to use the class version before turning in
IndentWidth: 4
UseTab: Always
TabWidth: 4
InsertBraces: false
SortIncludes: true
IncludeBlocks: Regroup
IncludeCategories:
# System headers from C (hardcoded, it isn't really possible to automatically detect them with regex)
- Regex: '<c(assert|complex|ctype|errno|fenv|float|inttypes|iso646|limits|locale|math|setjmp|signal|stdalign|stdarg|stdatomic|stdbool|stddef|stdint|stdio|stdlib|stdnoreturn|string|tgmath|threads|time|uchar|wchar|wctype)>'
Priority: 3
# System headers without extension.
- Regex: '<([A-Za-z0-9\Q/-_\E])+>'
Priority: 2
# Local headers with extension.
- Regex: '"([A-Za-z0-9\Q/-_\E])+\.h(pp)?"'
Priority: 1
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: Never
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
BeforeLambdaBody: false
BeforeWhile: false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
IndentCaseLabels: true
IntegerLiteralSeparator:
Binary: 0
Decimal: 3
Hex: -1
DerivePointerAlignment: false
PointerAlignment: Right
QualifierAlignment: Left

43
.class-clang-format Normal file
View file

@ -0,0 +1,43 @@
# A clang-format config to follow CSCI200's style guide, use before turning in
IndentWidth: 2
UseTab: Never
TabWidth: 2
InsertBraces: true
SortIncludes: true
IncludeBlocks: Regroup
IncludeCategories:
# System headers from C
- Regex: '<(cassert|ccomplex|cctype|cerrno|cfenv|cfloat|cinttypes|ciso646|climits|clocale|cmath|csetjmp|csignal|cstdalign|cstdarg|cstdatomic|cstdbool|cstddef|cstdint|cstdio|cstdlib|cstdnoreturn|cstring|ctgmath|cthreads|ctime|cuchar|cwchar|cwctype)>'
Priority: 3
# System headers without extension.
- Regex: '<([A-Za-z0-9\Q/-_\E])+>'
Priority: 2
# Local headers with extension.
- Regex: '"([A-Za-z0-9\Q/-_\E])+\.h(pp)?"'
Priority: 1
BraceWrapping:
AfterCaseLabel: false
AfterClass: false
AfterControlStatement: Never
AfterEnum: false
AfterFunction: false
AfterNamespace: false
AfterObjCDeclaration: false
AfterStruct: false
AfterUnion: false
AfterExternBlock: false
BeforeCatch: false
BeforeElse: false
BeforeLambdaBody: false
BeforeWhile: false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
IndentCaseLabels: true
IntegerLiteralSeparator:
Binary: -1
Decimal: -1
Hex: -1
DerivePointerAlignment: false
PointerAlignment: Left
QualifierAlignment: Left

4
.gitignore vendored Normal file
View file

@ -0,0 +1,4 @@
# Packed files
./*.tar.gz
# Built object files
./**/*.o

View file

@ -1,13 +1,13 @@
TARGET = A2
SRC_FILES = main.cpp atmFunctions.cpp
# Tyler's custom makefile extensions
# Tyler's custom makefile extensions for CSCI200 (anyone can use these if they want)
.DEFAULT_GOAL := all # Necessary so `make` doesn't run the "pack" target, as it is declared before "all"
.PHONY: pack clean-run c run
.PHONY: pack clean-run c run fmt
## Adds only the necessary files for build into a .tar.gz file, named appropriately
ARCHIVED_FILES = Makefile $(SRC_FILES) $(SRC_FILES:.cpp=.h) $(SRC_FILES:.cpp=.hpp)
pack:
pack: fmtc
tar --ignore-failed-read -czvf $(TARGET).tar.gz $(shell echo $(ARCHIVED_FILES) | xargs ls -d 2>/dev/null)
## Runs the pack target and then attempts to build & run the program to make sure it functions correctly
@ -26,6 +26,14 @@ c: clean
run: all
./$(TARGET)
## Formats all cpp, h, and hpp files with clang-format, using my personal clang-format config
fmt:
find . -iname '*.hpp' -o -iname '*.h' -o -iname '*.cpp' | xargs clang-format --style=file:.clang-format -i
## Formats all cpp, h, and hpp files with clang-format, using the class clang-format config
fmtc:
find . -iname '*.hpp' -o -iname '*.h' -o -iname '*.cpp' | xargs clang-format --style=file:.class-clang-format -i
# NO EDITS NEEDED BELOW THIS LINE
CXX = g++

View file

@ -4,93 +4,149 @@
#include <iostream>
#include <limits>
/**
* @brief Normalizes dollar & cent amounts by converting any 100 cent chunks to
* dollars
*
* @param pDollars A pointer to the amount of dollars
* @param pCents A pointer to the amount of cents
*/
void normalize_dollars(int* pDollars, int* pCents) {
*pDollars += *pCents / 100;
*pCents = *pCents % 100;
}
void start_atm() {
int dollars = 0;
int cents = 0;
int dollars = 0;
int cents = 0;
std::cout << "You have entered the ATM menu" << std::endl;
std::cout << "You have entered the ATM menu" << std::endl;
bool continuing = true;
char choice;
do {
print_menu();
choice = get_user_selection();
switch (choice) {
case '1':
print_balance(&dollars, &cents);
break;
case '2':
deposit_money(&dollars, &cents);
break;
case '3':
case 'Q':
continuing = false;
std::cout << "Have a nice day!" << std::endl;
break;
}
} while (continuing);
bool continuing = true;
char choice;
do {
print_menu();
choice = get_user_selection();
switch (choice) {
case '1':
print_balance(&dollars, &cents);
break;
case '2':
deposit_money(&dollars, &cents);
break;
case '3':
withdraw_money(&dollars, &cents);
break;
case 'Q':
continuing = false;
std::cout << "Have a nice day!" << std::endl;
break;
}
} while (continuing);
}
void print_menu() {
std::cout << "Please make a selection:" << std::endl
<< "(1) Print Current Balance" << std::endl
<< "(2) Deposit" << std::endl
<< "(3) Withdraw" << std::endl
<< "(Q) Quit" << std::endl;
std::cout << "Please make a selection:" << std::endl
<< "(1) Print Current Balance" << std::endl
<< "(2) Deposit" << std::endl
<< "(3) Withdraw" << std::endl
<< "(Q) Quit" << std::endl;
}
char get_user_selection() {
char input = 'Q';
char input = 'Q';
while (true) {
std::cout << "Choice: ";
std::cin >> input;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if (std::cin.fail() ||
!(input == '1' || input == '2' || input == '3' || input == 'Q')) {
std::cout << "Invalid choice, please try again" << std::endl;
} else {
std::cout << std::endl;
return input;
}
};
while (true) {
std::cout << "Choice: ";
std::cin >> input;
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
if (std::cin.fail() ||
!(input == '1' || input == '2' || input == '3' || input == 'Q')) {
std::cout << "Invalid choice, please try again" << std::endl;
} else {
std::cout << std::endl;
return input;
}
};
}
void print_balance(int* pDollars, int* pCents) {
std::cout << "Your account currently contains a total of $" << *pDollars
<< "." << std::setfill('0') << std::setw(2) << *pCents << "."
<< std::endl
<< std::endl;
std::cout << "Your account currently contains a total of $" << *pDollars
<< "." << std::setfill('0') << std::setw(2) << *pCents << "."
<< std::endl
<< std::endl;
}
void deposit_money(int* pDollars, int* pCents) {
// Prompt the user for the amount to deposit
int newDollars = 0, newCents = 0;
std::cout << "How many dollars would you like to deposit? ";
std::cin >> newDollars;
std::cout << "How many cents would you like to deposit? ";
std::cin >> newCents;
// Prompt the user for the amount to deposit
int dollarsToDeposit = 0, centsToDeposit = 0;
std::cout << "How many dollars would you like to deposit? ";
std::cin >> dollarsToDeposit;
std::cout << "How many cents would you like to deposit? ";
std::cin >> centsToDeposit;
// Ensure valid positive integers were entered
if (newDollars < 0 || newCents < 0) {
std::cout << "Deposits must be positive amounts, please try again with "
"proper values"
<< std::endl
<< std::endl;
}
// Ensure valid positive integers were entered
if (dollarsToDeposit < 0 || centsToDeposit < 0) {
std::cout << "Deposits must be positive amounts, please try again with "
"proper values"
<< std::endl
<< std::endl;
return;
}
// Add dollar and cent values to variables passed-by-pointer
*pDollars += newDollars;
*pCents += newCents;
// Normalize the user-inputted values for later display
normalize_dollars(&dollarsToDeposit, &centsToDeposit);
// Condense cent amounts > 100 into dollars
*pDollars += *pCents / 100;
*pCents = *pCents % 100;
*pDollars += dollarsToDeposit;
*pCents += centsToDeposit;
// Display the amount deposited nicely
std::cout << "You have successfully deposited a total of $"
<< (newDollars + (newCents / 100)) << "." << std::setfill('0')
<< std::setw(2) << newCents % 100 << "." << std::endl
<< std::endl;
// Normalize the true account values after addition
normalize_dollars(pDollars, pCents);
// Display the amount deposited nicely
std::cout << "You have successfully deposited a total of $"
<< dollarsToDeposit << "." << std::setfill('0') << std::setw(2)
<< centsToDeposit << "." << std::endl
<< std::endl;
}
void withdraw_money(int* pDollars, int* pCents) {
// Prompt the user for the amount to withdraw
int dollarsToWithdraw = 0, centsToWithdraw = 0;
std::cout << "How many dollars would you like to withdraw? ";
std::cin >> dollarsToWithdraw;
std::cout << "How many cents would you like to withdraw? ";
std::cin >> centsToWithdraw;
if (centsToWithdraw < 0 || dollarsToWithdraw < 0) {
std::cout << "Withdrawals must be positive amounts, please try again with "
"proper values"
<< std::endl
<< std::endl;
return;
}
normalize_dollars(&dollarsToWithdraw, &centsToWithdraw);
// Convert dollar amounts to cents for easier subtraction
int totalCentWithdrawal = dollarsToWithdraw + (dollarsToWithdraw * 100);
*pCents += *pDollars * 100;
*pDollars = 0;
if (totalCentWithdrawal > *pCents) {
std::cout << "You do not have enough money for this withdrawal" << std::endl
<< std::endl;
return;
}
*pCents -= totalCentWithdrawal;
normalize_dollars(pDollars, pCents);
std::cout << "You have successfully withdrawn a total of $"
<< dollarsToWithdraw << "." << std::setfill('0') << std::setw(2)
<< centsToWithdraw << "." << std::endl
<< std::endl;
}

View file

@ -1,6 +1,6 @@
#include "atmFunctions.h"
int main() {
start_atm();
return 0;
start_atm();
return 0;
}