【问题标题】:How do i change enum to char? I'm trying to cast it but it gives me an error我如何将枚举更改为字符?我正在尝试投射它,但它给了我一个错误
【发布时间】: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 &lt;&lt; direction_t; 此行导致错误,而不是您的演员表
  • 我更改了运算符,因为我认为我使用了错误的运算符,但它仍然给我一个错误。 @Camwin

标签: c++ enums char


【解决方案1】:

有几个问题:

首先cin 应该使用&gt;&gt; 而不是&lt;&lt;

那么direction_t是一个类型,direction是一个变量。

最后,你不能直接读入direction_t 类型的变量,除非你为cin 重载&gt;&gt; 运算符。

【讨论】:

  • 我确实更改了运算符,但它仍然给我同样的错误。 @迈克尔·马恩
  • 你必须修复我提到的所有错误。
  • 当我使用“direction”而不是“direction_t”时,它说“无法将 std::istream}lvalue 绑定到 'std::basic_istream&&'。@Michael Mahn
【解决方案2】:

" ""离家的距离:"

你忘了删除额外的引号

除此之外,cin 需要右向箭头 (>>)

【讨论】:

  • 我更改了运算符并删除了多余的引号,但它仍然给我同样的错误。 @Cx Vercility
  • 附加引号是无害的。相邻的字符串文字被连接起来,因此文本等同于” distance from home:”
【解决方案3】:

看起来转换实际上是正确实现的,但是,您从未初始化option_letter,因此您没有将任何数据传递给转换。在您的问题的上下文中,您应该通过cin &gt;&gt; option_letter 将数据从cin 传递给option_letter,然后将其转换为direction_t 类型。

您收到错误的原因是因为您在应该使用 direction 时使用了 direction_t(这发生在程序中的多个位置),并且正在使用 &lt;&lt; 运算符和 @ 987654329@ 当您应该使用 &gt;&gt; 运算符时。

您的代码应如下所示--

#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;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-02
    • 1970-01-01
    • 2019-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多