If 语句打印附加值

If statement printing additional values

测试环境:Visual Studio2010 Ultimate.

希望我的问题足够清楚——我的 objective 是让用户 select 有一个选项

1. Enter 1 for Chequing
2. Enter 2 (or any number) for Savings

所以无论用户选择哪个选项,程序都会打印“Account Type: Chequing [or] Savings

我已经设法接受了用户的输入并完成了我尝试完成的一半。我的问题是我选择的任何数字,都会打印一个额外的数字,见图。

The picture is from a previous build, I have fixed the selection issue but the additional number still gets printed under Account Type.

我的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 有问题,但无论我放什么 return 值,它也会被打印出来。

你这样做(第二个文件):

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;

根据我的理解,下面的代码应该可以工作:

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;
void  Account::WhichOne(int actype)
{   
    if (actype == 1)
    { 
        cout << "Account Type: Chequing" << endl;

    }
    else
        cout << "Account Type: Savings" << endl;

}

问题是您正在 returning int 值并且它被打印在代码的最后一行,如果您在函数内部打印或只是 return a如果您想在其他地方打印消息,则包含消息的字符串