If statements not executing correctly

My first If statement always executes in the studentFeesCalc(char typeOfStudent, int hoursTaken) function, even when I enter a number that is not true in the statement

if (( MIN_HOURS <= hoursTaken <= MAX_HOURS1) && ((typeOfStudent == 'U') || (typeOfStudent == 'u')))

Could someone point out why this is happening? Thank you all, this forum has been a great help as I learn C++.

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

const string TYPE_U = "Undergraduate";
const string TYPE_G = "Graduate";
const string TYPE_N = "Non-Degree Seeking";
const int COST_PER_CREDIT = 465;
const double NON_DEGREE_FEE = 100;
const double RATE1_3 = .03;
const double UNDER_RATE4_9 = .028;
const double UNDER_RATE10_14 = .025;
const double UNDER_RATE15_18 = .022;
const double RATE_OVER = .019;
const double GRAD_RATE4_9 = .027;
const double GRAD_RATE10_14 = .024;
const double GRAD_RATE15_18 = .021;
const double MIN_HOURS = 1;
const double MAX_HOURS1 = 3;
const double MIN_HOURS2 = 4;
const double MAX_HOURS2 = 9;
const double MIN_HOURS3 = 10;
const double MAX_HOURS3 = 14;
const double MIN_HOURS4 = 15;
const double MAX_HOURS4 = 18;
const double MAX_HOURS5 = 18;

double studentFeesCalc(char typeOfStudent, int hoursTaken);
int main()
{
	char studentType;
	int numHours;
	string displayStudentType;
	
	cout << "Student Fees Calculation Program" << endl << endl;
	cout << "Student types choices:" << endl;
	cout << "     U - undergraduate" << endl;
	cout << "     G - graduate" << endl;
	cout << "     N - non-degree seeking" << endl << endl;
	cout << "Enter student type: " ;
	cin >> studentType;
	cout << endl << endl;
	
	cout << "Enter number of credit hours enrolled in: " ;
	cin >> numHours;
	cout << endl << endl;
	
	if ((studentType == 'U') || (studentType == 'u'))
		{
			displayStudentType = TYPE_U;
		}	
	if ((studentType == 'G') || (studentType == 'g'))
		{
			displayStudentType = TYPE_G;
		}
	if ((studentType == 'N') || (studentType == 'n'))
		{
			displayStudentType = TYPE_N;
		}	
		
	
	
	cout << "Student Fees Summary" << endl << endl;
	cout << "     Student Type:" << setw(35) << displayStudentType << endl;
	cout << "     Credit Hours:" << setw(30) << numHours << endl;
	
	studentFeesCalc(studentType, numHours);


	
 cout << endl << endl;
 system ("PAUSE");
 
 return 0;
}
 
