0

Testing Environment: MS Visual 2010 Ultimate.

I have two functions that need to pass from one to the other.

function 1:

//Record which account was chosen
string Account::getAccountType()
{
  if (accountType == 1)
  {
    return "Account Type:  Chequing";
  }
  else
  {
    return "Account Type: Savings";
  }

};

This function will take the user's input and identify whether Chequing was selected or Savings.

Function 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;

    };

This function gets all the global variables that had their values returned, or inputs saved, and prints all the inputs and calculations made.

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;

My problem: When accountType is printed (via PrintStatement()), it shows the integer value that the user inputted. I understand why it does that. I just don't know how to change it so that when either 1 or a different number was used, the PrintStatement() function would display the appropriate account (Chequing or Savings).

What I tried: I tried changing function 1 into an integer based function and have accountType equal the option of Chequing or Savings, but integers cannot store a string value. I tried using char instead but I get the const char error.

What I'd like to do: I want to be able to take the user's input of any number, and pass it to the PrintStatement() (via another function) so it can display the appropriate account.

EDIT (adding additional code for clarity)

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;
};
SorryEh
  • 900
  • 2
  • 15
  • 47
  • How about writing a function that returns the right string based on the account type, and printing that? – sje397 Mar 01 '15 at 02:12
  • `cout << "Account Type: " << getAccountType() << endl;` ? – vines Mar 01 '15 at 02:12
  • Don't use global variables. – Neil Kirk Mar 01 '15 at 02:13
  • @NeilKirk Hopefully this is all in a class. – Jonathan Mee Mar 01 '15 at 02:23
  • 1
    @JonathanMee it is indeed all in a class, sorry for not clarifying that. – SorryEh Mar 01 '15 at 02:36
  • @NeilKirk why do you say not to use global variables? – SorryEh Mar 01 '15 at 02:37
  • 1
    http://stackoverflow.com/questions/484635/are-global-variables-bad – Neil Kirk Mar 01 '15 at 02:39
  • @NeilKirk wow i never saw it like that, thank you. That makes so much sense. Ill make my adjustments immediately before i expand the program. – SorryEh Mar 01 '15 at 02:42
  • 1
    You're welcome. For example, if `firstName` is a global variable, how could you handle multiple people's accounts.. – Neil Kirk Mar 01 '15 at 02:44
  • 1
    @Umeed +1 to offset all the downvotes. This seems like a well thought out question. I think the downvotes may just be due to the unclarity of whether your variables are member variables. I've included Microsoft's style guide in [my answer](http://stackoverflow.com/a/28789497/2642059) which may be helpful to you in that regard. Hang in there: http://meta.stackoverflow.com/questions/266370/consideration-for-removing-the-downvote-button-from-questions/266670#266670 – Jonathan Mee Mar 01 '15 at 02:45

2 Answers2

2

In your PrintStatement() function change the output for the accountType from

cout << "Account Type:  " << accountType  << endl;

to

cout << getAccountType() << endl;
Waldhuette
  • 46
  • 5
  • I have tried that, sorry for not mentioning it. But when i do that, it gives me " `no operator << matches these operands` " – SorryEh Mar 01 '15 at 02:38
  • Can you show the part where you declare your variables and the section with the imports ? – Waldhuette Mar 01 '15 at 02:42
  • 1
    `getAccountType()` returns a `string` (which is presumably a `std::string` and not some other `string` type). There is an `<<` operator that writes a `std::string` to `std::cout`. – Remy Lebeau Mar 01 '15 at 02:45
  • Done! added the declaration for the functions file, account.cpp and added the full script of the header file, account.h – SorryEh Mar 01 '15 at 02:45
  • 1
    Do you include the required headers such as ``? – Neil Kirk Mar 01 '15 at 02:45
  • 1
    Yes, `account.h` needs a `#include ` statement. – Remy Lebeau Mar 01 '15 at 02:47
  • @NeilKirk !!! oh man I can't believe i forgot to add `` to the account.cpp and account.h.....ah wow i feel so dumb right now lol. Thank you so much guys! its working! Do I just save this as the answer, or one of you can post it as an answer and I select that – SorryEh Mar 01 '15 at 02:49
  • @Umeed your initial question was not regarding the no operator << matches these operands " so I think you can mark this as the correct answer but I'm fairly new to stackoverflow so I don't know what is best practice here. – Waldhuette Mar 01 '15 at 03:02
  • @Waldhuette well the answer you provided was ultimately the answer I needed so I chose this anyway. thanks a lot Waldhuette! – SorryEh Mar 01 '15 at 03:09
1

Your getAccountType() is merely a ternary statement.

If this is a public accessor to accout::accountType you should retain it and just use it in your line in question: cout << "Account Type: " << getAccountType() << endl

If however getAccountType() is only used internally you should just replace it with a ternary statement: (accountType == 1 ? "Account Type: Chequing" : "Account Type: Savings")

So your line in question would become:

cout << "Account Type:  " << (accountType == 1 ? "Account Type:  Chequing" : "Account Type: Savings") << endl;

Note, as Neil Kirk mentioned, it's not good if these member variables are global. I believing the best of you when I say: "I'm sure they're members of class account." That said you should probably denote them as members somehow. Microsoft for instance uses an "m_" + type prefix, so your member variable would be named: m_iAccountType

Community
  • 1
  • 1
Jonathan Mee
  • 37,899
  • 23
  • 129
  • 288
  • thats excellent advice Jonathan, thank you. I will keep all of this in mind for expanding this project. I feel so much more motivated now that I'm understanding things more. thanks so much – SorryEh Mar 01 '15 at 02:52