【发布时间】:2021-06-15 01:44:59
【问题描述】:
所以我应该制定一个旅行计划来帮助旅行者了解他们在哪里(北、南、东、西)。我们应该为菜单选项创建枚举数据类型,而不是使用字符选项,用户选择他们要去的方向,它告诉总距离和离家的距离。 我尝试强制转换枚举,但出现此错误:预期的主要表达式之前;令牌。” 这是我的代码。
int main(){
char option_letter;
int lat1 = 0;
int lat2 = 0;
int long1 = 0;
int long2 = 0;
double distance = 0;
int totalDistance = 0;
enum direction_t {North = 'n', South = 's', East = 'e', West = 'w', Home = 'h', Quit = 'q'};
direction_t direction = static_cast<direction_t>(tolower(option_letter));
cout <<"Starting home at 0 North/South and 0 East/West, this program shows your position"<<endl;
cout<<"after travelling 10 miles: north, south, east or west, how far you are from home"<<endl;
cout<<"and how far you have traveled in total. You can jump home or quit." <<endl;
distance = sqrt(pow(lat2-lat1, 2) + pow(long2-long1, 2));
totalDistance = (lat1 + lat2) + (long1 + long2);
while (true){
cout <<"Location:"<< "longitude:" << long1<<" N"<< " latitude:" << lat1<<" E"<< " ""distance from home:"<< distance<< " ""distance traveled:"<< totalDistance<<endl;
cout <<"Choose direction to travel(0 to quit) N)orth, S)outh, E)ast, W)est, H)ome, Q)uit:"<<endl;
cin << direction_t;
switch(direction_t){
case 'n':{
distance = sqrt(pow(lat2-lat1, 2) + pow(long2-long1, 2) * 1);
totalDistance = (lat1+ lat2) + (long1 + long2);
break;
}
case 'e':{
distance = sqrt(pow(lat2-lat1, 2) + pow(long2-long1, 2) * 1);
totalDistance = (lat1 + long1) + (long1 + long2);
break;
}
}
}
return 0;
}
【问题讨论】:
-
cin << direction_t;此行导致错误,而不是您的演员表 -
我更改了运算符,因为我认为我使用了错误的运算符,但它仍然给我一个错误。 @Camwin