Problems with switch-statement

I have trouble putting this program below into a switch statement at the next program under "case 3" Im new at C++ so its difficult to see the problem. I only get error everytime and its getting really on my nerves. Anyone who knows what to do?


The first one her is a program who will calculate the distance between two geographical points on the Earth's but i need to get it under case 3 instead in the program after this one.
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
# 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;
}


This is the program i want to put it to under "case 3"

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
#include <iostream>
 #include <cstdio>
 #include <cmath>
 using namespace std;

    int main ()
    {
        const double PI = 3.1415927; 
        double radius, sum; 
        int exit = 1;
        char choice;
        while (exit == 1)
    {
	cout   << "**** Calculate Program **** \n\n"
		<< "1. Calculate Volume\n"
                << "2. Div\n"
                << "3. Calculate two geographical points\n\n"
                << "4. 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':   

        cout<<"Write the coords you like to calculate distance from: \n";
            
    break;

    case '4':   
            exit = 0;
    break;
    default: 
            exit = 0;
            cout << "ERROR!";
     }  
} 
return 0;
}
Last edited on
What errors are you getting? Please post the exact error messages.
I compiled you second program and did not get any errors.

The only issue I see with incorporating the first program into the second one is that
the function getdistance doesn't exist in the second program.

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
const double pi=3.14159265;

double getdistance(double x1,double y1,double x2,double y2)
{   double radius = 6371; // radius km
    
    // 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 radius, sum; 
    int exit = 1;
    char choice;
    while (exit == 1)
    {	cout   << "**** Calculate Program **** \n\n"
		<< "1. Calculate Volume\n"
                << "2. Div\n"
                << "3. Calculate two geographical points\n\n"
                << "4. 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':   
			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;
			break;

		case '4':   
			exit = 0;
			break;

		default: 
			exit = 0;
			cout << "ERROR!";
		}  
	} 
	return 0;
}





I've also had cases where, if you're declaring variables within a case block, you need to have the whole block within curly-braces. It may be affecting you here, but it's only a possibility.

Also, what's the forward declaration of function 'volume' doing at line 44? I don't see you using it.

Cheers,
Jim
Topic archived. No new replies allowed.