如何将 rand() 与枚举一起使用?

How do I use rand() with an enum?

你好,我刚开始学习 C++,我不明白枚举是如何工作的,我需要帮助知道如何让 rand() 与枚举一起工作

#include <iostream>
#include <cstring>
#include <string>
#include <cstdlib>

using namespace std;

int main()
{

    cout << "First part : Create an item. " << endl;
    int choice;

    int Fire = 25;
    int Water = 23;
    int Wind = 24;
    int Earth = 20;
    
    int WeaponNature = 0;
    
    enum NatureWeapons { Fire, Water, Wind, Earth}; // enum here if its wrong pls let me know ): 
    
   
    
    cout << "Enter the nature of weapon you want : " << endl;
    cout << " 1 - Fire " << endl;
    cout << " 2 - Water " << endl;
    cout << " 3 - Wind " << endl;
    cout << " 4 - Earth" << endl;
    cin >> choice;
    switch(choice)
    {
        case 1:
        cout << "You picked fire."
        cout << " Power : " << Fire << endl;
        WeaponNature = Fire;
        break;
        
        case 2:
        cout << "You picked water." << endl;
        cout << " Power : " << Water << endl;
        WeaponNature = Water;
        break;
        
        case 3:
        cout << "You picked wind nature." << endl;
        cout << " Power : " << Wind << endl;
        WeaponNature = Wind;
        break;
        
        case 4:
        cout << "You picked earth nature." << endl;
        cout << " Power : " << Earth << endl;
        WeaponNature = Earth;
        break;
        
        default:
        cout << "Incorrect input. Your weapon will be : " << rand() // this is where i need help 
        
    }
    


}

当 default: 在 switch() 中运行时,我希望它使用 rand() 选择随机性质,请帮忙:?

取自http://www.cprogramming.com/tutorial/enum.html

Printing Enums

You might wonder what happens when you print out an enum: by default, you'll get the integer value of the enum. If you want to do something fancier than that, you'll have to handle it specially.

您必须创建另一个开关块,将随机整数转换为枚举中的正确值。此外,通过 srand( time( NULL ) )

为您的随机数生成器初始化一个种子

也将兰特值分配给 WeaponNature。