Only getting one answer

no matter what answer I enter, the answer keeps popping up as "you owe $250 dollars."

#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
int month, day;

cout << "hello, what was the month and day you intended to pay?" << endl;

cin >> month;
cin >> day;

if (month > 1 && day > 1 || month < 4 && day < 20)
{
cout << "you owe $250 dollars." << endl;
}
else if (month > 4 && day > 21 || month < 6 && day < 21)
{
cout << "you owe $300 dollars!" << endl;
}
else if (month > 6 && day > 22 || month < 7 && day < 7)
{
cout << "you owe $350 dolalrs." << endl;
}
else if (month > 7 && day > 7)
{
cout << "you have missed the deadline!" << endl;
}

system("PAUSE");
return EXIT_SUCCESS;
}
If you enter a value for the month and day that is greater than 1, the if statement will always execute. To fix this, you should start the if-else conditions in decending order like so:

1
2
3
4
5
6
7
8
if (month > 7 && day > 7)
        cout << "you have missed the deadline!\n";
    else if (month > 6 && day > 22 || month < 7 && day < 7)
        cout << "you owe $350 dollars.\n";
    else if (month > 4 && day > 21 || month < 6 && day < 21)
        cout << "you owe $300 dollars!\n";
    else if (month > 1 && day > 1 || month < 4 && day < 20)
        cout << "you owe $250 dollars.\n";
okay that fixes it a little, but now the missed deadline won't appear, just 250 dollars owed.
A little revision:

1
2
3
4
5
6
7
8
if (month > 7 && day > 7)
        cout << "you have missed the deadline!\n";
    else if (month > 6 && day > 22)
        cout << "you owe $350 dollars.\n";
    else if (month > 4 && day > 21)
        cout << "you owe $300 dollars!\n";
    else if (month > 1 && day > 1)
        cout << "you owe $250 dollars.\n";
You need to take into account that, for example, January 31 is "less than" April 1. So something like month < 4 && day < 20 Will fail for days larger than 20, even when the month is less than 4. So I think you want something like:
1
2
3
4
5
6
7
8
9
    if (month < 3 || month == 3 && day < 20) { // before March 20
	cout << "you owe $250 dollars." << endl;
    } else if (month < 5 || month == 5 && day < 21) { // before May 21
	cout << "you owe $300 dollars!" << endl;
    } else if (month < 6 || month == 6 && day < 7) { // before June 7
	cout << "you owe $350 dolalrs." << endl;
    } else {
	cout << "you have missed the deadline!" << endl;
    }

Sometimes it's better to use data than code. Here is another way that uses an array to encode the deadlines and amounts. The advantage is that it's much easier to add or change a deadline:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    struct Deadline {
	int month, day, amount;
    };
    Deadline deadlines[] = {
	{ 3, 20, 250 },
	{ 5, 21, 300 },
	{ 6, 7, 350 }
    };

    int i;
    for (i=0; i < sizeof(deadlines) / sizeof(deadlines[0]); ++i) {
	if (month < deadlines[i].month ||
	    month == deadlines[i].month && day < deadlines[i].day) {
	    cout << "you own $" << deadlines[i].amount << " dollars.\n";
	    break;
	}
    }
    if (i == sizeof(deadlines) / sizeof(deadlines[0])) {
	cout << "you have missed the deadline!" << endl;
    }
Topic archived. No new replies allowed.