测试环境:Visual Studio 2010 Ultimate。
希望我的问题足够明确 - 我的目标是让用户选择一个选项
1. Enter 1 for Chequing
2. Enter 2 (or any number) for Savings
无论用户选择哪个选项,该程序都会打印" Account Type: Chequing [or] Savings
"
我已经设法接受用户的输入并获得了我尝试完成的一半。我的问题是我选择的任何数字,打印一个额外的数字,见图。
图片来自之前的版本,我已修复了选择问题,但附加编号仍会在帐户类型下打印。
我的account.cpp(包括account.h):
int Account::WhichOne(int actype)
{
if (actype == 1)
{
cout << "Account Type: Chequing" << endl;
}
else
cout << "Account Type: Savings" << endl;
return actype;
};
我的main.cpp
int main()
{
char firstName[255];
char lastName[255];
char sinNumber[255];
double balance = 0;
int choice;
int checker;
int accountType = 0;
int transactions = 0;
//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\n Choice: ";
cin >> choice;
cout << "\nPlease wait..." << endl;
//Creating the account
Account account(firstName, lastName, sinNumber, balance, accountType, transactions);
double deposit;
double withdraw;
double amount;
double ammount;
cout << "Amount to deposit: ";
cin >> amount;
deposit = account.DepositAmt(amount);
cout << "Your new balance is: " << deposit << endl;
cout << "Amount to withdraw: ";
cin >> ammount;
deposit = account.WithdrawAmt(ammount);
cout << "Your new balance is: " << deposit << endl;
accountType = account.WhichOne(choice);
cout << "" << accountType << endl;
}
我真的不知道该怎么做。我对return actype
的问题表示肯定,但无论我在那里放置什么回报值,它都会被打印出来。
答案 0 :(得分:1)
你这样做(第二档):
accountType = account.WhichOne(choice);
然后你这样做(第一个文件):
return actype;
但是,actype
是你的论据,因此返回的内容以及最终由命令cout << "" << accountType << endl;
打印的内容。之前的打印,即Account Type: Chequing
在方法内部完成,您可以在方法体中注意到。
我可以帮助您修复代码,问题是我不知道您希望如何修复代码。
编辑:由于您在构造函数中定义了项account
和accountType
,因此我认为您将其存储在成员变量中。我们假设该变量也称为accountType
。我将更改您的WhichOne
功能,如下所示(我将名称更改为更有意义的名称):
std::String Account::getAccountType()
{
if (accountType == 1)
{
return "Account Type: Chequing";
}
else
return "Account Type: Savings";
}
}
然后替换这些代码行
accountType = account.WhichOne(choice);
cout << "" << accountType << endl;
这一个
cout << account.getAccountType() << endl;
答案 1 :(得分:1)
由于我能够理解该要求,因此以下代码应该有效:
account.cpp
string Account::WhichOne(int actype)
{
string type;
if (actype == 1)
{
cout << "Account Type: Chequing" << endl;
type="Chequing Account";
}
else{
cout << "Account Type: Savings" << endl;
type= "Saving Account";
}
return type;
};
的main.cpp
string accountType;
accountType = account.WhichOne(choice);
cout << "" << accountType << endl;
答案 2 :(得分:0)
void Account::WhichOne(int actype)
{
if (actype == 1)
{
cout << "Account Type: Chequing" << endl;
}
else
cout << "Account Type: Savings" << endl;
}
问题是你要返回int值并在代码的最后一行打印,如果你在函数内部打印则使其无效,或者如果你想打印消息只返回一个包含该消息的字符串在其他地方