Reputation: 13
Hello Im getting negative valhes when trying to output some variables to the screen.
Ive looked this up and in most cases its an uninitialized variable but I cant find anything wrong.
Saying I have too much code to text ratio but I really dont know how to reiterate so heres some filler.
#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;
}
Upvotes: 0
Views: 1157
Reputation: 6740
The problem is here:
int Stats[3];
int AtkStats[4];
You should do:
int Stats[3] = {0, 0, 0};
int AtkStats[4] = {0, 0, 0, 0};
Or as BlastFurnace pointed out in the comments (which I forgot about):
int Stats[3] = {}; // Initialize all elements to zero.
int AtkStats[4] = {};
In order to initialize the values. Right now they are just random junk, so when you assign, you get errors.
Upvotes: 1