Card Eligibility

Hi I would need help on the stated below coding.

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


int main()
{
char cont='Y';
int count=0;
float sum = 0.0;
float amount = 0;
int age = -1;

while (cont != 's' && cont != 'S')
{
	while (age < 20)
	{
		cout << "Enter your age: ";
		cin >> age;
		if (age < 20)
		   cout << "Your age is not eligible for a credit card, please re-enter..." << endl;
	}
		while (amount < 30000)
	{
	    cout << "Enter your annual amount: ";
	    cin >> amount;
	    if (amount < 30000)
		cout << "Your entered annual amount is too low for a credit card, please re-enter...";
		else if (amount > 60001 || age < 19 || age < 56 )
		cout << "Your are eligible for Card A.";
		else if (amount > 60000 || age > 55 )
		cout << "Your are eligible for Card B.";
		else if (amount > 30000 || amount < 60001 || age > 20 || age < 55)
		cout << "Your are eligible for Card C.";
		else if (amount > 30000 || amount < 60001 || age > 55)
		cout << "Your are eligible for Card D.";

	    else
		sum = sum + amount;
	}
	cout << "Enter 's' or 'S' to terminate, any other letter to continue ...";
	cin >> cont;	
	age = 0;
	
}
cout << "Total donations : " << sum << endl;
cout << "Average donation per person : " << sum/count << endl;
cout << "Thank you...";
return 0;
cin.ignore();
cin.ignore();
}
Last edited on
hiya,
Basically your logic is completely wrong.
Example:
say you enter you age as 30.
then you say amount is 30,001.

Look at the first condition you assess:
else if (amount > 60001 || age < 19 || age < 56 )

because you are using or operators it only takes one of the above three conditions to evaluate to true for the whole if statement to evaluate to true. So 30,001 is NOT greater than 60,001, so you're expecting it to move onto the next if. BUT it also says evaluate to true if you age is less that 56, which it is, and therefore you get card A.

by the way these 2 together make no sense:
age < 19 || age < 56
because if a number is less than 19, it is of course always less that 56.

You've already tested for the minimum age of 20, so you don't event need the <19 test.
Last edited on
oh no.. so I have to use while, if and else?
@evgeric. You have mixed up the OR and AND. Your if statements shoud look like this -

1
2
3
4
5
6
7
8
9
10
11
if (amount < 30000)
				cout << "Your entered annual amount is too low for a credit card, please re-enter...";
			else if (amount > 60001 && age < 19 && age < 56)
				cout << "Your are eligible for Card A.";
			else if (amount > 60000 && age > 55)
				cout << "Your are eligible for Card B.";
			else if (amount > 30000 && amount < 60001 && age > 20 && age < 55)
				cout << "Your are eligible for Card C.";
			else if (amount > 30000 && amount < 60001 && age > 55)
				cout << "Your are eligible for Card D.";


You should be using && for AND instead of || for OR.
Think about it. for a Card C. you write that the user should be older than 20 OR younger than 55. Thats pretty much every age ever. 10 is younger than 55, so that contradicts the age > 20.

Thats why you want to use the &&.
Last edited on
Nah, i didnt say that.
As a first pass, do it in a convoluted way, just to get it working.
by that I mean look at your requirements:

1
2
3
4
Greater than $60000, Age Group: 20 to 55 = Card A
Greater than $60000, Age Group: Above 55 = Card B
Between $30000 and $60000, Age Group: 20 to 55 = Card C
Between $30000 and $60000, Age Group: Above 55 = Card D


so you could do this:

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
...
const int UPPER_BOUND = 60000;
const int LOWER_BOUND = 30000;
const int UPPER_AGE = 55;
...
while (amount < 30000)
		{
			cout << "Enter your annual amount: ";
			cin >> amount;

			// Just test the amounts first
			if (amount > UPPER_BOUND)
			{
				if (age <= UPPER_AGE)
				{
					// CARD A
				}
				else
				{
					// CARD B
				}
			}
			else if (amount >= LOWER_BOUND && amount <= UPPER_BOUND)
			{
				if (age <= UPPER_AGE)
				{
					// CARD C
				}
				else
				{
					// CARD D
				}

			}
			else
			{
				// report error/start again etc
			}
		}


it's not optimised or anything, but might give you a nice warm feeling if it works.
Last edited on
thanks mutexe. I managed to get it working. Thanks a lot.
Topic archived. No new replies allowed.