### 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.
 ``12345678910111213141516171819202122232425262728293031323334353637383940`` ``````# include # include # include 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<

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

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354`` ``````#include #include #include 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.

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475`` ``````#include #include #include 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<

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.