I will be taking a c++ class in the fall so I decided to do some self-studying. I took a programming logic and design class the previous semester and have been writing pusdo-code. But now I want to translate that into C++.
I wrote a program to calculate over drawn fees. The goal was to use it to practice passing variables between modules.
I got the program to work but I was wondering if I have extra code in there. I want to return variables to the main program without me needing to pass variables from the main() to the other modules.
Do I need these two variables to make my program work?
double accountBalance=0; double overDrawn=0; If you guys have any other tips for me to make my code cleaner please let me know. Thank you!
#include <iostream>
#include <cmath>
using namespace std;
// Page 83
// Exercise 5
// The program determines a monthly checking account fee.
//Declaration of all the modules
int acctBalInput(double acct);
int overdrawInput(double draw);
int feeCalculation(int bal, int draw);
void display(int bal, int draw, int fee);
//Main function
int main()
{
//Declarations
double accountBalance=0;
double overDrawn=0;
double balance;
double drawn;
double totalFee;
balance = acctBalInput(accountBalance);
drawn = overdrawInput(overDrawn);
totalFee = feeCalculation(balance, drawn);
display(balance, drawn, totalFee);
return 0;
}
//Input account balance.
int acctBalInput(double acct)
{
cout << "Enter account balance: ";
cin >> acct;
return acct;
}
//Input overdrawn times.
int overdrawInput(double draw)
{
cout << "Enter the number of times over drawn: ";
cin >> draw;
return draw;
}
//Calculates the total fee.
int feeCalculation( int bal, int draw)
{
int fee;
int feePercent = 0.01;
int drawFee = 5;
fee =(bal*feePercent)+(draw*drawFee);
return fee;
}
//Displays all the ouput.
void display(int bal, int draw, int fee)
{
cout <<"Balance: "<< bal<< endl
<<"Overdrawn: "<< draw << endl
<<"Fee: " << fee << endl;
return;
}
I tried googling to see a better way to write my code.
acctBalInput
andoverdrawInput
don't actually use their argument at all. Why are you passing arguments to them? You can just have them return local variables. Second, careful about mixingint
anddouble
. It can be tricky to use floating-point variables to handle currency correctly, but your current code will just be rounding a bunch of things to the nearest dollar value in places I'm not sure you intended it to.double
s. I've also changed the arguments that aren't used to local variables, and removed the now-superfluous local variables frommain
. I've also switched to initializing variables right where they're defined, instead of defining them and then initializing them on a later line.