-2

program is supposed to convert a user inputed number of minutes into days, hours, and minutes

 #include <stdio.h>

 int main(void) {



 int input_minutes, minutes, r_minutes;  
 int days, hours;
 float r_days, r_hours;


 printf("Enter a number of minutes: ");
 scanf("%d", &input_minutes); 

 days = input_minutes / 1440;

Here is where I want to get the remainder of input_minutes divided by 1440 but instead I am getting 160.00007 when I compile.

 r_days =  input_minutes % 1440;
 hours = r_days * 24;
 r_hours = (r_days * 1440) - hours;
 minutes = r_hours * 60; 

 printf("r_days = %f", r_days);

 printf("This represents:%d days, %d hours, %d minutes\n",days, hours,        minutes);
S. Thomas
  • 47
  • 1
  • 2
  • 8
  • 1
    why are you using `float` for your `r_days` variable? – Jean-François Fabre Feb 06 '17 at 21:18
  • @WeatherVane: Just updated myself on this, and the standard is on your side. Sorry for the confusion. – 3442 Feb 06 '17 at 21:44
  • @KemyLand today, the signature can be `int main(void)` or `int main(int argc)` or `int main(int argc, char *argv[])` or `int main(int argc, char *argv[], char *env[])`. – Weather Vane Feb 06 '17 at 21:47
  • @WeatherVane, I don't know any platform that allows `int main(int argc)`. – Jens Gustedt Feb 06 '17 at 22:41
  • @JensGustedt MSVC allows it: `int main(int argc) { printf("argc=%d\n", argc); }` reports the number of arguments. No compiler warnings. – Weather Vane Feb 06 '17 at 22:44
  • @JensGustedt ...which could be useful for a program that does not accept more arguments than the executable name, and so does not need to know what they are, only there are too many. – Weather Vane Feb 06 '17 at 22:50
  • @WeatherVane, oh, MSVC. It still does not seem to be conforming to any C standard, and doesn't seem to make much of an attempt for getting closer. So I often forget that it even exists. – Jens Gustedt Feb 06 '17 at 22:53
  • @JensGustedt I somehow knew you would say that. However, it can't be a platform "you do not know of". I find it remarkable that non-standard gcc extensions are "good" but "MSVC" is always bad. By the way I drive a Skoda. They are wonderful. Let's strike a deal: if you drive a better car, please don't make it an ego trip. – Weather Vane Feb 06 '17 at 22:53
  • @JensGustedt however from your profile, I congratulate you for turning away from C++. – Weather Vane Feb 06 '17 at 23:01

2 Answers2

4

There are no valid reasons to use float types here (no division involved, except for the integer modulo). Using float only causes the loss of precision and problems to represent & display an integer properly as you have experienced.

quickfix:

int r_days, r_hours;

and

printf("r_days = %d", r_days);

see also: Is floating point math broken?

Community
  • 1
  • 1
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
2

You might want to change the logic of how you calculate the hours and minutes. Days calculation seems okay.

r_days =  input_minutes % 1440;
hours = r_days/60;
minutes = r_days % 60;
printf("This represents:%d days, %d hours, %d minutes\n",days, hours, minutes);
VHS
  • 9,534
  • 3
  • 19
  • 43