Program doesn't do what I thought I told it to...

So when my code runs it doesn't do the operations I thought I told it to do... did I screw up my if, else if, while statements? Or am I just missing something.... Any help would be great thanx
:)



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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
#include <iostream>
#include <windows.h>

using std::cout;
using std:: cin;
using std:: endl;
using namespace std;

class BankAccount

{
public:
	BankAccount(void); // Base Class
	int getAccountNum(void);
	double getAccountBalance(void);// return balance
	void depositMoney(double depositAmount);// Deposit money
	bool withdrawMoney(double withdrawAmount); // Withdraw money
	void setAccountNum(int newAccountNum);// Set up account
	int newAccountNum;
	double accountBalance;


protected:
	int accountNum;


};
//////////////////////////////////////////////////////////////////////
class CheckingAccount :public BankAccount
{
public:
	CheckingAccount(void);
	bool withdrawMoney(double);

private:
	int transactionCount;
};
//////////////////////////////////////////////////////
class SavingsAccount : public BankAccount
{
public:
	SavingsAccount(void);

	double getAccountBalance(void);
	void SetInterestRate(double); // set daily interest rate

private:
	double InterestRate; // daily interest
	int numberofDays; // Days since last transaction
	double earnedInterest;// Interest since last transtion

public:
	double getInterestEarned(void); // earned interest
	int getNumberofDays(void);
	int getAccountBalance(double);
};
//////////////////////////////////////////////////////////////

BankAccount::BankAccount(void)
{
	accountNum = 0;
	accountBalance = 0.0;
}

void BankAccount::setAccountNum (int newAccountNum)
{
	accountNum= newAccountNum;
}

int BankAccount::getAccountNum(void)
{
	return accountNum;
}

double BankAccount::getAccountBalance(void)
{
	return accountBalance;
}

void BankAccount::depositMoney(double depositAmount)
{
	accountBalance= depositAmount + accountBalance;
}
bool BankAccount::withdrawMoney(double withdrawAmount)
{
	if
		(accountBalance - withdrawAmount < 0)
		return false;

	else
	{
		(accountBalance = withdrawAmount- accountBalance);
		return true;
	}
}

//////////////////////////////////////////////////////////////
CheckingAccount ::CheckingAccount (void)
{
	accountBalance =100.00;
}

/////////////////////////////////////////////////////////////////////
SavingsAccount :: SavingsAccount (void)
{
	accountBalance= 100.00;
}

double SavingsAccount::getAccountBalance()
{
	numberofDays = rand()%8;
	earnedInterest = accountBalance * (numberofDays * InterestRate/(100*365));
	return accountBalance+ earnedInterest;
}
void SavingsAccount::SetInterestRate(double newInterestRate)
{
	InterestRate = newInterestRate;
}
double SavingsAccount::getInterestEarned(void)
{
	return earnedInterest;
}
int SavingsAccount::getNumberofDays(void)
{
	return numberofDays;
}

bool CheckingAccount::withdrawMoney( double withdrawAmount)
{
	transactionCount++;
	if (transactionCount >3)

	{
		if
			(accountBalance - withdrawAmount - 0.5 < 0)
			cout << "insuficent Funds";
		return false;

		while
			(accountBalance = withdrawAmount + 0.5)

			cout << "Fifty cent fee included for more than three transactions";
		cout << endl;
		return true;
	}

	if (accountBalance-withdrawAmount <0)
		return false;
	else
		(accountBalance = accountBalance - withdrawAmount);
	return true;
}

////////////////////////////////////////////////////////////////////////////////

