Please help with my if else.

Oct 25, 2013 at 11:47pm
My instructor gave us an assignment. I have finished it, and it meets all parameters, but my else is not working. It should be displaying an error message, but it just continues with the code. Could someone PLEASE help me out?? My program is due Sunday.
Thank you so much!!

#include <iostream>
using namespace std;

int main()
{

int duration;
char c;
char s;
double interest;
double ammount;
char account;


cout<<"Would you like to open a checking account or a saving account?"<<endl;
cout<<"Enter C for checking or S for saving."<<endl;
cin>>account;
cout<<"How much would you like to deposit?"<<endl;
cin>>ammount;
if (account=='c','C')

interest=ammount*.05;

else if (account=='s','S')

interest=ammount*.1;


else
{
cout<<"Wrong type of account! Try again!"<<endl;
;}

;{
int duration;
cout<<"How many months would you like to open your account for?"<<endl;
cin>>duration
;if (duration>=12&&ammount>=1000)

cout<<"Your balance is "<<ammount+interest<<endl;

else if (duration<12||ammount<1000)

cout<<"Your balance is "<<ammount<<endl;



;cin.get();
cin.get();

return 0;
}
}
Oct 26, 2013 at 1:07am
Why did you start a new thread?
You should focus on cleaning up your code and use code tags.
Before you ask for how much to deposit, validate if the the account is good.
Oct 26, 2013 at 1:51am
You have got a lot of stray semi-colons...

Anyway, when you write this: if (account=='c','C'), you've got two expressions in the parenthesis.
account == c produces a boolean, which is then used as one of the operands of the comma operator. The result of the comma is what your if-statement is testing, and in your case, it will always be true, so the else will never be run.
What you really want is two comparisons, like this if (account == 'c' || account == 'C')
Oct 26, 2013 at 2:03am
You really need to practice your code design dude, and here i think i found a fix for you

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
#include <iostream>
using namespace std;

int main()
{
	int duration;
	char c;
	char s;
	double interest;
	double ammount;
	char account;
	bool success;

	cout<<"Would you like to open a checking account or a saving account?"<<endl;
	cout<<"Enter C for checking or S for saving."<<endl;
	cin>>account;
	cout<<"How much would you like to deposit?"<<endl;
	cin>>ammount;
	do
	{
	if (account == 'c' || account == 'C')
	{
	interest = ammount*.05;
	success = true;
	}
	else if (account == 's' || account == 'S')
	{
	interest = ammount*.1;
	success = true;
	}
	else
	{
	cout<<"Wrong type of account! Try again!"<<endl;
	success = false;
	cin>>account;
	}
	} while ( ! success );

	cout<<"How many months would you like to open your account for?"<<endl;
	cin>>duration;

	if (duration >= 12 && ammount >= 1000)
	{
		cout<<"Your balance is "<<ammount+interest<<endl;
	}
	else if (duration < 12 || ammount < 1000)
	{
	cout<<"Your balance is " <<ammount<<endl;
	}
	cin.get();

	return 0;
	}


Always use the code format (<>) when you're posting your code.
Topic archived. No new replies allowed.