New to C++ help find mistake

Jun 10, 2011 at 8:53pm
The bold line is showing error. Illegal else without matching if. there may be other errors, but looking for this one for now. Thanks

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
int main()
{
	const int bfee_1=10;			//monthly charge
	const int bfee_2=15;			//addon for balance <$400
	const double fee_3=.10;		//check fee for <20 checks
	const double fee_4=.08;		//check fee for 21-39 checks
	const double fee_5=.06;		//check fee for 40-59 checks
	const double fee_6=.04;		//check fee for >=60 checks

	double AcctBal;				//Account Balance before any fees
	int checks;					//number of checks written

	double servfee_1=bfee_1+bfee_2+(checks*fee_3);	//total monthly fee for account balance under $400 and <20 checks written
	double servfee_2=bfee_1+bfee_2+(checks*fee_4);	//total monthly fee for account balance under $400 and 20-39 checks written
	double servfee_3=bfee_1+bfee_2+(checks*fee_5);	//total monthly fee for account balance under $400 and 40-59 checks written
	double servfee_4=bfee_1+bfee_2+(checks*fee_6);	//total monthly fee for account balance under $400 and >=60 checks written

	cout <<"Please enter you current account balance\n";
	cin >> AcctBal;

	if (AcctBal < 0)
		cout<<"Warning! You are overdrawn. Please Deposit money\n";
	
	else if (AcctBal < 400)	
		cout<<"Please entry number of checks written this month\n";
		cin>>checks;
		{
		if (checks<20)
			cout <<"Your service fee this month is"<<servfee_1<<"\n";
		else if (20<=checks && checks<39)
			cout <<"Your service fee this month is"<<servfee_2<<"\n";
		else if (40<=checks && checks<60)
			cout <<"Your service fee this month is"<<servfee_3<<"\n";
		else (checks>=60);
			cout <<"Your service fee this month is"<<servfee_4<<"\n";
		}
	
	else (AcctBal >= 400);
		cout<<"Please entry number of checks written this month\n";
		cin>>checks;
		{
		if (checks<20)
			cout<<"Your service fee this month is"<<servfee_1-bfee_2<<"\n";
		else if (20<=checks && checks<39)
			cout<<"Your service fee this month is"<<servfee_2-bfee_2<<"\n";
		else if (40<=checks && checks<60)
			cout<<"Your service fee this month is"<<servfee_3-bfee_2<<"\n";
		else (checks>=60);
			cout<<"Your service fee this month is"<<servfee_4-bfee_2<<"\n";
		}
	
	return 0;
}
Last edited on Jun 10, 2011 at 9:22pm
Jun 10, 2011 at 8:55pm
The if before it is missing an opening brace. And please use [code][/code] tags in the future.
Jun 10, 2011 at 9:10pm
If this is what you mean, I am still getting the same error. line 42...in bold.

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
#include <iostream>

using namespace std;

int main()
{
	const int bfee_1=10;			//monthly charge
	const int bfee_2=15;			//addon for balance <$400
	const double fee_3=.10;		//check fee for <20 checks
	const double fee_4=.08;		//check fee for 21-39 checks
	const double fee_5=.06;		//check fee for 40-59 checks
	const double fee_6=.04;		//check fee for >=60 checks

	double AcctBal;				//Account Balance before any fees
	int checks;					//number of checks written

	double servfee_1=bfee_1+bfee_2+(checks*fee_3);	//total monthly fee for account balance under $400 and <20 checks written
	double servfee_2=bfee_1+bfee_2+(checks*fee_4);	//total monthly fee for account balance under $400 and 20-39 checks written
	double servfee_3=bfee_1+bfee_2+(checks*fee_5);	//total monthly fee for account balance under $400 and 40-59 checks written
	double servfee_4=bfee_1+bfee_2+(checks*fee_6);	//total monthly fee for account balance under $400 and >=60 checks written

	cout <<"Please enter you current account balance\n";
	cin >> AcctBal;
	{
	if (AcctBal < 0)
		cout<<"Warning! You are overdrawn. Please Deposit money\n";
	
	else if (AcctBal < 400)	
		cout<<"Please entry number of checks written this month\n";
		cin>>checks;
		{
		if (checks<20)
			cout <<"Your service fee this month is"<<servfee_1<<"\n";
		else if (20<=checks && checks<39)
			cout <<"Your service fee this month is"<<servfee_2<<"\n";
		else if (40<=checks && checks<60)
			cout <<"Your service fee this month is"<<servfee_3<<"\n";
		else (checks>=60);
			cout <<"Your service fee this month is"<<servfee_4<<"\n";
		}
	
	else (AcctBal>=400);
		cout<<"Please entry number of checks written this month\n";
		cin>>checks;
		{
		if (checks<20)
			cout<<"Your service fee this month is"<<servfee_1-bfee_2<<"\n";
		else if (20<=checks && checks<39)
			cout<<"Your service fee this month is"<<servfee_2-bfee_2<<"\n";
		else if (40<=checks && checks<60)
			cout<<"Your service fee this month is"<<servfee_3-bfee_2<<"\n";
		else (checks>=60);
			cout<<"Your service fee this month is"<<servfee_4-bfee_2<<"\n";
		}
	}
	return 0;
}


