so this is my final grade calculator that i turned in, and my teacher came back and said my IF/ELSE structure was Malformed, Is there something i did poorly, or wrong, what can i do to make it better. any suggestions will be taken accounted for

``````#include <stdio.h>

int main() {
// initialize variables needed for user inputs and calculations
int midterm = 0;
int attendence = 0;
int totalPoints = 0;
int total = 0;
int finalexam = 0;
int finalExam2 = 0;
float eightfiftytotal = 850;

//display and ask user for score inputs with error correction

fflush(stdin);

fflush(stdin);

fflush(stdin);

fflush(stdin);

scanf("%d", &midterm);
fflush(stdin);

scanf("%d", &attendence);
fflush(stdin);

// add all the inputs and display the total points then calculate the percentage

total=totalPoints/eightfiftytotal*100;

printf("your total points are: %d out of 850\n\n", totalPoints);

if((int) total >= 90 && total <= 500) {
printf("%c---------||-----%d Percent\n\n ", gradeLetters[0], (int) total);
}
else if((int) total >= 80 && total <= 89) {
}
else if((int) total >= 70 && total <= 79) {
}
else if((int) total >= 60 && total <= 69) {
}
else if((int) total <= 59) {
printf("You are failing with a %d percent \n\n", (int) total);
}

return 0;

}``````
4
Contributors
3
Replies
5
Views
7 Years
Discussion Span
Last Post by abhimanipal

It isn't necessary to typecast total because total was declared as an int

I would line up all the else's and reverse the order of the tests

``````if( total < 60)
printf("You are failing with a %d percent \n\n", (int) total);
else if( total < 70)
else if( total < 80)
<etc. etc.>``````

Edited by Ancient Dragon: n/a

In the if ... else block you have used the variable total twice. I think you meant to use totalPoints in one of the places ...

Also if I am using 2 conditions separated by a logical operator in an if statement trying I write it like this

``````if ( (a <100) && (a >50))
{}``````

This makes it easy for me to spot an error in my conditions

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.