#calculate with Ascii to floating point

7 messages · Page 1 of 1 (latest)

proven comet
#

I should calculate the average value of 4 letters as floating point.

int main() {
char c1, c2, c3, c4;

printf("Bitte geben Sie vier einzelne Zeichen ein: ");
scanf("%c %c %c %c", &c1, &c2, &c3, &c4);
printf("ASCII Codes: %d %d %d %d\n", c1, c2, c3, c4);
printf("ASCII Code (Mittel,fl): %f \n", atof((const char *) c1+c2+c3+c4)/4));


return 0;

}

faint jayBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

daring glen
#

what you are doing in the line atof((cont char *) c1 + c2 + c3 + c4) / 4 doesn't make sense. That line is adding c1, c2 ,c3, and c4 and using the resulting value as an address. It then interprets the values at this arbitrary address as a string an converts it to a float, then divides the result by 4. in your screenshot, I think you have more of the right idea when you do (c1 + c2 + c3 + c4) / 4, but this will yield in an int - since it seems like you want it to be a float, change this line to (c1 + c2 + c3 + c4) / 4.0. This now divides an int (specifically char) by a float which results in a float.

amber owl
quartz wagon
#

Obviously %lf does the same, so should you prefer to be more explicit then more power to you.

proven comet
#

!solved