Originally Posted by
Matticus
No, integer is a more appropriate data type for counting. The OP can use casting to get a double result, as I have shown.
If the following "if" is removed, then without additional modification, the final "else" will increment failingGrades on invalid input below -1.
This was also addressed in my suggested logic example.
yes .but if needed 34.5 then what? . too I wanted to stay away from casting ... I doubled everything and that works too. but yes, casting too works. but i wanted to stay away from casting because who knows if they've covered that in class yet or not?
just fix it in the printf
Code:
,
total = passingGrades + failingGrades;
printf("total %.0lf\n", total);
// percentage = (( (double) passingGrades / total) * 100 );
percentage = (( passingGrades / total) * 100 );
printf("%.0lf are passing grades. \n", passingGrades);
printf("%.2lf percent are passing grades. \n",percentage );
do not look at casting part I as trying to stay away from that as I said why already.
no casting / Plus I take that all back, most of it anyways. upon further investigation, I see why you where
trying to use that -1 further
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
double passingGrades, failingGrades;
int grades;
passingGrades = 0.0;
failingGrades = 0.0;
while(grades != -1)
{
// Ask the user to input grades, and type -1 to stop
printf("Enter the grades. Enter -1 to stop. \n");
scanf("%d", &grades);
if(grades >= 101 )
printf("That is not a valid grade. \n");
else if (grades < 70 && grades != -1 )
failingGrades += 1;
else if (grades >= 70 && grades != -1 )
passingGrades += 1;
}
// Calculates the total and percentage
printf("total %.0lf\n", passingGrades + failingGrades);
printf("%.0lf are passing grades. \n", passingGrades); // gives total
printf("%.2lf percent are passing grades. \n",(( passingGrades / (passingGrades + failingGrades) ) * 100 ) );
return 0;
}
results
Code:
Enter the grades. Enter -1 to stop.
-1
total 11
5 are passing grades.
45.45 percent are passing grades.
OP said they had it figured out so....