K
Kelly Goode
I have a problem with some simple math operations resulting in the wrong
integer. Why am I getting the wrong integer back for 2.30? I should get
back 2 hours and 30 minutes but instead I get 2 hours and 29 minutes.
#include <stdio.h>
int main(void)
{
double elapsed_time;
int hours;
int minutes;
printf("Enter hours (hh.mm): ");
scanf("%lf", &elapsed_time);
//take the integer of 2.30 to get # of hours which is 2
hours = elapsed_time;
// Ex. (2.30 - 2) = 0.3 ===> (0.3 * 100) = 29 !?!?!?!?!?!?!?!?!?
minutes = (elapsed_time - hours) * 100;
printf("\nYou entered an elapsed time of %d hours and %d
minutes.\n", hours, minutes);
getch();
//Output for 2.30 is always 2 Hours and 29 minutes
return 0;
}
integer. Why am I getting the wrong integer back for 2.30? I should get
back 2 hours and 30 minutes but instead I get 2 hours and 29 minutes.
#include <stdio.h>
int main(void)
{
double elapsed_time;
int hours;
int minutes;
printf("Enter hours (hh.mm): ");
scanf("%lf", &elapsed_time);
//take the integer of 2.30 to get # of hours which is 2
hours = elapsed_time;
// Ex. (2.30 - 2) = 0.3 ===> (0.3 * 100) = 29 !?!?!?!?!?!?!?!?!?
minutes = (elapsed_time - hours) * 100;
printf("\nYou entered an elapsed time of %d hours and %d
minutes.\n", hours, minutes);
getch();
//Output for 2.30 is always 2 Hours and 29 minutes
return 0;
}