Help with integer array
Jul 10, 2014 at 12:38pm UTC
Hi, I'm trying to make it so every time the user types an input, it stores that input in the proper variable, and moves on to the next one in the array. It's kind of hard to explain without looking at the code so here it is:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
void MainChar::CharacterCreation()
{
int statPoints = 20;
int health = 0;
int magicDamage = 0;
int magicResist = 0;
int physicalResist = 0;
int physicalDamage = 0;
int magicOffMastery = 0;
int physicalOffMastery = 0;
int magicDefMastery = 0;
int physicalDefMastery = 0;
// SETS STATS AND THIER RESPECTIVE ARRAY PLACEMENT
int statArray[9];
statArray[0] = health;
statArray[1] = magicDamage;
statArray[2] = magicResist;
statArray[3] = physicalResist;
statArray[4] = physicalDamage;
statArray[5] = magicOffMastery;
statArray[6] = physicalOffMastery;
statArray[7] = magicDefMastery;
statArray[8] = physicalDefMastery;
std::string stats[9];
stats[0] = "Health : " ;
stats[1] = "Magic Damage : " ;
stats[2] = "Magic Resist : " ;
stats[3] = "Physical Resist : " ;
stats[4] = "Physical Damage : " ;
stats[5] = "Magic Offensive Mastery : " ;
stats[6] = "Physical Offensive Mastery : " ;
stats[7] = "Magic Defensive Mastery : " ;
stats[8] = "Physical Defensive Mastery : " ;
int statString = 0;
int statInt = 0;
while (statPoints > 0)
{
std::cout << "*******************************************************************************" << std::endl;
std::cout << "* *" << std::endl;
std::cout << "* CHARACTER CREATION *" << std::endl;
std::cout << "* *" << std::endl;
std::cout << "*******************************************************************************" << std::endl;
std::cout << " Health : " << health * 10 << std::endl;
std::cout << std::endl;
std::cout << " Magic Damage : " << magicDamage << std::endl;
std::cout << std::endl;
std::cout << " Magic Resist : " << magicResist << std::endl;
std::cout << std::endl;
std::cout << " Physical Resist : " << physicalResist << std::endl;
std::cout << std::endl;
std::cout << " Physical Damage : " << physicalDamage << std::endl;
std::cout << std::endl;
std::cout << " Magic Offensive Mastery : " << magicOffMastery << std::endl;
std::cout << std::endl;
std::cout << " Physical Offensive Mastery : " << physicalOffMastery << std::endl;
std::cout << std::endl;
std::cout << " Magic Defensive Mastery : " << magicDefMastery << std::endl;
std::cout << std::endl;
std::cout << " Physical Defensive Mastery : " << physicalDefMastery << std::endl;
std::cout << "*******************************************************************************" << std::endl;
std::cout << "STAT POINTS: " << statPoints << std::endl;
std::cout << stats[statString];
std::cin >> statArray[statInt];
statPoints -= statArray[statInt];
++statString;
++statInt;
}
What I'm trying to say is, I want the user to modify the value of the integer stored in a certain array location, not enter an array location.
As you might notice, I'm trying to have the user change the value of health, which is stored in statArray[statInt], which equates to statArray[0], then I ++statInt. The idea was I'd be able to have the user input all his stats one at a time. Instead of my intention, whats happening is it's taking the user input as a reference to the array slot. (statArray[0], statArray[1]) etc. Instead of the stat associated with that array slot.
Last edited on Jul 10, 2014 at 1:19pm UTC
Jul 10, 2014 at 1:19pm UTC
statArray should contain points to the stats:
1 2 3 4 5 6 7 8 9 10
int *statArray[9];
statArray[0] = &health;
statArray[1] = &magicDamage;
statArray[2] = &magicResist;
statArray[3] = &physicalResist;
statArray[4] = &physicalDamage;
statArray[5] = &magicOffMastery;
statArray[6] = &physicalOffMastery;
statArray[7] = &magicDefMastery;
statArray[8] = &physicalDefMastery;
and line 69 changes to:
std::cin >> *statArray[statInt];
For what it's worth, a better way to do this might be a Stat class that contains the name and value of a statistic, and then create an array of these.
Jul 10, 2014 at 1:26pm UTC
Oohooohggooo
You are a god sent my friend. Thank you. I'll consider the tip, sounds like a smart move
Topic archived. No new replies allowed.