0

I'm trying to make it so every time the user inputs a value, it's stored in an array in a place that represents a stat. Hard to explain so here's the code:

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;
}

}

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.

2 Answers 2

1

To achieve what you want to do you could use a union:

union Stat {
  struct {
   int health;
   int magicDamage;
   int magicResist;
   // ...
  };
  int array[3];
};

int main() {
  Stat stat;
  stat.array[1] = 42;
  cout<<stat.magicDamage<<endl; // should give 42
};

However a better solution would be to use a map:

map<string,int> stat;
const char *keys[] = {"health","magicDamage","magicResist"};

int main() {
  for (int i=0;i<3; ++i) {
    cout<<"enter "<<keys[i]<<endl;
    cin>>stat[keys[i]];
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Change statArraydefinition to use pointers instead of values

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 update next lines:

std::cout << *stats[statString]  ;
std::cin  >> *statArray[statInt] ;

statPoints -= *statArray[statInt];

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.