# Thread: How to write a percentage in this problem?

1. Looks like you're making good progress. Does your output look correct?

Code:
```Enter the grades. Enter -1 to stop.
75
Enter the grades. Enter -1 to stop.
50
Enter the grades. Enter -1 to stop.
-1
0.00 percent are passing.```
You're performing integer division in the "percentage" calculation, which means the result is truncated (you lose any fractional part).
Try casting one of the variables to a double:

Code:
`percentage = (((double)passingGrades / total) * 100);`
You can also neaten up the logic in your loop. It helps to plan the logic "by hand" on paper first, before translating it to code.

I might approach the loop as follows:

Code:
```while grade != -1

prompt for input

if grade is between 70 and 100
increment passing

else if grade is between 0 and 69
increment failing

else if grade is not -1
print error```

2. Originally Posted by userxbw
Code:
```   ---> // int grades, passingGrades, total, failingGrades;
// change everything to use float, or double
// then format it properly in your printf 's```
No, integer is a more appropriate data type for counting. The OP can use casting to get a double result, as I have shown.

Originally Posted by userxbw
Code:
```{ // invalid code, because it never gets used,
// what else is using -1 ```
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.

3. Originally Posted by Matticus
Looks like you're making good progress. Does your output look correct?

Code:
```Enter the grades. Enter -1 to stop.
75
Enter the grades. Enter -1 to stop.
50
Enter the grades. Enter -1 to stop.
-1
0.00 percent are passing.```
You're performing integer division in the "percentage" calculation, which means the result is truncated (you lose any fractional part).
Try casting one of the variables to a double:

Code:
`percentage = (((double)passingGrades / total) * 100);`
You can also neaten up the logic in your loop. It helps to plan the logic "by hand" on paper first, before translating it to code.

I might approach the loop as follows:

Code:
```while grade != -1

prompt for input

if grade is between 70 and 100
increment passing

else if grade is between 0 and 69
increment failing

else if grade is not -1
print error```
I got it to work!!!!
Adding the double part was the solution. Thank you so much for your help and patience guys. I think planning it out first by hand will help me a lot.

4. 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:
```,
printf("total %.0lf\n", total);
//   percentage =  (( (double) passingGrades / total) * 100 );
percentage =  (( passingGrades / total) * 100 );

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)
{

{
// Ask the user to input grades, and type -1 to stop
printf("Enter the grades. Enter -1 to stop. \n");
printf("That is not a valid grade. \n");
}
// Calculates the total and percentage
return 0;
}```
results
Code:
```Enter the grades. Enter -1 to stop.
-1
total 11
OP said they had it figured out so....

5. 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?
I think Matticus still has a point, though. If you only wanted to avoid a cast, you could have simply taken percentage variable and done this:
Code:
```double percentage = passingGrades;
percentage = percentage / total * 100;```
You can think of casting as an attempt by the compiler to store the value in an implicit variable of the desired type. To avoid casting, you can employ the same approach explicitly, most of the time. (Some casts are required.)

Doing it your way was pretty silly.

6. Originally Posted by whiteflags
I think Matticus still has a point, though. If you only wanted to avoid a cast, you could have simply taken percentage variable and done this:
Code:
```double percentage = passingGrades;
percentage = percentage / total * 100;```
You can think of casting as an attempt by the compiler to store the value in an implicit variable of the desired type. To avoid casting, you can employ the same approach explicitly, most of the time. (Some casts are required.)

Doing it your way was pretty silly.

I kept thinking about this other program I help someone with that calculated grades, and it was different input, so it kept clouding my judgment on input. but I am ok now, thanks !

so go look at what I just posted fixing my mess up. then tell me what you think. post #19

7. If that is you doing this to me go try the two lines that I wrote in the OP's program and tell me what you think. post #20.

8. Originally Posted by whiteflags
If that is you doing this to me go try the two lines that I wrote in the OP's program and tell me what you think. post #20.
either way what I ended up with fixes for my complaints to the OP, I was misleading the witness, on the -1 check thing, and I must have been modifying that post #19 while you where posting. never the less it works either way. but my checks keeps it from counting if grades are -1 on either end of the spectrum. &&

just using this
Code:
```double passingGrades, failingGrades;

9. I only think that making the counting variables double type is unnecessary, and I wanted to suggest something that I know is less of a hassle. I'm not sure I follow everything else that you are talking about.

10. Originally Posted by whiteflags
I only think that making the counting variables double type is unnecessary, and I wanted to suggest something that I know is less of a hassle. I'm not sure I follow everything else that you are talking about.
I was being lazy and confused, so I just made everything a double, fixed. Not taking into account, and getting confused by OP code, and that what was being done with the -1 assigning it to grade when if grade = -1 quit

use int to take in scores. double to do the math because of floating point math. grade does not get added into that equation, which my brain keep thinking it did, so that screwed me up. then got it to no casting and using the int for count, and replacing the check to -1 so if greater than 100 it will not be counted, or if lesser then 70 which -1 is it will not count it, or cause the code to add 1 to fail count. so I just made it to not accept grade -1 period, and no casting of data types.

either way I am done and it works properly my code does anyways. the if else 's could perhaps be removed but that is not my problem.

and now you're probably more confused.

11. Originally Posted by userxbw
I was being lazy and confused, so I just made everything a double, fixed. Not taking into account, and getting confused by OP code, and that what was being done with the -1 assigning it to grade when if grade = -1 quit
Not to be rude (and I apologize if I come across that way), but perhaps you should focus your effort on post quality instead of quantity. Take the time to read and understand the problems, and take the time to write up a thorough reply.

Blasting several versions of the OP's program back at them with minor modifications usually just leads to more confusion on their part. Understanding what the problems are, and addressing them in a clear and concise way, will usually go much farther in helping them.

It's great that you want to help people - that is really good for this community - just make sure your contributions are as good as they can be.