Reputation: 15
i am needing to create a class with elements for a savings account, i have to ask the user for account type, name, ID, balance, and closure percentage. I then have to display what was just inputted, and then at the end call an imbedded class function to calculate the closure percentage via the inputted percentage and the balance. Then return that value to the main function and output the answer.
My issues: When i display the users input in the second function it gives me a bunch of long numbers or nothing at all (name). So how can i set the output to clean these up. Am i returning the wrong type? Any help would be greatly appreciated. Thank you, i am new to C++.
Also the layout and return types were defined in the parameters i was given for this task. GetInitalValues and DisplayValues must have a void parameter list and return void, and CalculatePenalty must have a void parameter list and return an appropriate type. And DisplayValues also has to be a constant inline member function.
MAIN CODE:
#include <iostream>
#include <iomanip>
#include "C1A8E1_SavingsAccount.h"
using namespace std;
void GetInitialValues(void);
const inline void DisplayValues(void);
int main()
{
SavingsAccount MyAccount;
GetInitialValues();
DisplayValues();
cout << "\nAccount Closure Penalty: " << MyAccount.CalculatePenalty()
<< "\n";
return 0;
}
Header File:
#include <iostream>
#ifndef C1A8E1_SAVINGSACCOUNT_H // Header Guard
#define C1A8E1_SAVINGSACCOUNT_H
using namespace std;
void GetInitialValues(void);
const inline void DisplayValues(void);
//Class definition
class SavingsAccount
{
public:
int type;
string ownerName;
long IDnbr;
double balance, closurePenaltyPercent;
double CalculatePenalty(void)
{
return closurePenaltyPercent * balance;
}
};
// Define inline function that displays users inputs
// This is where i am supposed to put this function "Outside the Class
// Definintion above" and where im having the issues
const inline void DisplayValues(void)
{
SavingsAccount SA;
cout << "\nAccount type: " << SA.type << "\nOwner Name: " << SA.ownerName
<< "\nID Number: " << SA.IDnbr << "\nAccount balance: " << SA.balance
<< "\nAccount closure penalty percent: " << SA.closurePenaltyPercent;
return void();
}
#endif
USER INPUT FUNCTION:
#include <iostream>
#include <string>
#include "C1A8E1_SavingsAccount.h"
using namespace std;
void GetInitialValues(void)
{
SavingsAccount AccountType, Name, ID, Balance, CP_percent;
cout << "Please Enter Account Type: ";
cin >> AccountType.type;
cout << "Enter Name: ";
cin >> ws;
getline(cin, Name.ownerName);
cout << "Enter ID Number: ";
cin >> ID.IDnbr;
cout << "Enter Balance: ";
cin >> Balance.balance;
cout << "Enter Closure Penalty Percent: ";
cin >> CP_percent.closurePenaltyPercent;
return void();
}
Upvotes: 0
Views: 88
Reputation: 1397
When you write SavingsAccount SA
or SavingsAccount AccountType, Name, ID, Balance, CP_percent
those both are creating new SavingsAccount objects. If you want to act on the one object you should not be creating new ones.
"member function GetInitialValues", currently your function is not a member function. Make it a member function. Example:
void GetInitialValues(void) {
cout << "Please Enter Account Type: ";
cin >> type;
cout << "Enter Name: ";
cin >> ws;
getline(cin, ownerName);
cout << "Enter ID Number: ";
cin >> IDnbr;
cout << "Enter Balance: ";
cin >> balance;
cout << "Enter Closure Penalty Percent: ";
cin >> closurePenaltyPercent;
return void();
}
The should also be done for DisplayValues()
.
These can be called from main()
as follows:
MyAccount.GetInitialValues();
MyAccount.DisplayValues();
Ideally, all values should be initialized by the constructor.
If you do not want to create member functions, you can pass by pointer or reference, I will do the latter for the next example:
void GetInitialValues(SavingsAccount& savings_account) {
cout << "Please Enter Account Type: ";
cin >> savings_account.type;
cout << "Enter Name: ";
cin >> ws;
getline(cin, savings_account.ownerName);
cout << "Enter ID Number: ";
cin >> savings_account.IDnbr;
cout << "Enter Balance: ";
cin >> savings_account.balance;
cout << "Enter Closure Penalty Percent: ";
cin >> savings_account.closurePenaltyPercent;
return void();
}
Note that this will only work if the member variables are public, which they currently are but it is a better practise to private the variables and use member functions instead.
If you decide to do this for DisplayValues()
as well, you should pass by constant reference since that is safer when values are not modified. This would look like this: void DisplayValues(const SavingsAccount& savings_account) {
These can be called from main()
as follows:
GetInitialValues(MyAccount);
DisplayValues(MyAccount);
Upvotes: 2