1

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.

15
  • 2
    Note that "module" has a specific meaning in C++. You are talking about functions. Why exactly do you want to avoid passing variables to functions and returning values from functions? It's a normal, idiomatic thing to do. Jun 2 at 1:11
  • 1
    See also here for an explanation of why you should be posting code as text, not as images. Jun 2 at 1:12
  • 1
    Some general comments about your code: acctBalInput and overdrawInput 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 mixing int and double. 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. Jun 2 at 1:15
  • 1
    Note: Until you know the C++ fundamentals well enough to recognize idiots and frauds, googling for code is fraught with risk. The idiots outnumber the experts about 10:1, so you are a lot more likely to find yourself learning from idiots if you try to learn online. Here's a list of good books Note: It's Stackoverflow's content rating system and active management of the content that both made it a useful and generally trusty Internet information source and earned it a reputation for toxicity. Idiots generally don't like being told they're idiots. Jun 2 at 1:32
  • 2
    @Mr.Puli Take a look at this demo. I've taken the liberty of changing a few variables to doubles. I've also changed the arguments that aren't used to local variables, and removed the now-superfluous local variables from main. I've also switched to initializing variables right where they're defined, instead of defining them and then initializing them on a later line. Jun 2 at 2:59

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.