i got an if statement that will display the letter A and the percent grade if there percent grade is between 90 and 100. and the else statement will do the same but its display a B if the grade is between 80 and 90, my problem is, even if the grade percentage comes out as a 50%, it only displays the first IF statement and says the person who got a 50% got a A which def. is not right so idk what im doing wrong with this code

total=totalPoints/eightfiftytotal*100;
	

// display the grade letter and grade percentage

		printf("Grade     ||     Percentage\n\n");

	if((int) total > 90 || total < 100) {
		printf("%c           %d percent\n ", gradeLetter1, (int) total);
	}
	
		else if ((int) total > 80 || total < 89) { 
			printf("%c", gradeLetter2, (int) total);
		}

Recommended Answers

All 6 Replies

Right away I see a problem...What if you enter 90 or 100, how does your program handle that. You should be saying, A is a grade from 90 to 100.

if (total >= 90)

Right away I see a problem...What if you enter 90 or 100, how does your program handle that. You should be saying, A is a grade from 90 to 100.

if (total >= 90)

good point. But I dont think that still helps why it always displays the first IF statement still even if its below 90

It should be (condition && condition) not (condition || condition). All grades(except 100) will be less than 100 so your first condition will always be true.

if((int) total > 90 || total < 100)

the logical operator OR is not very efficient at traping a number in certain boundaries, because. only one of the two conditions to be true to conclude that the condition is valid to proceed, so if you enter 50

total < 100

is valid and if you enter 101

total > 90

so your first if statement is always valid

if you use AND operator

if( total > 90 && total < 100

then both conditions must be reached for the compiler to declare that condition valid

Seems like that 80 is less than 100 satisfying your first compare.

but since it doesn't satisfy the second compare the compiler will consider the condition invalid

try using the && operator pal don't use the ||

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.