Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在rand()函数中使用枚举值_C++_Random_Enums - Fatal编程技术网

C++ 如何在rand()函数中使用枚举值

C++ 如何在rand()函数中使用枚举值,c++,random,enums,C++,Random,Enums,如何使用生成随机枚举变量的rand函数?这就是我目前所拥有的(它吐出了垃圾数字): 严格来说,可能是不可能的。 你可以 检查哪些值具有整数形式的枚举 然后您将能够生成一个整数 对它做一个模运算(%) parse to enum[编辑-不必在C++中将其解析为enum] 例如 #包括 #包括 使用名称空间std; int main() { 枚举游戏{旗帜,炸弹,元帅,将军,上校,少校, 上尉,中尉,少校,间谍}; int maxEnumValue=12; int randpoice=rand()%

如何使用生成随机枚举变量的rand函数?这就是我目前所拥有的(它吐出了垃圾数字):


严格来说,可能是不可能的。 你可以

  • 检查哪些值具有整数形式的枚举
  • 然后您将能够生成一个整数
  • 对它做一个模运算(%)
  • parse to enum[编辑-不必在C++中将其解析为enum]
  • 例如

    #包括
    #包括
    使用名称空间std;
    int main()
    {
    枚举游戏{旗帜,炸弹,元帅,将军,上校,少校,
    上尉,中尉,少校,间谍};
    int maxEnumValue=12;
    int randpoice=rand()%maxEnumValue;
    cout您可以使用
    rand()%12
    ,这将在0-11范围内创建一个随机数:

    int rand_piece = gamePieces[rand() % 12];
    

    代码中有更多错误-请尝试以下操作:

    #include <stdlib.h>
    
    const int numPieces = 11;
    
    enum pieces { FLAG = 'F', BOMB = 'B', MARSHAL = '1', GENERAL = '2', COLONEL = 
                  '3', MAJOR = '4', CAPTAIN = '5', LIEUTENANT = '6', SERGEANT = 
                  '7', MINER = '8', SPY = 'S' };
    
    int gamePieces[numPieces] = { FLAG, BOMB, MARSHAL, GENERAL, COLONEL, MAJOR, 
                           CAPTAIN, LIEUTENANT, SERGEANT, MINER, SPY };
    
    int rand_piece = gamePieces[rand() % numPieces];
    
    尝试:


    请尝试
    gamePieces[rand()%12]
    您的代码充满了错误。错误的SEGEANT,MINOR,rand_piece中缺少结束括号,枚举数为11,而大小声明为12。以下是我由此得到的错误:------------------------------------------ex.cpp:88:44:错误:从“int*”到“int”的转换无效[-fpermissive]int rand_piece=gamePieces[rand()%12];@Gbrandonconner您的代码中还有一些错误。请参阅更新的答案。
    int rand_piece = gamePieces[rand() % 12];
    
    #include <stdlib.h>
    
    const int numPieces = 11;
    
    enum pieces { FLAG = 'F', BOMB = 'B', MARSHAL = '1', GENERAL = '2', COLONEL = 
                  '3', MAJOR = '4', CAPTAIN = '5', LIEUTENANT = '6', SERGEANT = 
                  '7', MINER = '8', SPY = 'S' };
    
    int gamePieces[numPieces] = { FLAG, BOMB, MARSHAL, GENERAL, COLONEL, MAJOR, 
                           CAPTAIN, LIEUTENANT, SERGEANT, MINER, SPY };
    
    int rand_piece = gamePieces[rand() % numPieces];
    
    enum pieces { FLAG, BOMB, MARSHAL, GENERAL, COLONEL, MAJOR, 
                  CAPTAIN, LIEUTENANT, SERGEANT, MINER, SPY,
                  MAX_PIECE };
    
    int rand_piece = rand() % MAX_PIECE;
    
    int main()
    {
        enum pieces {
            FLAG = 'F', BOMB = 'B', MARSHAL = '1', GENERAL = '2', COLONEL =
            '3', MAJOR = '4', CAPTAIN = '5', LIEUTENANT = '6', SERGEANT =
            '7', MINER = '8', SPY = 'S'
        };
    
        int gamePieces[11] = { FLAG, BOMB, MARSHAL, GENERAL, COLONEL, MAJOR,
            CAPTAIN, LIEUTENANT, SERGEANT, MAJOR, SPY };
    
    
        std::cout << char(gamePieces[rand() % 11]) << std::endl;
        std::cout << char(gamePieces[rand() % 11]) << std::endl;
        std::cout << char(gamePieces[rand() % 11]) << std::endl;
        std::cout << char(gamePieces[rand() % 11]) << std::endl;
        std::cout << char(gamePieces[rand() % 11]) << std::endl;
    
        return 0;
    }
    
    7
    4
    4
    B
    6