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.

Mr. Puli
  • 11
  • 2
  • 2
    Note that ["module"](https://en.cppreference.com/w/cpp/language/modules) 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. – Nathan Pierson Jun 02 '23 at 01:11
  • 1
    See also [here](https://idownvotedbecau.se/imageofcode) for an explanation of why you should be posting code as text, not as images. – Nathan Pierson Jun 02 '23 at 01: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](https://stackoverflow.com/questions/588004/is-floating-point-math-broken) 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. – Nathan Pierson Jun 02 '23 at 01: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](https://stackoverflow.com/q/388242/4581301) 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. – user4581301 Jun 02 '23 at 01:32
  • That content rating goes both ways, mind you. If you don't fix up the question to bring it in line with [ask] and the [asking questions](https://stackoverflow.com/help/asking) portion of the Help Center, it'll be viewed as not useful to the site, downvoted, and removed. A question eeds to be seen as useful to more than just the asker to be preserved. Practically nothing sinks a question faster around here than images of text. – user4581301 Jun 02 '23 at 01:36
  • Firewalls are preventing me from accessing your code screenshots. Also, many IDEs can't accept code in image form. Please post a plaintext [mcve]. – Thomas Matthews Jun 02 '23 at 01:41
  • Thank you for the heads up about asking questions here. I assumed it would be easier to view it in a picture. In the future I will use code instead. This is my first question on stackoverflow. – Mr. Puli Jun 02 '23 at 01:43
  • Answering the question asked in the title, no. A function does not necessarily need parameters. The information processed in the function could come from asking the user, reading a file, accessing a database, or countless other sources. My personal preference is to break things up so some functions do nothing but get and return data which is then passed into other functions to do the processing. As you get further in your learning you'll define classes that gather data into member variables that will be used by other class methods without passing anything (or anything you can see). – user4581301 Jun 02 '23 at 01:44
  • @NathanPierson are you saying I can do the following? balance = acctBalInput(acct); drawn = overdrawInput(drawn);........................Could you write some code with it to clarify? – Mr. Puli Jun 02 '23 at 02:17
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Jun 02 '23 at 02:36
  • Have you tried experimenting with removing the variables in question? Or moving them elsewhere? Or comparing with what you do with `fee` in `feeCalculation`? While asking questions is good, you can sometimes get quicker results by trying different ideas (although as recommended, it helps to start with a good book to form the basis of those ideas). – TheUndeadFish Jun 02 '23 at 02:44
  • 2
    @Mr.Puli Take a look at [this demo](https://godbolt.org/z/WWfzMx4o6). I've taken the liberty of changing a few variables to `double`s. 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. – Nathan Pierson Jun 02 '23 at 02:59
  • Side note: [Why is "using namespace std;" considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – Jesper Juhl Jun 02 '23 at 04:43
  • @Mr.Puli - In addition to everything else, here's a stylistic comment (about *your* comments :-). Instead of having `//Input account balance.` to explain the code, you **are** allowed to call the function that, like `int Input_account_balance()`. Really great code doesn't *need* explanations! – BoP Jun 02 '23 at 08:32
  • @NathanPierson thank you for the demo! That was what I was looking for! I had to see it to understand it. And thank you to all you others for your coaching too! I hope one day I can help others here too when my knowledge grows. – Mr. Puli Jun 02 '23 at 11:48

0 Answers0