0

I'm working with a program who will calculate the distance between two geographical points on the Earth's surface. Im pretty new at C++ and I apologize if my English is bad

Any suggestions on how i can get the content at the program at the bottom into the one at the top with no errors?

This is the program who doesn`t work.

   #include <iostream>
   #include <cstdio>
   #include <cmath>
   using namespace std;

    int main ()
    double getdistance(double x1,double y1,double x2,double y2)
    {

        const double PI = 3.1415927; 
        double radius, sum; 
        int exit = 1;
        char choice;
        while (exit == 1)
    {

        cout    << "**** Calculate Program **** \n\n"
                << "1. Volume\n"
                << "2. Div\n"
                << "3. Calculate two geographical points\n"
                << "4. Working\n\n"
                << "5. Quit program\n\n";

        cout    << "Choose one of the options and write it here: "; 
        cin     >> choice; 

switch (choice) 
    {
    // 
    case '1':   
        float volume (float radius);               
    {
            cout << "radius you want to calculate: " << endl;
            cin >> radius;
            sum = 4.0/3.0 * PI * pow (radius, 3);
            cout << "Volume: " << sum << endl;
    }

    break;


    // 
    case '2':   
        cout<<"div \n"; 
            break;
    // 
    case '3':   

        radius=6371; // radius km
        double pi=3.14159265;
        double x1,y1,x2,y2;
        // X og Y coords
            x1=(x1/180)*pi; 
            y1=(y1/180)*pi;
            x2=(x2/180)*pi;
            y2=(y2/180)*pi;

         // if else
            if (x1==x2 && y1==y2)
            return 0;
            else
        {
            if ((sin(x2)*sin(x1)+cos(x2)*cos(x1)*cos(y2-y1))>1)
             return radius*acos(1.0);
            else
            return radius*acos(sin(x2)*sin(x1)+cos(x2)*cos(x1)*cos(y2-y1));


            // 

            double x1,y1,x2,y2;
            cout<<"Write the coords you like to calculate distance from: \n";
            cin>>x1>>y1;
            cin>>x2>>y2;
            cout<<"distance in km : \n";
            cout<<getdistance(x1,y1,x2,y2)<<endl;
            return 0;
        }

    break;


    case '4':   
        cout<<"working \n"; 
    break;


    // 
    case '5':   
            exit = 0;
    break;
    default: 
            exit = 0;
            cout << "ERROR!";
     }  // Switch quit

} // While quit
return 0;
}

This is the program that work , but i have to put it inside case 3 in the program above.

# include <iostream>
# include <cstdio>
# include <cmath>

using namespace std;

double getdistance(double x1,double y1,double x2,double y2)
{
    double radius;
    radius=6371; // radius km
    double pi=3.14159265;

    // X og Y coord
    x1=(x1/180)*pi; 
    y1=(y1/180)*pi;
    x2=(x2/180)*pi;
    y2=(y2/180)*pi;


    if (x1==x2 && y1==y2)
        return 0;
    else
    {
       if ((sin(x2)*sin(x1)+cos(x2)*cos(x1)*cos(y2-y1))>1)
           return radius*acos(1.0);
       else
           return radius*acos(sin(x2)*sin(x1)+cos(x2)*cos(x1)*cos(y2-y1));
    }
}
int main()
{

    double x1,y1,x2,y2;
    cout<<"Write the coords you like to calculate distance from: \n";
    cin>>x1>>y1;
    cin>>x2>>y2;
    cout<<"distance in km : \n";
    cout<<getdistance(x1,y1,x2,y2)<<endl;
    return 0;
}
  • 2
    Not sure what you mean, but your program compiles for me – Andy Prowl Feb 05 '13 at 20:01
  • You should show the code with the `switch` statement, as well as the full error message. – Reinstate Monica -- notmaynard Feb 05 '13 at 20:02
  • Anyway just for the record, comparing two doubles with `==` is not a good practice. See [this](http://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/). – Andy Prowl Feb 05 '13 at 20:03
  • 1
    Please get rid of the [system("pause")](http://www.gidnetwork.com/b-61.html). You have no idea whether or not my computer has a command called `pause` or what it might do. It could pause the cooling system on a nuclear reactor. – David Schwartz Feb 05 '13 at 20:04
  • 1
    Welcome to Stack Overflow. I am confused why you would post the program that "works fine". Please post the smallest possible complete program that demonstrates the error you are seeing. For more information on this debugging technique, see http://SSCCE.ORG. – Robᵩ Feb 05 '13 at 20:12
  • I apologize if i make you guys confused. I have edit my post and written down the program with the errors and the program who works. I need help to get both program to works – user2044438 Feb 05 '13 at 20:29

1 Answers1

2

The program as it is seems fine. As for the switch statements, they only work with integers or single character values. switch does not compare strings or floating-point numbers. Perhaps posting the full code of your alternative would help identify the problem.

EDIT

int main ()
double getdistance(double x1,double y1,double x2,double y2)
{

This is wrong. When you declare functions you must declare them before main and define them before or after main, although conventionally you would define them after.

i.e.

//#include directives

double getdistance(double x1,double y1,double x2,double y2);

int main() {
// code for main()
}

double getdistance(double x1,double y1,double x2,double y2) {
//code for your function
}
rubbyrubber
  • 567
  • 4
  • 19