Program doesn't calculate

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?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <stdio.h>
#include <stdlib.h>
#include <iostream>

using namespace 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).
closed account (Dy7SLyTq)
what are you using as input?

edit: nevermind
Last edited on
long double main, thanks a lot for your help. I've learned C in college and I didn't know that C++ would be so different.
long double main wrote:
since you don't use any functions in that header

system(const char*) ?

Aceix.
system is in <stdlib.h>.
system(const char*) is part of cstdlib or stdlib.h
http://www.cplusplus.com/reference/cstdlib/system/
So it is true that the OP does not necessarily need cstdio or stdio.h since iostream is in use.
Oops didn't see that clearly. Sorry my fault.

Aceix.
Topic archived. No new replies allowed.