1 //----------------------------------------------------------------------------
2 //
3 // License:  LGPL
4 //
5 // See LICENSE.txt file in the top level directory for more details.
6 //
7 // Author:  David Burken
8 //
9 // Description: Application to convert degrees, minutes seconds (DMS)
10 // to decimal degrees.
11 //
12 //----------------------------------------------------------------------------
13 // $Id$
14 
15 #include <cstdlib>
16 #include <iostream>
17 #include <iomanip>
18 #include <cmath>
19 #include <string>
20 #include <sstream>
21 
22 using namespace std;
23 
getDegrees(double & deg)24 void getDegrees(double& deg)
25 {
26    std::string s;
27    cout << "Enter degrees:  " << flush;
28    cin >> s;
29    if ( (s == "q") || (s == "Q") )
30    {
31       exit(0);
32    }
33    else
34    {
35       istringstream is(s);
36       is >> deg;
37       if( is.fail() )
38       {
39          cerr << "Must enter a valid number between -180 and 180 for degrees."
40               << "  Exiting..." << endl;
41          exit(1);
42       }
43       else if ( (deg < -180.0) || (deg > 180.0) ) // Check the range.
44       {
45          cerr << "Degrees must be between -180 and 180.  Exiting..."
46               << endl;
47          exit(1);
48       }
49    }
50 }
51 
getMinutes(double & min)52 void getMinutes(double& min)
53 {
54    std::string s;
55    cout << "Enter minutes:  " << flush;
56    cin >> s;
57    if ( (s == "q") || (s == "Q") )
58    {
59       exit(0);
60    }
61    else
62    {
63       istringstream is(s);
64       is >> min;
65       if(is.fail())
66       {
67          cerr << "Must enter a valid number between 0 and 60 for minutes."
68               << "  Exiting..." << endl;
69          exit(1);
70       }
71       else if ( (min < 0.0) || (min > 60.0) ) // Check the range.
72       {
73          cerr << "Minutes must be between 0 and 60.  Exiting\n";
74          exit(1);
75       }
76    }
77 }
78 
getSeconds(double & sec)79 void getSeconds(double& sec)
80 {
81    std::string s;
82    cout << "Enter seconds:  " << flush;
83    cin >> s;
84    if ( (s == "q") || (s == "Q") )
85    {
86       exit(0);
87    }
88    else
89    {
90       istringstream is(s);
91       is >> sec;
92       if( is.fail() )
93       {
94          cerr << "Must enter a valid number between 0 and 60 for seconds."
95               << "  Exiting..." << endl;
96          exit(1);
97       }
98       else if ( (sec < 0.0) || (sec > 60.0) ) // Check the range.
99       {
100          cerr << "Seconds must be between 0 and 60.  Exiting\n";
101          exit(1);
102       }
103    }
104 }
105 
main(int argc,char * argv[])106 int main(int argc, char* argv[])
107 {
108    enum
109    {
110       FOREVER=1
111    };
112 
113    if (argc != 1)
114    {
115       cout << "Usage: " << argv[0]
116            << "\nThis application will convert degrees, minutes, seconds to "
117            << "decimal degrees.\nIt take no arguments, you will be prompted "
118            << "for the degrees, minutes, seconds.\n" << endl;
119       return 0;
120    }
121 
122 
123    cout << "\nEnter \"q\" to quit.\n\n";
124 
125    double deg = 0.0;
126    double min = 0.0;
127    double sec = 0.0;
128 
129    cout << setiosflags(ios::fixed) << setprecision(15);
130 
131    while (FOREVER)
132    {
133       // Get the degrees.
134       getDegrees(deg);
135 
136       // Get the minutes.
137       getMinutes(min);
138 
139       // Get the the seconds.
140       getSeconds(sec);
141 
142       // Compute the answer.
143       if (deg < 0.0)
144       {
145          double d = fabs(deg);
146          deg = -(d + min/60.0 + sec/3600.0);
147       }
148       else
149       {
150          deg = deg + min/60.0 + sec/3600.0;
151       }
152 
153       if ( deg < -180.0 )
154       {
155          deg += 360.0;
156       }
157       else if ( deg > 180.0 )
158       {
159          deg -= 360.0;
160       }
161       cout << "Decimal degrees = " << deg << "\n" << endl;
162 
163    }
164 
165    return 0;
166 }
167