void main(void)
{
	char Choice;
	int newAccountNum;
	double depositAmount;
	double withdrawAmount;
	double newInterestRate= 0;
	int transactionCount=0;
	int randNumber;

	randNumber = rand();
	srand(GetTickCount());

	BankAccount accountBank;
	CheckingAccount accountChecking;
	SavingsAccount accountSavings;


	cout<< "Welcome!"<< "Let's start by creating your Checking account";
	cout << "Enter a five digit number to create your checking account number ";
	cin >> newAccountNum;

	cout << "A new checking account was created.";
	cout << endl;

	cout << "Your Checking account Balance is $" << accountChecking.getAccountBalance();
	cout << endl << endl;

	cout<< "Would you like to make a Withdraw or Deposit?"<< "Enter 'W' for withdraw or 'D' for deposit";
	cin >> Choice;
	do
	{

		if (Choice == 'W'|| Choice == 'w') 
		{

			cout<< "How much would you like to withdraw?";
			cin >> withdrawAmount;

			cout << "New Checking account balance  is $" << accountChecking.withdrawMoney(withdrawAmount);
			cout<< endl;
			cout <<"What would you like to do now?" << "Enter 'D' for deposit and 'W' for Withdrawal or 'S' to set up Savings Account" ;
						cin >> Choice;

		}
		else if (Choice='D' || Choice == 'd')
		{
			cout<< "How much would you like to Deposit?";
			cin >> depositAmount;

			cout<< "Your new Account Balance is:   "; accountChecking.depositMoney( depositAmount);
			cout << endl;

			cout << "Would you like to make another Transaction";
			cout << "Enter 'D' for Deposit and 'W' for Withdrawal or 'S' to create a Savings Account" ;
			cin >> Choice;
			
		}
	}
while (Choice == 'S' || Choice == 's');
		{
				cout << "Lets set up an Account Number for your Savings Account!";
				cout<< endl;

				cout << "Enter a five digit Number for a new savings account : ";
				cin >> newAccountNum;

				cout << "A savings account has been created";
				cout  << endl;
				
				cout << "Savings account balance is $" << accountSavings.getAccountBalance();
				cout << endl;

				cout << "Total interest earned: ";
				cout << accountSavings.getInterestEarned();
				cout << " over " ;
				cout << accountSavings.getNumberofDays();
				cout << " days" ;
				cout << endl;


				cout << "Total interest earned: " << accountSavings.getInterestEarned() << " over" << accountSavings.getNumberofDays() << " days";
				cout << endl;
				cout<< endl;

				cout <<"What would you like to do now?" << "Enter 'D' for deposit and 'W' for Withdrawal or 'E' to Exit" ;
						cin >> Choice;
		}
				if (Choice == 'W'|| Choice == 'w' )
					{

						cout<< "How much would you like to withdraw?";
						cin >> withdrawAmount;

						cout << "New Checking account balance  is $" << accountSavings.withdrawMoney(withdrawAmount);
						cout<< endl;
						cout <<"What would you like to do now?" << "Enter 'D' for deposit and 'W' for Withdrawal or 'E' to Exit" ;
						cin >> Choice;
						

					}
				else if (Choice=='D' || Choice == 'd')

					{

						cout<< "How much would you like to Deposit?";
						cin >> depositAmount;

						cout<< "Your new Account Balance is:   "; accountSavings.depositMoney( depositAmount);
						cout << endl;
						cout <<"What would you like to do now?" << "Enter 'D' for deposit and 'W' for Withdrawal or 'E' to Exit" ;
						cin >> Choice;
						
					}
					
					 while (Choice== 'E' || Choice== 'e');
					 {
					
						cout<< "have a nice day... Good Bye " ;
					 }

						exit(0);

		}
		
Lines 139, 140
139
140
		while
			(accountBalance = withdrawAmount + 0.5)

There is an = which doesn't look quite right. (not sure why it's split over two lines - it gives the impression there is something omitted).

Line 201:
 
		else if (Choice='D' || Choice == 'd')

there is an = instead of ==
I didnt catch the one= and not two, thank you, probably from looking at it so much... lol

Im not quite sure if i know what you mean by line 139 and 140, should it be right after the while, will the code be read different if its not, i dont think i left anything out?
the while loop Chervil pointed out doesn't make sense. Either it runs infinite or not at all. What are you trying to do there?

By the way: to avoid that =/== disaster you may write the clause like so (constant values first):

if ('D' = Choice || 'd' == Choice ) // <- compiler error
Last edited on
You also have a few spots (lines 215, 271) where you have a stray semi-colon after the loop condition.
Coder777: I want those if... else if statements to loop(I think) I wanted that when the user had a choice they can pick from any choice. I thought I would use (if, else, else) but visual basic kept giving me an error that I can't use a else statement without a previous if statement... Which I had (Line 244,189). It also told me that my third else it expected a while, so I changed it even though I didn't want to, to get rid of the errors. Ps. Thank you for the tip.

What should I do to get visual basic to let me change that?


