CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com

Thread: Using Structure to Create Accounts and Calculating Currentbalance.

1. Junior Member
Join Date
Nov 2016
Posts
7

Using Structure to Create Accounts and Calculating Currentbalance.

As part of my C++(OOP) learning process, I'm I have a tempted a question that requires a user to enter details of 3 bank account holders then calculate and display their Current Balances. I know current balances=Open balance +Deposit. I however cannot figure out where exactly to include the calculation and have all results display after entering accounts holders' details. Below is my code. Your inputs/guidance will be highly appreciated. As I indicated before, I'm still a beginner by all standards.
Thanks.

Code:
```#include<iostream>
using namespace std;

struct Account
{
char AccName[12];
int Openbalance, Deposit;
//where do I insert current balance calculation(Currentbalanace=Openbalance+Deposit?
} x[3];

int main()
{
int Currentbalance;
int Openbalance;
int Deposit;

Currentbalance = Openbalance + Deposit;

int i;

for (i = 0; i <= 2; i++)
{
cout << "Enter name, openbalance and depostit for " << i + 1 << endl;
cin >> x[i].AccName;
cin >> x[i].Openbalance;
cin >> x[i].Deposit;
}

for (i = 0, i <= 2;i++;)
{
cout << x[i].AccName << "\t" << x[i].Openbalance << "\t" << x[i].Deposit << x[i].Openbalance + x[i].Deposit << endl;
cout << "Current Balance is " << x[i].Openbalance + x[i].Deposit << endl;
}

system("pause");
return 0;
}```
Last edited by 2kaud; June 11th, 2017 at 10:51 AM. Reason: Corrected code tags

2. Re: Using Structure to Create Accounts and Calculating Currentbalance.

I however cannot figure out where exactly to include the calculation and have all results display after entering accounts holders' details
haven't you done this is the cout statements? You could also have
Code:
```struct Account
{
char AccName[12];
int Openbalance, Deposit;
int Currbalance;
//where do I insert current balance calculation(Currentbalanace=Openbalance+Deposit?
} x[3];```
Code:
```cin >> x[i].Deposit;
x[i].Currbalance = x[1].Openbalance + x[i].Deposit;```
then the cout statements becomes
Code:
```        cout << x[i].AccName << "\t" << x[i].Openbalance << "\t" << x[i].Deposit << endl;
cout << "Current Balance is " << x[i].Currbalance << endl;```
Why are the balances of type int? Wouldn't a type float be more appropriate?

Also
Code:
`for (i = 0; i <= 2; i++)`
This would be more usual to write as
Code:
`for (i = 0; i < 3; ++i)`

3. Member
Join Date
Feb 2017
Posts
296

Re: Using Structure to Create Accounts and Calculating Currentbalance.

Originally Posted by meshc++
I know current balances=Open balance +Deposit. I however cannot figure out where exactly to include the calculation
In OO the natural way would be to put the calculation in a member function of Account, like

Code:
```struct Account
{
char AccName[12];
int Openbalance, Deposit;

int Currbalance() { // a member function
return Openbalance + Deposit; // add and return result
}
} x[3];
//
cout << "Current Balance is " << x[i].Currbalance() << endl; // usage```

4. Member
Join Date
Feb 2017
Posts
296

Re: Using Structure to Create Accounts and Calculating Currentbalance.

Originally Posted by 2kaud
Why are the balances of type int? Wouldn't a type float be more appropriate?
In that case one should use a decimal floating point. As far as I know it is not yet part of the C++ standard but I know it has been suggested. I also know Intel has an implementation,

https://software.intel.com/en-us/art...t-math-library

Another way to handle money/currency is to use a dedicated Money class.

Both are obviously overkill for the OP but in any case it is better to stick with an integer representation because the use of binary floating point is not recommended when dealing with money/currency.
Last edited by wolle; June 12th, 2017 at 01:56 AM.

5. Re: Using Structure to Create Accounts and Calculating Currentbalance.

