Modify the program.

closed account (D4NbpfjN)
Modify the program from Exercise 2 to allow the following categories:
Invalid data (data above 100),
‘A’ category (90 <=score<= 100),
‘B’ category (80 <=score<= 89),
“You Pass” category (60 <=score<= 79),
“You Fail” category (0 <=score<= 59).

The code below is what i have so far. What do i need to do to add the categories to my program?


1
2
3
4
5
6
7
8
9
10
11
12
13
  #include <iostream>
using namespace std;
int main()
{
	float average; // holds the grade average
	cout << "Input your average:" << endl;
	cin >> average;
	if (average >= 60)
		cout << "You Pass" << endl;
	else (average < 60);
		cout << "You Fail" << endl;
	return 0;
}
You're missing the tests for A and B categories.

Line 8: Your test for passing is not correct. The statement includes the condition <= 79, which you're missing.

Last edited on
closed account (D4NbpfjN)
1
2
if (90 <= average <= 100)
			cout << "A" << endl;

This is what I have for the first category. Is this right so far?
closed account (D4NbpfjN)
This is my program. I need some help with the else statements. I am getting errors.


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
#include <iostream>
using namespace std;
int main()
{
	float average; // holds the grade average
	cout << "Input your average:" << endl;
	cin >> average;
	
	if (average >= 100);
	cout << "Invalid Data" << endl;
	
	else (90 <= average <= 100);
			cout << "A" << endl;
				
			if ( 80 =< average = < 90);
			cout << "B" << endl;
			
		
else (60 <= average <= 80);
		cout << "You Pass" << endl;

	else (0 <= average <= 60);
		cout << "You Fail" << endl;
	return 0;
}
Not quite. That's not going to evaluate the way you think it will.

 
if ( (90 <= average) <= 100)

Note the extra parenthesis to indicate the order of evaluation.
90 <= average will result in a bool (true or false). That result will then be compared to 100, which will always be true.

What you want is:
 
if (average >= 90 && average <= 100) 

This syntax won't work.
 
if (90 <= average <= 100)


You need two separate tests, combined with the logical and operator &&
 
if ((90 <= average) && (average  <= 100))


Actually I would not recommend that style at all.

Rather, you can use a chain of if-else statements, each of which needs just a single test.

1
2
3
4
5
6
7
8
9
if (average > 100)
{
    // not valid
}
else if (average >= 90)
{
    // in range 90 to 100 inclusive
} 
else if ( ... etc. 

closed account (D4NbpfjN)

This is my code. I am having trouble with the categories. For example if I enter an 85 for the input it brings up A and B categories. What am I doing wrong?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;
int main()
{
	float average; // holds the grade average
	cout << "Input your average:" << endl;
	cin >> average;
	if (average >= 100)
		cout << "Invalid Data" << endl;
	
	if (average >= 90 && average <= 100);
		cout << "A" << endl;
		
		if (average >= 80 && average <= 90)
			cout << "B" << endl;
	
		if (average >= 60 && average <= 80)
		cout << "You Pass" << endl;
	
		if (average >= 0 && average <= 60)
		cout << "You Fail" << endl;
	return 0;
}
Line 11: The ; terminates the if statement. Remove it.

Lines 11,14: A 90 will satisfy both if statements.

Lines 14,17: An 80 will satisfy both if statements.

Line 17,20: A 60 will satisfy both if statements.


closed account (D4NbpfjN)
okay i changed were 60 is now 59. So when i enter for example 100 i am getting invalid data which that is what it wants, but i also am getting an A and "you fail". What am i doing wrong there? The code is below. This is what i have.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
using namespace std;
int main()
{
	float average; // holds the grade average
	cout << "Input your average:" << endl;
	cin >> average;
	if (average >= 100)
		cout << "Invalid Data" << endl;
	
	if (average >= 90 && average <= 100)
		cout << "A" << endl;
		
	if (average >= 80 && average <= 89)
			cout << "B" << endl;
	
		if (average >= 60 && average <= 79)
		cout << "You Pass" << endl;
	
		else (average >= 0 && average <= 59);
		cout << "You Fail" << endl;
	return 0;
}
Last edited on
for example 100 i am getting invalid data which that is what it wants, but i also am getting an A and "you fail".

This is one of the potential pitfalls of using two tests in each if statement. That is, you may end up with gaps where some values are not handled, or with overlaps where some value is handled more than once (both of which you have in your case). With care you can resolve these issues, but it is creating extra work for yourself, since each condition is tested more than once.

I'd still recommend the approach mentioned in a previous post,
http://www.cplusplus.com/forum/beginner/192070/#msg925909
I have done some minor changes like removed last else condition and added some{} after if loop.
here is the code which works fine and gives exact output at condition=100
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
#include <iostream>
using namespace std;
int main()
{
	float average; // holds the grade average
	cout << "Input your average:" << endl;
	cin >> average;
	if (average > 100)
	{
		cout << "Invalid Data" << endl;
	}
	else if (average >= 90 && average <= 100)
	{
		cout << "A" << endl;
	}	
	else if (average >= 80 && average <= 89)
	{
			cout << "B" << endl;
	}
	else if (average >= 60 && average <= 79)
	{
		cout << "You Pass" << endl;
	}
	else 
		cout << "You Fail" << endl;
	return 0;
}
Hi,

If average is 89.5, the result is fail.

Maybe cast the average to an unsigned int (rounding at that stage is acceptable), and go from there:

unsigned int averageAsInt = static_cast<unsigned int>(average);

or, alter the conditions: greater or equal, and less than.

Prefer double rather than float, the precision of float is easily exceeded.
I'd still say it is better to remove the redundant code. At line 12,
 
    else if (average >= 90 && average <= 100)
it is already known that average must no greater than 100 (because that was eliminated at line 8), thus there is no need for the clause && average <= 100.
The same would apply at lines 16 and 20. && average <= 89 and && average <= 79 are redundant and should be removed.

This simplification also allows the code to work correctly for values such as 89.5 with no other changes.
Topic archived. No new replies allowed.