Hi everyone, please bear in mind that I am new to this...
I have the following files and they will not compile, can anyone help?
Its an attempt to create a simple bankaccount class as the exercise has stated but looks as if I have failed miserably...
//create a bank account class
//constructor to provide an initial balance and validate greater than or equal to zero
//if not set balance to zero and display initial balance not valid
//provide 3 member functions
//memeber function credit to add to current balance
//member function debit to withdraw and ensure it does not exceed the balance
//if its does balance to remain unchanged but display message
//member function get balance to display current balance
#include<iostream>
#include<string>
usingnamespace std;
//BankAccount definition
class BankAccount
{
public:
BankAccount();//constructor
void creditAccount(float);//add to current balance
void debitAccount(float);//withdraw from account
float getBalance(float);//display current balance
private:
float balance;
};
//end of class BankAccount
//BankAaccount Class
//create a bank account class
//constructor to provide an initial balance and validate greater than or equal to zero
//if not set balance to zero and display initial balance not valid
//provide 3 member functions
//memeber function credit to add to current balance
//member function debit to withdraw and ensure it does not exceed the balance
//if its does balance to remain unchanged but display message
//member function get balance to display current balance
#include "BankAccount.h"//include definition of BankAccount Class
BankAccount::BankAccount()//constructor for BankAccount
{
cout << "Welcome to your new bank account.\n";
cout << "Please input your opening balance: ";
cin >> balance;
cout << endl;
if ( balance < 0 )
{
balance = 0;
cout << "Sorry your balance has been set to £" << balance << endl;
}
}
void creditAccount(float balance)
{
float deposit = 0;
cout << "How much would you like to deposit: ";
cin >> deposit;
balance = balance + deposit;
}
void debitAccount(float balance)
{
float debit = 0;
cout << "What amount would you like to withdraw: ";
cin >> debit;
if ( debit > balance )
{
cout << "Sorry you have £" << balance << " available\n";
cout << "Balanve left unchanged" << endl;
}
else
balance = balance - debit;
}
float getBalance(float balance)
{
return balance;
}
//create a bank account class
//constructor to provide an initial balance and validate greater than or equal to zero
//if not set balance to zero and display initial balance not valid
//provide 3 member functions
//memeber function credit to add to current balance
//member function debit to withdraw and ensure it does not exceed the balance
//if its does balance to remain unchanged but display message
//member function get balance to display current balance
//#include "BankAccount.h"
#include "BankAccount.cpp"
int main()
{
BankAccount newAccount;
return 0;
}
Since you wrote everything into one big file (which I do not recommend doing by the way), you don't need your #include statements, except for the necessary ones, like <iostream>. You don't need <string> because you're not doing C-style char arrays.
However, in the event that you DO separate implementation and output, you'd have exactly 3 files: a BankAccount.h, BankAccount.cpp, and Main.cpp. What you need to do for those is #include "BankAccount.h" inside BankAccount.cpp, and then for your Main.cpp, include the header file again.
@VFGHNG - The OP may have indeed had three files and simply pasted all three files between one set of code tags. Three sets of code tags would have been clearer.
@OP - Line 91. NEVER include a .cpp file. I received no errors when compiling (as a single file) after commenting out lines 38 and 91.
If you do indeed have three separate files, uncomment line 90 in your main.cpp file.
Sorry guys thats my mistake, I copied and pasted 3 seperta files the first file is upto line 28 and is BankAccount.h
next file is BankAccount.cpp and finishes at line 79
and last file is main
You don't need <string> because you're not doing C-style char arrays.
Not using C-strings is good, but <string> is not for C-strings, rather it is for the use of std::string. The include for the C-string functions would be <cstring>.