In OO the natural way would be to put the calculation in a member function of Account
In c++, the OO way would be to have Account as a full class with setters and getters and with the member variables private. Consider
Code:
```#include <iostream>
#include <string>
using namespace std;

class Account
{
public:
Account() = default;
Account(const string& name, double bal) : AccName(name), openbalance(bal) {}

void setName(const string& name);
string getName() const;
void setOpen(double bal);
double getOpen() const;
double getDeposit() const;
double getBalance() const;

private:
string AccName;
double openbalance = 0.0;
double deposit = 0.0;
};

void Account::setName(const string& name)
{
AccName = name;
}

string Account::getName() const
{
return AccName;
}

void Account::setOpen(double bal)
{
openbalance = bal;
}

double Account::getOpen() const
{
return openbalance;
}

{
deposit = dep;
}

double Account::getDeposit() const
{
return deposit;
}

double Account::getBalance() const
{
return openbalance + deposit;
}

int main()
{
const size_t numDeposits = 3;

Account acts[numDeposits];

for (size_t i = 0; i < numDeposits; ++i)
{
string name;
double balance, deposit;
cout << "Enter name, open balance and deposit for " << i + 1 << endl;
cin >> name >> balance >> deposit;
acts[i].setName(name);
acts[i].setOpen(balance);
}

for (size_t i = 0; i < numDeposits; ++i)
{
cout << acts[i].getName() << "\t" << acts[i].getOpen() << "\t" << acts[i].getDeposit() << endl;
cout << "Current Balance is " << acts[i].getBalance() << endl;
}
}```
Last edited by 2kaud; June 12th, 2017 at 03:27 AM.

6. Re: Using Structure to Create Accounts and Calculating Currentbalance.

better to stick with an integer representation because the use of binary floating point is not recommended when dealing with money/currency
But this is c++ 101. Are you suggesting that only integer representations of money can be entered? How would you have pounds/pence or dollars/cent entered - as two values? Then that complicates the calculations of balance etc. For a final banking project probably, but for this......

7. Member
Join Date
Feb 2017
Posts
296

Re: Using Structure to Create Accounts and Calculating Currentbalance.

Originally Posted by 2kaud
But this is c++ 101. Are you suggesting that only integer representations of money can be entered? How would you have pounds/pence or dollars/cent entered - as two values? Then that complicates the calculations of balance etc. For a final banking project probably, but for this......
So you are arguing that in the name of simplicity the OP should use floating point both for the internal and external representations of money?

Well, it's up to the OP but at least s/he should know that money is not well represented by a floating point. Knowing why is very important and certainly C++ 101. It may even one day stand between the OP and that job s/he wants, even if it's not at a bank.

And then there is the important principle of separation of concerns. I/O is one concern, calculations is another. That is also C++ 101. It's not a very good idea to intertwine how money is entered with how it's stored inside. There's no reason why money cannot be represented differently in each situation with conversions in between.
Last edited by wolle; June 13th, 2017 at 12:02 AM.

8. Member
Join Date
Feb 2017
Posts
296

Re: Using Structure to Create Accounts and Calculating Currentbalance.

Originally Posted by meshc++
As part of my C++(OOP) learning process,
You have asked what essentially is the same question before,

http://forums.codeguru.com/showthrea...Member-Methods

It's very hard to teach someone the very C++ basics at a forum. I suggest you get a book.

9. Member
Join Date
Feb 2017
Posts
296

Re: Using Structure to Create Accounts and Calculating Currentbalance.

Originally Posted by 2kaud
In c++, the OO way would be
I have a few comments on that:

In C++ struct and class are conceptually equivalent with few differences. If you know how they differ you can use either although convention has it that struct is preferred for very simple objects and class for more complex. From an OO standpoint it doesn't matter.

It is not good OO to routinely supply getters and setters for member variables. Instead of shuffling data in and out, objects should rather be asked to do things. Setters should be kept to a minimum and ideally objects should be immutable, meaning their internal state never changes after creation.

In C++ you can either define member functions within a class/struct definition, or declare them inside and define them somewhere else outside. How this is done has nothing to do with OO.
Last edited by wolle; June 13th, 2017 at 06:11 AM.

Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•