简体   繁体   English

如果语句打印其他值

[英]If statement printing additional values

Testing environment: Visual Studio 2010 Ultimate. 测试环境:Visual Studio 2010 Ultimate。

Hopefully my question was clear enough--my objective is to have a user select an option 希望我的问题很清楚-我的目标是让用户选择一个选项

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

so whichever option the users chooses, the program will print " Account Type: Chequing [or] Savings " 因此,无论用户选择哪种选项,程序都会打印“ Account Type: Chequing [or] Savings

I've managed to accept the user's input and get half of what I was trying done. 我已经设法接受用户的输入,并得到了我尝试完成的一半。 My issue is any number i choose, an additional number gets printed, see picture. 我的问题是我选择的任何号码,还会打印一个附加号码,请参见图片。

http://imgur.com/FnFkQQ8

The picture is from a previous build, I have fixed the selection issue but the additional number still gets printed under Account Type. 图片来自以前的版本,我已经解决了选择问题,但其他数字仍会打印在“帐户类型”下。

my account.cpp (include account.h): 我的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;

};

my main.cpp 我的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;
}

I'm really not sure what to do. 我真的不知道该怎么办。 I'm positive my issue with the return actype but no matter what return value i put there it gets printed as well. 我很肯定我对return actype问题,但是无论我放在那里的返回值如何,它也会被打印出来。

You do this (second file): 您执行此操作(第二个文件):

accountType = account.WhichOne(choice);

And then you do this (first file): 然后执行此操作(第一个文件):

return actype;

But, actype is your argument, so that's what gets returned and what is printed eventually by the command cout << "" << accountType << endl; 但是, actype是您的参数,因此返回的内容是最终由命令cout << "" << accountType << endl;打印的内容cout << "" << accountType << endl; . The previous printing, namely Account Type: Chequing is done inside the method, as you can notice in the method's body. 您可以在方法主体中注意到,先前的打印即Account Type: Chequing在方法内部完成。

I could help you fix your code, the problem is that I do not know how you would like your code fixed. 我可以帮助您修复代码,问题是我不知道您希望如何修复您的代码。

EDIT: Since you are defining the item account and accountType is in the constructor, I suppose you store it in a member variable. 编辑:由于您正在定义项目accountaccountType在构造函数中,我想您将其存储在成员变量中。 Let's suppose that variable is called accountType as well. 让我们假设该变量也称为accountType I will change your WhichOne function as follows (I'm changing the name to a more meaningful one): 我将如下更改您的WhichOne函数(将名称更改为更有意义的名称):

std::String Account::getAccountType()
{
  if (accountType == 1)
  {
    return "Account Type: Chequing";
  }
  else
    return "Account Type: Savings";
  }
}

and then replace these lines of code 然后替换这些代码行

accountType = account.WhichOne(choice);
cout << "" << accountType << endl;

with this one 与这个

cout << account.getAccountType() << endl;

As I am able to understand the requirement, following code should work: 据我所知,以下代码应该可以工作:

account.cpp 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 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;

}

the problem is that you are returning the int value and it gets printed in the last line of your code, make it void if you are printing inside the function or just return a string containing the message if you want to print the message somewhere else 问题是您要返回int值,并且该值会在代码的最后一行打印出来;如果要在函数内部进行打印,则将其设为空;如果要在其他地方打印该消息,则只需返回一个包含消息的字符串即可

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM