Trying to make a little space exploration game, and I'm just testing some planet data stuff, and it won't work. I don't get any errors until I compile it, then I get an error message saying that "vector subscript" is out of range.

main.cpp:

#include <iostream>
#include <conio.h>
#include <string>
#include <vector>

using namespace std;

//Declarations:
void seed();
int getrandomint(int from, int to);
bool getrandomresult(int probability);
struct planet;
struct star;
struct starsystem;
void createuniverse();


//Stats:
vector<starsystem> systems;
bool hasseeded = false;
bool running = true;
#include "functions.h"

int main(void)
{
    seed();
    createuniverse();

    int prob;

    while (running)
    {
        string input;
        cout << "Welcome to space console 143. What do you want to do?" << endl;
        cin >> input;

        if (input == "requestsystemdata")
        {
            int system;
            cout << "Specify system: ";
            cin >> system;

            cout << "Data for system " << system << ":" << endl;
            cout << "Planet count: " << systems[system].planetcount << endl;
            cout << "Diameter: " << systems[system].diameter << " million km" << endl;
            cout << "Sun data: " << endl << "Sun core temp: " << systems[system].sun.coretemp << "million degrees" << endl;
            cout << "Sun age: " << systems[system].sun.age << "billion years" << endl << endl;


        }
        else
        {
            cout << "Unknown command." << endl << endl;
        }

    }

}

functions.h:

#include <string>
#include <ctime>
#include <cstdlib>

using namespace std;


//Randimization:

void seed()
{
    srand(time(0));
}

int getrandomint(int from, int to)
{
    return rand() % ((to + 1) - from) + from;

}

bool getrandomresult(int probability)
{
    int rndnumber = getrandomint(1, 100);
    if (rndnumber <= probability)
    {
        return true;
    }
    else
    {
        return false;
    }
}

//Universe:

struct planet
{
    bool haslife;
    string name;
    long population;
    int intelligence;
    int radprotection;
    int mineralworth;
    double orbitaldistance;
    double distancefromearth;
    double gravity;
    int diameter;
    int surfacetemp;
    double massbase;
    int massexp;
    int rating;
};

struct star
{
    int diameter;
    int surfacetemp;
    int coretemp;
    int type;
    int age;
};

struct starsystem
{
    string name;
    int planetcount;
    planet planets[20];
    star sun;
    int diameter;

};

void createuniverse()
{
    systems.reserve(10000);
    for (int index = 1; index <= 10000; index++)
    {
        systems[index].planetcount = getrandomint(1, 20);
        systems[index].diameter = (getrandomint(1, 30) * systems[index].planetcount);
        systems[index].sun.age = getrandomint(1, 20);
        systems[index].sun.coretemp = getrandomint(1, 30);
        systems[index].sun.diameter = getrandomint(1, 2000);

    }


}

vector::reserve does NOT initialize the vector with the structures, reserve() only allocates memory for them. Your program still must call vector::push_back() or vector::insert()

The second problem with your function is that the first element of all arrays (vector is an array) is 0, not 1. And the max element index is 10000-1 So the loop needs to be modified as shown in my code below. I did not check the rest of your program for this problem, I figured you can do that yourself.

Finally, you should be into the habit of declaring const int to refer to constant values that are used throughout the program. 10000 is a good candidate for that because if you want to change it to some other value you will have to change it in quite a few places. Declaring const int at the beginning of the program makes all that a lot simpler and error free.

cont int maxSize = 10000;
void createuniverse()
{
    systems.reserve(maxSize);
    for (int index = 0; index < maxSize; index++)
    {
        starsystem sys;
        systems.push_back(sys);
        systems[index].planetcount = getrandomint(1, 20);;
        systems[index].diameter = (getrandomint(1, 30) * systems[index].planetcount);
        systems[index].sun.age = getrandomint(1, 20);
        systems[index].sun.coretemp = getrandomint(1, 30);
        systems[index].sun.diameter = getrandomint(1, 2000);

    }


}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.