问题描述
我是C ++的新手。这是我的第一个任务,我在打印(在main()
中)到函数d
的最终计算距离getairdistance()
时遇到了麻烦。我的程序如下所示,我将不胜感激!
#include <iostream>
#include <cmath> //for pi
using namespace std;
void getAirdistance(int *ptr,int *ptr1);
int main(){
struct LOC{
char loc_name[50]; //name of a location on earth
double latitude; //latitude of this location
double longitude; //longitude of this location
}; //structure deFinition
struct LOC location1;
struct LOC location2; //declared two structures
struct LOC *ptr,*ptr1; //declared two pointers
ptr = &location1; //pointer to the first location structure
ptr1 = &location2; //pointer to the second location structure
cout << "Please enter the name of the first location: "; //print statement
cin.getline(location1.loc_name,50);//use getline to store the first location
cout << "Please enter the latitude (degrees): "; //print statement
cin >> location1.latitude; //use cin to get the first latitude
cout << "Please enter the longitude (degrees): "; //print statement
cin >> location1.longitude; //use cin to get the first longitude
fflush(stdin);
cout << "Please enter the name of the second location: ";
cin.getline(location2.loc_name,50);
cout << "Please enter the latitude (degrees): ";
cin >> location2.latitude;
cout << "Please enter the longitude (degrees): ";
cin >> location2.longitude; //exact same thing as above
//getAirdistance(location1,location2);
cout << "The air distance between " << location1.loc_name << " and " << location2.loc_name << " is appx. " << " km. "; //final print statement
}
void getAirdistance(int *ptr,int *ptr1){ //function deFinition
double long1 = *(ptr + 2) * (M_PI/180); //longitude of first location (rad)
double long2 = *(ptr1 + 2) * (M_PI/180); //longitude of second location (rad)
double lat1 = *(ptr + 1) * (M_PI/180); //latitude of first location (rad)
double lat2 = *(ptr1 + 1) * (M_PI/180); //longitude of first location (rad)
double x = (long2 - long1) * cos((lat1 + lat2)/2); //x coordinate difference
double y = lat2 - lat1; //y coordinate difference
int R = 6371; //Radius of the earth
int d = R * sqrt(pow(x,2) + pow(y,2)); //distance between the two locations
cout << d;
}
解决方法
为调用者(即return
)使函数d
的值为main()
,例如:
int getAirDistance(int *ptr,int *ptr1);
int main(){
...
int d = getAirDistance(...);
cout << ... << d << ...;
return 0;
}
int getAirDistance(int *ptr,int *ptr1){
...
int d = ...;
return d;
}
话虽这么说,您的函数将int*
指针作为输入,但是int
代码中没有main()
变量可以传递给函数。使用指针算术表明您认为+1
和+2
正在访问LOC
结构的第一和第二字段,但这根本不会发生。我怀疑您真正打算做的是这样的事情:
#include <iostream>
#include <cmath> //for pi
using namespace std;
struct LOC{
char loc_name[50]; //name of a location on earth
double latitude; //latitude of this location
double longitude; //longitude of this location
}; //structure definition
int getAirDistance(const LOC &loc1,const LOC &loc2);
int main(){
LOC location1;
LOC location2; //declared two structures
cout << "Please enter the name of the first location: "; //print statement
cin.getline(location1.loc_name,50);//use getline to store the first location
cout << "Please enter the latitude (degrees): "; //print statement
cin >> location1.latitude; //use cin to get the first latitude
cout << "Please enter the longitude (degrees): "; //print statement
cin >> location1.longitude; //use cin to get the first longitude
fflush(stdin);
cout << "Please enter the name of the second location: ";
cin.getline(location2.loc_name,50);
cout << "Please enter the latitude (degrees): ";
cin >> location2.latitude;
cout << "Please enter the longitude (degrees): ";
cin >> location2.longitude; //exact same thing as above
int d = getAirDistance(location1,location2);
cout << "The air distance between " << location1.loc_name << " and " << location2.loc_name << " is appx. " << d << " km. "; //final print statement
return 0;
}
int getAirDistance(const LOC &loc1,const LOC &loc2){ //function definition
double long1 = loc1.longitude * (M_PI/180); //longitude of first location (rad)
double long2 = loc2.longitude * (M_PI/180); //longitude of second location (rad)
double lat1 = loc1.latitude * (M_PI/180); //latitude of first location (rad)
double lat2 = loc2.latitude * (M_PI/180); //latitude of second location (rad)
double x = (long2 - long1) * cos((lat1 + lat2)/2); //x coordinate difference
double y = lat2 - lat1; //y coordinate difference
int R = 6371; //Radius of the earth
int d = R * sqrt(pow(x,2) + pow(y,2)); //distance between the two locations
return d;
}