Search code examples
c++static-functions

How to calculate user input using non-static functions


My testing environment: Visual Studio 2010 Ultimate.

the error I received:

main.cpp(39): error C2352: 'Account::DepositAmt' : illegal call of non-static member function

I've tried changing it to static functions but it only caused more errors.

account.cpp (23): error C2597: illegal reference to non-static member 'Account::balance' account.cpp(40): error C3867: 'Account::balance': function call missing argument list; use '&Account::balance' to create a pointer to member

I've tried to fix using the suggestions of trying to reference but failed miserably.

the objective, to be able to take the user input for the initial balance, and then if the user is making a deposit add it to the balance, if a user makes a withdrawal, subtract it, then print final balance. I am required to create member functions

All I want is to figure out how to achieve this without having to change to static functions (if at all possible).

This is my code for the

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

private:
    //Object properties
    char firstName[255];
    char lastName[255];
    char sinNumber[255];
    double balance;
    int accountType;
    int transactions;
};

Account.cpp

#include "account.h"

//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 = trans;
};

//Despoit amount to account of user, will return the balance now
double Account::DepositAmt(double amount) 
{
    if (amount < 0){
        cout << "You cannot deposit a negative balance!" << endl;
        return 0;
    }

    //this will be the balance now, and it adds balances, when we do a deposit
    balance += amount;

    return balance;

};

//Withdrawal amount from account of user
double Account::WithdrawAmt(double withdrawal)
{

    //if the withdraw amount is 0 or in minus then return 0
    if (withdrawal < 0)
    {
        cout<<"sorry we cannot process your withdraw at the moment, its either 0 or in minus"<<endl;
        return 0;   
    };
    //if what we are trying to withdraw is bigger than our balance, then it should fail too
    if (withdrawal > balance)
    {
        cout<<"Sorry the withdraw amount exceeds the account balance"<<endl;
        return 0;
    };

    //we deposited some amount in the deposite method, then we should have some money to withdraw
    balance = balance - withdrawal;

    return balance;

};


//Get final balance after withdrawal
 double Account::getFinalBalance(double fbal)
{
        //this will return the remaining amount
        return balance;
};

//Print remaining balance 
void Account::PrintStatement()
{
        cout<<"First Name: "<<firstName<<endl;
        cout<<"Last Name: " <<lastName<<endl;
        cout<<"SIN Number: " <<sinNumber<<endl;
        cout<<"Initial Balance: "<<balance<<endl;
        cout<<"Account Type: "<<accountType<<endl;
        cout<<"Transactions: "<<transactions<<endl;

};

Main.cpp

#include <iostream>
#include <string>
#include "account.h"


using namespace std;

int main()
{
    char firstName[255];
    char lastName[255];
    char sinNumber[255];
    double balance;
    int accountType;
    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 << "Please enter your initial balance: ";
    cin >> balance;
    cout << "Enter account type:\n-1 for Chequing\n-2 for Savings\n:::::: ";
    cin >> accountType;
    cout << "Please wait..." << endl;

    //Creating the account
    Account account(firstName, lastName, sinNumber, balance, accountType, transactions);

    double deposit;
    double withdraw;
    double amount;
    cout << "Amount to deposit: ";
    cin >> &Account::DepositAmt(deposit);
    cout << "Your new balance is: " << deposit << endl;




}

Solution

  • You should at first get user input to the variable, and then pass it to a method:

    cin >> amount;
    deposit = account.DepositAmt(amount);