你好我在尝试将一些变量输出到屏幕时得到负值。
我看了这个,在大多数情况下它是一个未初始化的变量,但我找不到任何错误。
说我有太多的代码到文本比例,但我真的不知道如何重申,所以继承人有些填充。
#include <iostream>
#include <string>
using namespace std;
int main()
{
int Input;
int WepType;
string Weapon;
string WeaponType;
int Stats[3];
int AtkStats[4];
cout << "Pick your weapon.\n" << endl;
cout << "{===================================================}\n";
cout << "[ ]\n";
cout << "[ Iron Longsword Sharp Fishing Spear ]\n";
cout << "[ 1 ]\n";
cout << "[ ]\n";
cout << "[ Ornate Hunting Bow ]\n";
cout << "[ ]\n";
cout << "{===================================================}\n";
//Weapon Selection
cin >> Input;
if(Input == 1)
{
WepType = 1;
Weapon = "Iron Longsword";
WeaponType = "OneHanded";
Stats[0] = Stats[0] + 10;
Stats[1] = Stats[1] + 0;
Stats[2] = Stats[2] + 0;
AtkStats[0] = AtkStats[0] + 10;
AtkStats[1] = AtkStats[1] + 0;
AtkStats[2] = AtkStats[2] + 0;
AtkStats[3] = AtkStats[3] + 0;
cout << "Weapon = " << Weapon << endl;
cout << "Weapon Type = " << WeaponType << endl;
cout << "Health = " << Stats[0] << endl;
cout << "Physical = " << AtkStats[0] << endl;
cout << "Light = " << AtkStats[1] << endl;
cout << "Dark = " << AtkStats[2] << endl;
cout << "Temporal = " << AtkStats[3] << endl;
}
return 0;
}
答案 0 :(得分:1)
问题在于:
int Stats[3];
int AtkStats[4];
你应该这样做:
int Stats[3] = {0, 0, 0};
int AtkStats[4] = {0, 0, 0, 0};
或者正如BlastFurnace在评论中指出的那样(我忘了):
int Stats[3] = {}; // Initialize all elements to zero.
int AtkStats[4] = {};
为了初始化值。现在它们只是随机垃圾,所以当你分配时,你会得到错误。