测试环境:MS Visual 2010 Ultimate。
我有两个需要从一个传递到另一个的函数。
功能1:
//Record which account was chosen
string Account::getAccountType()
{
if (accountType == 1)
{
return "Account Type: Chequing";
}
else
{
return "Account Type: Savings";
}
};
此功能将接受用户的输入,并确定是选择了Chequing还是存储。
功能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;
};
此函数获取所有返回值或保存输入的全局变量,并打印所有输入和计算。
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;
我的问题:
打印accountType
时(通过PrintStatement()
),它会显示用户输入的整数值。我明白为什么会那样做。我只是不知道如何更改它,以便当使用1或不同的数字时,PrintStatement()
功能将显示相应的帐户(Chequing或Savings)。
我尝试了什么: 我尝试将函数1更改为基于整数的函数,并使accountType等于Chequing或Savings选项,但整数不能存储字符串值。我尝试使用char代替但是我得到了const char错误。
我想做的事情:
我希望能够获取用户输入的任何数字,并将其传递给PrintStatement()
(通过其他功能),以便它可以显示相应的帐户。
编辑 (为了清晰起见,请添加其他代码)
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;
};
答案 0 :(得分:2)
在PrintStatement()函数中,从
更改accountType的输出cout << "Account Type: " << accountType << endl;
到
cout << getAccountType() << endl;
答案 1 :(得分:1)
你的getAccountType()
只是一个三元声明。
如果这是public
accout::accountType
的{{1}}访问者,您应该将其保留,并在相关专栏中使用它:cout << "Account Type: " << getAccountType() << endl
但是,如果getAccountType()
仅在内部使用,则应使用三元语句替换它:(accountType == 1 ? "Account Type: Chequing" : "Account Type: Savings")
所以你的问题将成为:
cout << "Account Type: " << (accountType == 1 ? "Account Type: Chequing" : "Account Type: Savings") << endl;
注意,如Neil Kirk mentioned,这些成员变量是全局变量并不好。当我说:“我确信他们是class account
的成员时,我相信你们中最好的。”那说你应该以某种方式将它们表示为成员。 Microsoft for instance使用“m_”+类型前缀,因此您的成员变量将命名为:m_iAccountType