This commit is contained in:
Tyler Beckman 2024-09-22 20:25:00 -06:00
parent 68e45dd3b5
commit bf1c77bd06
Signed by: Ty
GPG key ID: 2813440C772555A4
3 changed files with 114 additions and 112 deletions

View file

@ -11,142 +11,144 @@
* @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 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':
withdraw_money(&dollars, &cents);
break;
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;
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;
}
void deposit_money(int* pDollars, int* pCents) {
// 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;
void deposit_money(int *pDollars, int *pCents) {
// 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 (dollarsToDeposit < 0 || centsToDeposit < 0) {
std::cout << "Deposits must be positive amounts, please try again with "
"proper values"
<< std::endl
<< std::endl;
return;
}
// 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;
}
// Normalize the user-inputted values for later display
normalize_dollars(&dollarsToDeposit, &centsToDeposit);
// Normalize the user-inputted values for later display
normalize_dollars(&dollarsToDeposit, &centsToDeposit);
*pDollars += dollarsToDeposit;
*pCents += centsToDeposit;
*pDollars += dollarsToDeposit;
*pCents += centsToDeposit;
// Normalize the true account values after addition
normalize_dollars(pDollars, pCents);
// 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;
// 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;
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;
}
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);
normalize_dollars(&dollarsToWithdraw, &centsToWithdraw);
// Convert dollar amounts to cents for easier subtraction
int totalCentWithdrawal = centsToWithdraw + (dollarsToWithdraw * 100);
// Convert dollar amounts to cents for easier subtraction
int totalCentWithdrawal = centsToWithdraw + (dollarsToWithdraw * 100);
*pCents += *pDollars * 100;
*pDollars = 0;
*pCents += *pDollars * 100;
*pDollars = 0;
if (totalCentWithdrawal > *pCents) {
std::cout << "You do not have enough money for this withdrawal" << std::endl
<< std::endl;
return;
}
if (totalCentWithdrawal > *pCents) {
std::cout << "You do not have enough money for this withdrawal"
<< std::endl
<< std::endl;
return;
}
*pCents -= totalCentWithdrawal;
*pCents -= totalCentWithdrawal;
normalize_dollars(pDollars, pCents);
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;
std::cout << "You have successfully withdrawn a total of $"
<< dollarsToWithdraw << "." << std::setfill('0') << std::setw(2)
<< centsToWithdraw << "." << std::endl
<< std::endl;
}

View file

@ -29,7 +29,7 @@ char get_user_selection();
* @param dollars The amount of full dollars the user currently has
* @param cents The amount of cents the user currently has
*/
void print_balance(int* pDollars, int* pCents);
void print_balance(int *pDollars, int *pCents);
/**
* @brief Prompts the user an amount to deposit, validates it, and modifies the
@ -38,7 +38,7 @@ void print_balance(int* pDollars, int* pCents);
* @param dollars The amount of full dollars the user currently has
* @param cents The amount of cents the user currently has
*/
void deposit_money(int* pDollars, int* pCents);
void deposit_money(int *pDollars, int *pCents);
/**
* @brief Prompts the user an amount to withdraw, validates it, and modifies the
@ -47,6 +47,6 @@ void deposit_money(int* pDollars, int* pCents);
* @param dollars The amount of full dollars the user currently has
* @param cents The amount of cents the user currently has
*/
void withdraw_money(int* pDollars, int* pCents);
void withdraw_money(int *pDollars, int *pCents);
#endif // ATMFUNCTIONS_H

View file

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