Cire: Once again my visual basic was giving me an error telling me that it expected a ';' in that spot so when I put I there the error went away...

... I'm sorry I am not very good at this/troubleshooting my faults...

Visual Basic is a completely separate programming language. I think you are referring to the C++ compiler of Visual Studio.
Yes yes I am sorry I long day yesterday, But I still don't understand why it tells me weird things like that...
Don't worry about the VB thing, It distracted me because sometimes people are attempting to translate code from visual basic into c++ (or from c++ into python ...) and that was what I thought you meant at first.

Well, I know the code is fairly long, but it's difficult to give a precise answer without seeing either the latest version of the code, and/or the exact error message as output from the compiler.

It sounds as though you have made some changes in order to keep the compiler happy (which is a good idea), but without understanding exactly why. Perhaps you'd like to post the latest version of the code and state which are the areas giving you concern.
Okay so I went back to how I had the code before I got those compile errors about ("I thought I would use (if, else, else) but visual basic kept giving me an error that I can't use a else statement without a previous if statement... Which I had (Line 244,189). It also told me that my third else it expected a while, so I changed it even though I didn't want to"). I also changed to the constant values first idea... Now I am getting new errors completly. I just thing Visual Studio hates me

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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include <iostream>
#incluse <windows.h>


using std::cout;
using std:: cin;
using std:: endl;
using namespace std;

class BankAccount

{
public:
BankAccount(void); // Base Class
int getAccountNum(void);
double getAccountBalance(void);// return balance
void depositMoney(double depositAmount);// Deposit money
bool withdrawMoney(double withdrawAmount); // Withdraw money
void setAccountNum(int newAccountNum);// Set up account
int newAccountNum;
double accountBalance;

protected:
int accountNum;
};
//////////////////////////////////////////////////////////////////////
class CheckingAccount :public BankAccount
{
public:
CheckingAccount(void);
bool withdrawMoney(double);

private:
int transactionCount;
};
//////////////////////////////////////////////////////
class SavingsAccount : public BankAccount
{
public:
SavingsAccount(void);

double getAccountBalance(void);
void SetInterestRate(double); // set daily interest rate

private:
double InterestRate; // daily interest
int numberofDays; // Days since last transaction
double earnedInterest;// Interest since last transtion

public:
double getInterestEarned(void); // earned interest
int getNumberofDays(void);
int getAccountBalance(double);
};
//////////////////////////////////////////////////////////////