double studentFeesCalc(char typeOfStudent, int hoursTaken)
{
	double totalFees;

			if (( MIN_HOURS <= hoursTaken <= MAX_HOURS1) && ((typeOfStudent == 'U') || (typeOfStudent == 'u')))
		{
					totalFees = ((COST_PER_CREDIT * hoursTaken) * RATE1_3);
					cout << "     Rate Used:" << setw(30) << RATE1_3 << endl << endl;
					cout << "     Student Fees:" << setw(31) << totalFees << endl;
		}	
		
		
			else if ((MIN_HOURS2 <= hoursTaken <= MAX_HOURS2 ) && ((typeOfStudent == 'U') || (typeOfStudent == 'u')))
		{	
			      	totalFees = ((COST_PER_CREDIT * hoursTaken) * UNDER_RATE4_9);
					cout << "     Rate Used:" << setw(30) << UNDER_RATE4_9 << endl << endl;
					cout << "     Student Fees:" << setw(31) << totalFees << endl;
		}
		
		
			else if (( MIN_HOURS3 <= hoursTaken <= MAX_HOURS3) && ((typeOfStudent == 'U') || (typeOfStudent == 'u')))
		{
			     	totalFees = ((COST_PER_CREDIT * hoursTaken) * UNDER_RATE10_14);
					cout << "     Rate Used:" << setw(30) << UNDER_RATE10_14 << endl << endl;
					cout << "     Student Fees:" << setw(31) << totalFees << endl;
		}
		
		
			else if (( MIN_HOURS4 <= hoursTaken <= MAX_HOURS4 ) && ((typeOfStudent == 'U') || (typeOfStudent == 'u')))
		{
                    totalFees = ((COST_PER_CREDIT * hoursTaken) * UNDER_RATE15_18);
					cout << "     Rate Used:" << setw(30) << UNDER_RATE15_18 << endl << endl;
					cout << "     Student Fees:" << setw(31) << totalFees << endl;
		}
		
		
			else if (( MAX_HOURS5 < hoursTaken ) && ((typeOfStudent == 'U') || (typeOfStudent == 'u')))
		{
			     	totalFees = ((COST_PER_CREDIT * hoursTaken) * RATE_OVER);
					cout << "     Rate Used:" << setw(30) << RATE_OVER << endl << endl;
					cout << "     Student Fees:" << setw(31) << totalFees << endl;
		}

	


		
			else if (( MIN_HOURS <= hoursTaken <= MAX_HOURS1 ) && ((typeOfStudent == 'G') || (typeOfStudent == 'g')))
		{
                    totalFees = ((COST_PER_CREDIT * hoursTaken) * RATE1_3);
  			        cout << "     Rate Used:" << setw(30) << RATE1_3 << endl << endl;
                    cout << "     Student Fees:" << setw(31) << totalFees << endl;
		}
		
		
			else if (( MIN_HOURS2 <= hoursTaken <= MAX_HOURS2 ) && ((typeOfStudent == 'G') || (typeOfStudent == 'g')))
		{
                totalFees = ((COST_PER_CREDIT * hoursTaken) * GRAD_RATE4_9);
                cout << "     Rate Used:" << setw(30) << GRAD_RATE4_9 << endl << endl;
		        cout << "     Student Fees:" << setw(31) << totalFees << endl;
		}
		
	
			else if (( MIN_HOURS3 <= hoursTaken <= MAX_HOURS3 ) && ((typeOfStudent == 'G') || (typeOfStudent == 'g')))
		{
                totalFees = ((COST_PER_CREDIT * hoursTaken) * GRAD_RATE10_14);
                cout << "     Rate Used:" << setw(30) << GRAD_RATE10_14 << endl << endl;
		        cout << "     Student Fees:" << setw(31) << totalFees << endl;
		}
		
		
			else if (( MIN_HOURS4 <= hoursTaken <= MAX_HOURS4 ) && ((typeOfStudent == 'G') || (typeOfStudent == 'g')))
		{
               totalFees = ((COST_PER_CREDIT * hoursTaken) * GRAD_RATE15_18);
               cout << "     Rate Used:" << setw(30) << GRAD_RATE15_18 << endl << endl;
		       cout << "     Student Fees:" << setw(31) << totalFees << endl;
		}
		
		
			else if (( MAX_HOURS5 < hoursTaken ) && ((typeOfStudent == 'G') || (typeOfStudent == 'g')))
		{
			   	totalFees = ((COST_PER_CREDIT * hoursTaken) * RATE_OVER);
		        cout << "     Rate Used:" << setw(30) << RATE_OVER << endl << endl;
				cout << "     Student Fees:" << setw(31) << totalFees << endl;
		}
	
	
	
        	if ((typeOfStudent == 'n') || (typeOfStudent == 'N'))
	     {
     	       cout << fixed;
		       cout << setprecision(2);
		       cout << "     Student Fees:" << setw(25) << NON_DEGREE_FEE << endl;
         }	
}       
A common mistake, each comparison needs be be written out.

if (( MIN_HOURS <= hoursTaken <= MAX_HOURS1)

Needs to be:

if (( MIN_HOURS <= hoursTaken && hoursTaken <= MAX_HOURS1)
Ah ok, thank you! I wonder why they don't allow it the way I originally had it.
well, it works, but not the way you want it...

because of the operator priority and left-to-right opartors for same-level operators:
if((MIN_HOURS <= hoursTaken) <= MAX_HOURS1)

its the same as:
a + b + c
which is equivalent to
((a + b) + c)

the only difference is the operator
Last edited on
Yes..

(MIN_HOURS <= hoursTaken) return bool (0 or 1)

which it always less than MAX_HOURS1




Gamer2015 that makes a lot of sense, and thank you for breaking it down obscure.
Topic archived. No new replies allowed.