EvilZone

Programming and Scripting => C - C++ => : gh0st April 08, 2011, 12:30:16 AM

: [C++] Symbolic const example
: gh0st April 08, 2011, 12:30:16 AM
hello guys Im just making some exercises from the book that Im reading and I decided to post it  cause they are interesting and you can learn a lot from it.

3.Write a program that asks the user to enter a latitude in degrees, minutes, and seconds and that then displays the latitude in decimal format. There are 60 seconds of arc to a minute and 60 minutes of arc to a degree; represent these values with symbolic constants. You should use a separate variable for each input value. A sample run should look like this:

Enter a latitude in degrees, minutes, and seconds:
First, enter the degrees: 37
Next, enter the minutes of arc: 51
Finally, enter the seconds of arc: 19
37 degrees, 51 minutes, 19 seconds = 37.8553 degrees


the answer would be like this:

:
#include <iostream>
#include <cmath>

using namespace std;

int main()
{
    int degrees;// declaring a variable
    int minutes;
    int seconds;
    const float dividingminutes = 60;// here is where a contant variable goes
    const float dividingseconds = 60;//is like a constant in maths you may know it
   
    cout << "Enter the latitudes in degrees ,minutes and seconds:\a" << endl;// "\a" makes a cool sound and if you put it a lot of times your computer may crash :D just kidding
    cout << endl;
    cout << "First enter the degrees: ____\b\b\b\b";// what does the "\b" do is just to make the input place a underline there are "_ _ _ _" underscores
    cin >> degrees;
    cout << endl;
    cout << "Next, enter the minutes of arcs: ____\b\b\b\b";
    cin >> minutes;
    float minutestodegrees = minutes / dividingminutes;//declaration of a new float variable and here is where we use a constant and makes the conversion
    cout << endl;
    cout << "Finally,enter the seconds of arcs: ____\b\b\b\b";
    cin >> seconds;
    float secondstodegrees = (seconds / dividingseconds) / dividingseconds;//declaration of a new float variable and here is where we use a constant and makes the conversion
    cout << endl;
    float result = degrees + minutestodegrees + secondstodegrees//here is the final conversion
    cout << degrees << "degrees " << minutes << "minutes, " << seconds << "seconds: " << result;
    cout << endl;
    system("pause");
    return 0;
}
   

more about constants: http://en.wikipedia.org/wiki/Constant_%28programming%29 (http://en.wikipedia.org/wiki/Constant_%28programming%29)