Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

C++ Help, Array error

Name: Anonymous 2007-06-07 12:06 ID:+O+EKF2S

Hey guys this compiles but when i build the selected number of knights only the first one gets a random number and the rest get -801208102 or something?
// knight values is a struct.
knightValues* CreateKnights2(int& numberOfKnights2)
{
    system("CLS");
    srand( (unsigned)time( NULL ) );

    numberOfKnights2 = 0;

    std::cout << "Enter how many Knights you would like to create: ";
    std::cin >> numberOfKnights2;

    knightValues* myCreatedKnights2 = new knightValues[numberOfKnights2];

    for (int i = 0; i < numberOfKnights2; i++){
        myCreatedKnights2[i].health = rand() % 100 + 100;
        myCreatedKnights2[i].armour = rand() % 200 + 150;

    return myCreatedKnights2; // this is being assigned to army in the switch
    }
}
------
knightValues* army;        // army is the pointer to the array of knights
int nKnights;            // this is the number of knights

------
switch (option)
      {
          case 1 : army = CreateKnights(nKnights); break;

Name: Anonymous 2007-06-07 13:21 ID:zYqviSS6

Use the stl faggot. They created it for a reason you know.

[code]// knight values is a struct.
vector<knightValues> CreateKnights2()
{
    system("CLS");
    srand( (unsigned)time( NULL ) );

    numberOfKnights2 = 0;

    std::cout << "Enter how many Knights you would like to create: ";
    std::cin >> numberOfKnights2;

    vector<knightValues> myCreatedKnights2(numberOfKnights2);

    for (vector<knightValues>::iterator it = myCreatedKnight2.begin(); it != myCreatedKnight2.end(); ++it){
        it->health = rand() % 100 + 100;
        it->armour = rand() % 200 + 150;
    }
    return myCreatedKnights2; // this is being assigned to army in the switch
}
------
vector<knightValues> army;        // army is the vector of knights
int nKnights;            // this is the number of knights

------
switch (option)
      {
          case 1:
              army = CreateKnights();
              nKnight = army.size();
              break;

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List