Converting Degrees Decimal to DMS - HELP

New to C++, i'm sure i have included libraries that i don't need however i am trying to convert radians, to degrees decimal, to DMS and display the result.

The problem is,
The minutes value is half of what it should be in output,
The seconds value is in the thousands when it should be [0-59].

Appreciate any input.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#include <iostream>
#include <istream>
#include <fstream>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <iomanip>



using namespace std;



int main()
{
    string var;
    
    while(var != "n")
    {
    
        cout << "Enter Easting: ";
        double e1;
        cin >> e1;
    
        cout << "Enter Northing: ";
        double n1;
        cin >> n1;
    
        cout << "Enter Easting: ";
        double e2;
        cin >> e2;
    
        cout << "Enter Northing: ";
        double n2;
        cin >> n2;
    
        
        double distance = (sqrt((pow(e2-e1,2)+pow(n2-n1,2))));
        cout << fixed;
        cout << setprecision(3);
        cout << "Distance: " << distance << endl;
        
        double degdec = (atan((pow(e2-e1,2)/pow(n2-n1,2)))) * (180/M_PI);

        int D = (degdec);
        int M = (degdec - D) * 60;
        int S = (degdec - D - M/60) * 3600;
        
        cout << "Bearing: " << D << ", " << M << ", " << S << ". " << endl;
        
        cout << "Would you like to run Joins again (y/n): ";
        cin >> var;
    }
    return 0;
}
Hi,

Consider doing the calculations to min and second with double first, then convert to unsigned int afterwards (if required) . Line 48 does integer division, and this messes up your calc. I always write double numbers with digits either side of the decimal point, it forces a double calculation, avoid integer division.

Also need some logic to cover when min or sec is near to 60.0. Consider your bearing is 359d59'59.6" , this should result in 0d00'00" obviously, but at the moment you might get 359d60'60"

Can also use the floor function, C++ has overloads so this will work with double.

Should use the atan2 function, then you can get the correct quadrant for your bearing. atan gives a result +/- 90 degrees

The pow function is generally inefficient for squaring numbers (it probably uses a binomial expansion or some other technique to do it) so it's easier to just multiply the number with itself. I would work out the delta (EAST or NORTH) and square that.

To be pedantic, an Easting is a delta EAST, a lot of people call total coordinates Easting / Northing when they really mean EAST / NORTH. In the old days our traverse tables had Easting, Westing, Northing and Southing, then EAST NORTH

180.0/M_PI is a constant value, so you could express that in the code:

constexpr double RadianToDegree = 180.0/M_PI; // C++11, use const otherwise

Good Luck !!



Topic archived. No new replies allowed.