This is part of a code I’m working in, and for some reason it doesn’t work. It doesn’t matter the values I enter for h and min, it’s returning GT = -off. If I put off = -2.0, it returns GT=2.000000. Do you know what’s the problem?
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
usingnamespace std;
int main () {
int h, min;
double off, GT;
cout << "Enter the hour at your place: \n";
cin >> h;
cout << "Now enter the minutes: \n";
cin >> min;
cout << "Enter your local offset to Greenwich Time: \n";
cin >> off;
GT=(h/24)+(min/60)-off;
cout << "Greenwich Time in decimals is " << GT << "\n";
system ("PAUSE");
return 0;
}
On line 16, you're doing integer division: GT=(h/24)+(min/60)-off;
Since h and min are integers, h/24 and min/60 will both be integers as well.
The easiest way to fix that is to make one of them a floating-point value: GT=(h/24.0)+(min/60.0)-off;
Also, you don't need to #include <stdio.h> (probably should've been #include <cstdio> anyways since it's C++) in your code (since you don't use any functions in that header).