Last edited on Jun 10, 2011 at 9:11pm
Jun 10, 2011 at 9:49pm
Line 28 will only do line 29, and then 30 to the end will run every time, regardless. You need to use the curly braces to force the else-if to do everything from lines 29 to 41. The else on line 42 is illegal because currently, it is right after another else statement and not an if/else-if.
Jun 11, 2011 at 12:51am
I am trying again from start. I get bool error but it starts to run anyway. If I enter a neg number it still continues instead of bumping to end?? I also have other looping errors. Please help.
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
#include <iostream>

using namespace std;

int main()
{
	const int bfee_1=10;			//monthly charge
	const int bfee_2=15;			//addon for balance <$400
	double fee_3=.10;		//check fee for <20 checks
	double fee_4=.08;		//check fee for 21-39 checks
	double fee_5=.06;		//check fee for 40-59 checks
	double fee_6=.04;		//check fee for >=60 checks

	double AcctBal;				//Account Balance before any fees
	int checks;					//number of checks written

	double servfee_1=bfee_1+bfee_2+(checks*fee_3);	//total monthly fee for account balance under $400 and <20 checks written
	double servfee_2=bfee_1+bfee_2+(checks*fee_4);	//total monthly fee for account balance under $400 and 20-39 checks written
	double servfee_3=bfee_1+bfee_2+(checks*fee_5);	//total monthly fee for account balance under $400 and 40-59 checks written
	double servfee_4=bfee_1+bfee_2+(checks*fee_6);	//total monthly fee for account balance under $400 and >=60 checks written

	cout <<"Please enter you current account balance\n";
	cin >> AcctBal;
	
	if (AcctBal < 0)
	{
		cout<<"Warning! You are overdrawn. Please Deposit money\n";
		
	}
	if (0>=AcctBal <400)
	{
		cout<<"Please entry number of checks written this month\n";
		cin>>checks;
	
		if (checks<20)
			cout <<"Your service fee this month is"<<servfee_1<<"\n";
		else if (20<=checks<39)
			cout <<"Your service fee this month is"<<servfee_2<<"\n";
		else if (40<=checks<60)
			cout <<"Your service fee this month is"<<servfee_3<<"\n";
		else (checks>=60);
			cout <<"Your service fee this month is"<<servfee_4<<"\n";
	}
	
	if (AcctBal >= 400)
	{
		cout<<"Please entry number of checks written this month\n";
		cin>>checks;
		
		if (checks<20)
			cout<<"Your service fee this month is"<<servfee_1-bfee_2<<"\n";
		else if (20<=checks<39)
			cout<<"Your service fee this month is"<<servfee_2-bfee_2<<"\n";
		else if (40<=checks<60)
			cout<<"Your service fee this month is"<<servfee_3-bfee_2<<"\n";
		else (checks>=60);
			cout<<"Your service fee this month is"<<servfee_4-bfee_2<<"\n";
		}
	
	return 0;
}
Jun 11, 2011 at 1:55am
Look at line 30. It doesn't do what you think it does, which is why you have an error. The expression 0>=AcctBal <400 first checks if 0 is greater or equal to AcctBal; this operation returns a boolean (bool). Then, you try to see if that boolean is less than 400...wait what? How can a boolean, being true or false, be less than 400? It can't, which is why you have an error from the compiler. I think what you meant to do was this:
if(AcctBal <= 0 && AcctBal < 400)

However, that is clearly not what you wanted...I think you really wanted this:
if(AcctBal >= 0 && AcctBal < 400)
Jun 11, 2011 at 2:27am
yes, so many typos to see. I meant (0<=AcctBal<400) but i should use && I suppose. Thank you.
Jun 11, 2011 at 3:36am
I have made some progress but it says variable checks not initialized. Isn't it initialized in lines 33 and 48? if I set variable checks=0 it doesnt calculate properly
Jun 11, 2011 at 4:57am
You are using it on like 17-20 before it has been initialized.
Jun 13, 2011 at 2:36pm
So move lines 32 and 33 above line 24?
Jun 13, 2011 at 3:50pm
Actually, the other way around. Do lines 17-20 both on line 34 AND line 49.
Jun 15, 2011 at 4:12pm
Thank you!!
Topic archived. No new replies allowed.