Help! My first cpp program

Hi fellow cpp beginners,

I started learning cpp programming officially yesterday. Here's my first cpp program which is fraught with problems. It consists of 3 files and emulates a bank ATM. It is an exercise from Deitel's book "C++ how to program".

I use Eclipse+Cygwin (both the latest version) on a Win 8 PC. The compiler appeared to have problems with account.displayAcctBalance() in the main.cpp. Whenever it is called (line 24, 26, 33, and 35), an error pops up with a msg like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
Multiple markers at this line
	- cannot convert 'account.BankAccount::displayAcctBalance()' (type 'void') to type 'const signed 
	 char*'
	- cannot convert 'account.BankAccount::displayAcctBalance()' (type 'void') to type 'const char*'
	- mismatched types 'const _CharT*' and 'void'
	- cannot convert 'account.BankAccount::displayAcctBalance()' (type 'void') to type 'const 
	 unsigned char*'
	- mismatched types 'const std::basic_string<_CharT, _Traits, _Alloc>' and 'void'
	- candidates are:
	- no match for 'operator<<' (operand types are 'std::basic_ostream<char>' and 'void')
	- cannot convert 'account.BankAccount::displayAcctBalance()' (type 'void') to type 'unsigned char'
	- cannot convert 'account.BankAccount::displayAcctBalance()' (type 'void') to type 'signed char'
	- cannot convert 'account.BankAccount::displayAcctBalance()' (type 'void') to type 'char'
	- deduced conflicting types for parameter '_CharT' ('char' and 'void')


I'm at my wit's end with this problem. I cannot figure it out. Can someone help? Your insights will be appreciated! My code can be seen below.

tcollar

***************

BankAccount.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef BACNKACCOUNT_H_
#define BACNKACCOUNT_H_

using namespace std;

class BankAccount
    {
		public:
			BankAccount (int);
			void setAcctBalance (int);
			int getAcctBalance ();
			int Credit (int);
			int Debit (int);
			void displayAcctBalance ();
		private:
			int AcctBalance;
    };
#endif /* BACNKACCOUNT_H_ */ 


BankAccount.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <iostream>
using namespace std;
#include "BankAccount.h"

BankAccount::BankAccount( int balance )
{
   setAcctBalance(balance);
}

void BankAccount::setAcctBalance (int balance)
{
	AcctBalance=balance;
}

int BankAccount::getAcctBalance ()
{
	return AcctBalance;
}

int BankAccount::Credit (int credit)
{
	while (credit<0)
		{
		cout<<"The number is incorrect. Please key in the number again:\n" <<endl;
		cin>>credit;
		if (credit>0)
			break;
		}
	AcctBalance=AcctBalance+credit;
	return AcctBalance;
}

int BankAccount::Debit (int debit)
{
	while (debit<0 || debit>AcctBalance)
		{
		cout<<"The number is either incorrect or greater than your balance. Please try again:\n" <<endl;
		cin>>debit;
		if (debit>0 && debit<AcctBalance)
			break;
		}
	AcctBalance=AcctBalance-debit;
	return AcctBalance;
}

void BankAccount::displayAcctBalance ()
{
   cout <<" " <<getAcctBalance () << " "<<endl;
}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include "BankAccount.h"

using namespace std;

int main()
{
	int number, amount;

	BankAccount account ( 238 ); //Instantiating the account. Initial balance=$238.

	cout<<"What do you want to do?\n" <<endl;
	cout<<"To credit to you account, please press 1;\n" <<"To debit from you account, please press 2;\n"<<endl;
	cin>>number;
	while (number!=1 && number !=2)
	{
		cout<<"Wrong number. Please try again:\n" <<endl;
		cout<<"To credit to you account, please press 1;\n" <<"To debit from you account, please press 2;\n"<<endl;
		cin>>number;
		if (number==1)
		{
			cout<<"Please enter amount:\n" <<endl;
			cin>>amount;
			cout<< "Your original balance is "<<account.displayAcctBalance()<<"!\n"<<endl;
			account.Credit(amount);
			cout<< "Your current balance is "<<account.displayAcctBalance()<<"!\n"<<endl;
			break;
		}
		else if (number==2)
		{
			cout<<"Please enter amount:\n" <<endl;
			cin>>amount;
			cout<< "Your original balance is "<<account.displayAcctBalance()<<"!\n"<<endl;
			account.Debit(amount);
			cout<< "Your current balance is "<<account.displayAcctBalance()<<"!\n"<<endl;
			break;
		}

	}
}
change account.displayAcctBalance()
to account.getBalance()

your display account balance method doesnt return anything cout can use.

if you wanted that code to work you would have to do it like

1
2
3
cout << "your balance is ";
account.display();
cout << "!\n" << endl;


also, dont put using namespace in every file, especially header files, this can cause naming collision in larger programs and is a very bad habit.

just put std:: in front of whatever it is you need
Dear Paoletti301,

It works like a charm! Thank you!!!!!


dont put using namespace in every file, ...and is a very bad habit.


This sounds like a serious problem. I did this only because Deitel encouraged readers to include "using namespace std" in the files so one doesn't have to type the pesky little "std::" every time! Duly noted.
Topic archived. No new replies allowed.