[英]How do i change enum to char? I'm trying to cast it but it gives me an error
所以我應該制定一個旅行計划來幫助旅行者知道他們在哪里(北,南,東,西)。 我們應該為菜單選項創建枚舉數據類型,而不是使用字符選項,用戶選擇他們要去的方向,它告訴總距離和離家的距離。 我嘗試強制轉換枚舉,但出現此錯誤:預期的主表達式之前; 令牌。”這是我的代碼。
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 的右向箭頭 (>>)
有幾個問題:
首先cin
應該使用>>
而不是<<
。
那么direction_t
是一個類型, direction
是一個變量。
最后你不能直接讀入direction_t
類型的變量,除非你重載cin 的>>
操作符。
看起來強制轉換實際上是正確實現的,但是,您從未初始化option_letter
,因此您沒有將任何數據傳遞給強制轉換。 在您的問題的上下文中,您應該通過cin >> option_letter
將數據從cin
傳遞到option_letter
,然后將其轉換為direction_t
類型。
您收到錯誤的原因是因為您在應該使用direction_t
時使用了direction
(這發生在程序中的多個位置),並且在您應該使用>>
時將<<
運算符與cin
一起使用操作員。
你的代碼應該是這樣的——
#include <cctype>
#include <iostream>
#include <math.h>
using namespace std;
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 >> option_letter;
direction_t direction = static_cast<direction_t>(tolower(option_letter));
switch(direction){
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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.