Reputation: 928
Testing Environment: MS Visual 2010 Ultimate.
I have two functions that need to pass from one to the other.
function 1:
//Record which account was chosen
string Account::getAccountType()
{
if (accountType == 1)
{
return "Account Type: Chequing";
}
else
{
return "Account Type: Savings";
}
};
This function will take the user's input and identify whether Chequing was selected or Savings.
Function 2:
//Print the Account Statement
void Account::PrintStatement()
{
cout << "First Name: " << firstName << endl;
cout << "Last Name: " << lastName << endl;
cout << "SIN Number: " << sinNumber << endl;
cout << "Account Type: " << accountType << endl;
cout << "Transactions: " << transactions << endl;
cout << "Final Balance: " << balance << endl;
};
This function gets all the global variables that had their values returned, or inputs saved, and prints all the inputs and calculations made.
main.cpp:
//Retrieve client information
cout << "Please fill out the information below for registration:" << endl;
cout << "Enter first name: ";
cin.getline(firstName, 255);
cout << "Enter last name: ";
cin.getline(lastName, 255);
cout << "Enter SIN number: ";
cin.getline(sinNumber, 255);
cout << "Enter initial balance: ";
cin >> balance;
cout << "Enter account type:\n Chequing - 1\n Savings - 2 (Or any number)\n Choice: ";
cin >> accountType;
cout << "\nPlease wait..." << endl;
My problem:
When accountType
is printed (via PrintStatement()
), it shows the integer value that the user inputted. I understand why it does that. I just don't know how to change it so that when either 1 or a different number was used, the PrintStatement()
function would display the appropriate account (Chequing or Savings).
What I tried: I tried changing function 1 into an integer based function and have accountType equal the option of Chequing or Savings, but integers cannot store a string value. I tried using char instead but I get the const char error.
What I'd like to do:
I want to be able to take the user's input of any number, and pass it to the PrintStatement()
(via another function) so it can display the appropriate account.
EDIT (adding additional code for clarity)
account.cpp
//Initializing account entities
Account::Account(char fn[], char ln[], char sin[], double bal, int actype, int trans)
{
strcpy(firstName, fn);
strcpy(lastName, ln);
strcpy(sinNumber, sin);
balance = bal;
accountType = actype;
transactions = 0;
};
account.h
#define ACCOUNT_H
#include <iostream>
#include <cstring>
using namespace std;
class Account {
public:
//Object constructor
Account(char firstName[], char lastName[], char sinNumber[], double balance, int accountType, int transactions);
//Object operations
double DepositAmt(double amount);
double WithdrawAmt(double amount);
void PrintStatement();
double getFinalBalance(double fbal);
string getAccountType();
double getTransactions (double cDeposit, double cWithdraw);
private:
//Object properties
char firstName[255];
char lastName[255];
char sinNumber[255];
double balance;
int accountType;
int transactions;
};
Upvotes: 0
Views: 1505
Reputation: 38929
Your getAccountType()
is merely a ternary statement.
If this is a public
accessor to accout::accountType
you should retain it and just use it in your line in question: cout << "Account Type: " << getAccountType() << endl
If however getAccountType()
is only used internally you should just replace it with a ternary statement: (accountType == 1 ? "Account Type: Chequing" : "Account Type: Savings")
So your line in question would become:
cout << "Account Type: " << (accountType == 1 ? "Account Type: Chequing" : "Account Type: Savings") << endl;
Note, as Neil Kirk mentioned, it's not good if these member variables are global. I believing the best of you when I say: "I'm sure they're members of class account
." That said you should probably denote them as members somehow. Microsoft for instance uses an "m_" + type prefix, so your member variable would be named: m_iAccountType
Upvotes: 1
Reputation: 46
In your PrintStatement() function change the output for the accountType from
cout << "Account Type: " << accountType << endl;
to
cout << getAccountType() << endl;
Upvotes: 2