这是我第一次进行演员表演和枚举,所以我在尝试演员时遇到了这个问题。
假设我有以下枚举
enum Region
{
A,
B,
C,
D,
};
和一个读取具有数据列表
的文件的函数void loadData(castle &al3a, queue &q, float &c1, float &c2, float &c3)
{
int i = 0;
ifstream myfile;
int n = 0;
myfile.open("data.txt");
if (myfile.fail())
{
cout << "Error" << endl;
myfile.close();
}
else {
string line;
while (!myfile.eof())
{
n++;
getline(myfile, line);
}
}
myfile.close();
string line2;
int T_Health;
int T_Attack_N_Enemies;
int T_Fire_Power;
myfile.open("data.txt");
myfile >> T_Health >> T_Attack_N_Enemies >> T_Fire_Power;
getline(myfile, line2);
myfile >> c1 >> c2 >> c3;
getline(myfile, line2);
int a,b,c,d,e,f;
char g;
enemy x;
for (int i = 0; i < n - 3; i++)
{
myfile >> a >> b >> c >> d >> e >> f >> g;
x.ID = a;
x.type = static_cast<Type>(b);
x.time_step = c;
x.Health = d;
x.fire_power = e;
x.reload_power = f;
x.region = static_cast<Region>(g);
enqueue(q, x);
getline(myfile, line2);
}
for (int i = 0; i < 4; i++)
{
al3a.towers[i].Health = T_Health;
al3a.towers[i].attackEnemies = T_Attack_N_Enemies;
al3a.towers[i].firePower = T_Fire_Power;
al3a.towers[i].Tregion = (Region)i;
}
// shielded enemies have higher priority than ordinary enemies
/*Priority(Shielded Enemy) = C1 * (EnemyFirePower / EnemyDistance) + C2 / (EnemeyRemaining time to shoot + 1) + EnemyHealth * C3*/
}
数据文件看起来应该像上传的照片一样。
每一行代表一个敌人,第一列是ID,第二列是类型,第三列是time_step,第四列是Health,反正最后一列是区域。
以某种方式成功实现了队列,但是当我尝试cout<<enemyy.region
时,我最终得到了65或66 ..等等,指出了A和B的ASCII代码..如何将数组转换为打印出来的A或者乙
P.S:类型也是一个与前面提到的问题相同的枚举
照片:
答案 0 :(得分:0)
如果你的char变量包含'a',它确实包含一个ascii值(97)。除非你知道自己在做什么,否则你不应该将这样的价值投射到枚举上。 C ++中的枚举通常存储在8位(与char的大小相同)(我不确定这是否标准化)所以你的x.region包含与char g相同的值(ASCII)(这就是为什么std :: cout打印ASCII值。)
只有在整数类型在枚举范围内时才进行枚举(例如,如果枚举包含4个元素,则只应该转换范围0-3的整数值)。
如果您对枚举类型行为感兴趣,请查看this answer。
此外,您应该查看关于将字符映射到枚举类类型的c++11 enum classes和this答案,这可能就是您要查找的内容。