0

Testing environment: 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"

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):

int  Account::WhichOne(int actype)
{   
    if (actype == 1)
    { 
        cout << "Account Type: Chequing" << endl;

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

    return actype;

};

my 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.

SorryEh
  • 900
  • 2
  • 15
  • 47
  • 1
    Why don't you use `std::string` instead of char arrays, so you can support any lengths? Also be aware floating-point is not 100% accurate and not suitable to store currency. – Neil Kirk Feb 23 '15 at 02:03
  • `cout << "" << accountType << endl;` Won't this print out the number again? – Neil Kirk Feb 23 '15 at 02:04
  • good point about the strings, will fix that. as for `cout<<""< – SorryEh Feb 23 '15 at 02:06
  • `accountType` is an integer, so it causes the number to be printed. I'm not sure why `WhichOne` returns an integer which you just passed to it. – Neil Kirk Feb 23 '15 at 02:10
  • actually sorry, the reason I did char is because im using strcpy for the end of the program to reprint everything as statement....as for `WhichOne` i thought because an integer needs to be passed to it, for the if statement to validate the choices, it also has to return the integer that got validated? – SorryEh Feb 23 '15 at 02:14
  • 1
    There's no need for `strcpy`, you can use `=` and `+=` on strings. – Neil Kirk Feb 23 '15 at 02:28

3 Answers3

1

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;. The previous printing, namely Account Type: Chequing is done inside the method, as you can notice in the method's body.

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. Let's suppose that variable is called accountType as well. I will change your WhichOne function as follows (I'm changing the name to a more meaningful one):

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;
codingEnthusiast
  • 3,800
  • 2
  • 25
  • 37
  • all I want is to be able to print back the user's choice of chequing or savings (with savings being selected with any number, and chequing only being selected by number 1). I'm just trying to remind the user which account they chose has been affected. – SorryEh Feb 23 '15 at 02:16
  • 1
    I've edited the question, adding a solution to it. If you're not storing the accountType as a member variable, you might as well include it as an argument to that method. – codingEnthusiast Feb 23 '15 at 02:39
  • so why is string the better option here? and I get now why the extra number was being printed, I was going based on the assumption that since this was working in the same fashion for `deposit=account.WithdrawAmt` that it would work for this situation. Both are doing if statements but WithdrawAmt is actually returning my remaining balance. – SorryEh Feb 23 '15 at 02:46
  • 1
    We'd better print the string instead of the number, as it is more user-friendly. Also, std::string is usually preferred over a char pointer. If you're wondering why, please read this: http://stackoverflow.com/questions/801209/char-vs-stdstring-in-c – codingEnthusiast Feb 23 '15 at 15:58
  • thank you very much Naltipar, you've been extremely helpful! I'm making notes of everything. – SorryEh Feb 23 '15 at 15:59
1

As I am able to understand the requirement, following code should work:

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;
s_ag
  • 82
  • 1
  • 7
0
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