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