BankAccount::BankAccount(void)
{
accountNum = 0;
accountBalance = 0.0;
}
void BankAccount::setAccountNum (int newAccountNum)
{
accountNum= newAccountNum;
}
int BankAccount::getAccountNum(void)
{
return accountNum;
}
double BankAccount::getAccountBalance(void)
{
return accountBalance;
}
void BankAccount::depositMoney(double depositAmount)
{
accountBalance= depositAmount + accountBalance;
}
bool BankAccount::withdrawMoney(double withdrawAmount)
{
if
(accountBalance - withdrawAmount < 0)
return false;

else
{
(accountBalance = withdrawAmount- accountBalance);
return true;
}
}
//////////////////////////////////////////////////////////////
CheckingAccount ::CheckingAccount (void)
{
accountBalance =100.00;
}
/////////////////////////////////////////////////////////////////////
SavingsAccount :: SavingsAccount (void)
{
accountBalance= 100.00;
}
double SavingsAccount::getAccountBalance()
{
numberofDays = rand()%8;
earnedInterest = accountBalance * (numberofDays * InterestRate/(100*365));
return accountBalance+ earnedInterest;
}
void SavingsAccount::SetInterestRate(double newInterestRate)
{
InterestRate = newInterestRate;
}
double SavingsAccount::getInterestEarned(void)
{
return earnedInterest;
}
int SavingsAccount::getNumberofDays(void)
{
return numberofDays;
}
bool CheckingAccount::withdrawMoney( double withdrawAmount)
{
transactionCount++;
if (transactionCount >3)
{
if
(accountBalance - withdrawAmount - 0.5 < 0)
cout << "insuficent Funds";
return false;
}


else
{
(accountBalance = withdrawAmount + 0.5);
cout << "Fifty cent fee included for more than three transactions";
cout << endl;
return true;
}
if (accountBalance-withdrawAmount <0)
return false;
else
{
(accountBalance = accountBalance - withdrawAmount);
return true;
}

////////////////////////////////////////////////////////////////////////////////

void main(void)
{

char Choice;
int newAccountNum;
double depositAmount;
double withdrawAmount;
double newInterestRate= 0;
int transactionCount=0;
int randNumber;
randNumber = rand();
srand(GetTickCount());
BankAccount accountBank;
CheckingAccount accountChecking;
SavingsAccount accountSavings;
{
cout<< "Welcome!"<< "Let's start by creating your Checking account";
cout << "Enter a five digit number to create your checking account number ";
cin >> newAccountNum;
cout << "A new checking account was created.";
cout << endl;
cout << "Your Checking account Balance is $" << accountChecking.getAccountBalance();
cout << endl << endl;
cout<< "Would you like to make a Withdraw or Deposit?"<< "Enter 'W' for withdraw or 'D' for deposit";
cin >> Choice;
do
{
if ( 'W'== Choice||  'w'== Choice)
{

cout<< "How much would you like to withdraw?";
cin >> withdrawAmount;
cout << "New Checking account balance  is $" << accountChecking.withdrawMoney(withdrawAmount);
cout<< endl;
cout <<"What would you like to do now?" << "Enter 'D' for deposit and 'W' for Withdrawal or 'S' to set up Savings Account" ;
cin >> Choice;
}
else
{
(Choice='D' || Choice == 'd');
cout<< "How much would you like to Deposit?";
cin >> depositAmount;
cout<< "Your new Account Balance is:   "; accountChecking.depositMoney( depositAmount);
cout << endl;
cout << "Would you like to make another Transaction";
cout << "Enter 'D' for Deposit and 'W' for Withdrawal or 'S' to create a Savings Account" ;
cin >> Choice;
}
else
{
('S'==Choice || 's'== Choice);

cout << "Lets set up an Account Number for your Savings Account!";
cout<< endl;
cout << "Enter a five digit Number for a new savings account : ";
cin >> newAccountNum;
cout << "A savings account has been created";
cout  << endl;
cout << "Savings account balance is $" << accountSavings.getAccountBalance();
cout << endl;
cout << "Total interest earned: ";
cout << accountSavings.getInterestEarned();
cout << " over " ;
cout << accountSavings.getNumberofDays();
cout << " days" ;
cout << endl;
cout << "Total interest earned: " << accountSavings.getInterestEarned() << " over" << accountSavings.getNumberofDays() << " days";
cout << endl;
cout<< endl;
cout <<"What would you like to do now?" << "Enter 'D' for deposit and 'W' for Withdrawal or 'E' to Exit" ;
cin >> Choice;
}
do
if ( 'W'==Choice|| 'w'==Choice )
{
cout<< "How much would you like to withdraw?";
cin >> withdrawAmount;
cout << "New Checking account balance  is $" << accountSavings.withdrawMoney(withdrawAmount);
cout<< endl;
cout <<"What would you like to do now?" << "Enter 'D' for deposit and 'W' for Withdrawal or 'E' to Exit" ;
cin >> Choice;
}
else
{
('D'==Choice ||  'd'==Choice);
cout<< "How much would you like to Deposit?";
cin >> depositAmount;
cout<< "Your new Account Balance is:   "; accountSavings.depositMoney( depositAmount);
cout << endl;
cout <<"What would you like to do now?" << "Enter 'D' for deposit and 'W' for Withdrawal or 'E' to Exit" ;
cin >> Choice;
}
else
{
( 'E'==Choice ||  'e'==Choice);


cout<< "have a nice day... Good Bye " ;
}
exit(0);
}		}





... Oh and it unformatted it, I'll fix that later though..

Errors:

Line 148: error: a function-definition is not allowed here before '{' token
compilation terminated due to -Wfatal-errors.


Have not seen this before and had the program working with it that exact same way?
Your if statement chains are off.

You are saying If and then have 3 else statements..

You need to make them 'else if' and save your 'else' for default in the chance that your inputs or conditions are false for every other possible statement
I changed them to else if but I still get this error... Thank you i musat have missed that as well when switching it back to how it was when I got those errors, but it's werid I'm not getting those same errors any more, just this one?


Line 148: error: a function-definition is not allowed here before '{' token
compilation terminated due to -Wfatal-errors.
Topic archived. No new replies allowed.