-
Structure help
Code:
#include <iostream>
#include <vector>
#include <cstdlib>
#include <ctime>
#include <fstream>
using namespace std;
struct soldier {
soldier(void);
int agility;
int tech;
int gunskill;
};
soldier :: soldier(void)
{
agility = rand() % 31 + 10;
tech = rand() % 31 + 10;
gunskill = rand() % 31 + 10;
}
struct mforce {
mforce(void);
int w;
int z;
vector<soldier> millitant;
};
mforce :: mforce(void)
{
w = rand() % 1001 + 1;
z = rand() % 1001 + 1;
x = rand() % (w + 1) + z;
}
int main()
{
mforce batt[10];
srand(time(0));
printf("%d",sizeof(batt[2].millitant;) / sizeof(int));
system("pause");
return 0;
}
First, I defined what a soldier is. What I wanted to do was define what a soldier was, than create a structure, mforce (for Militant force) that would generate a random number of millitants. I went to a C++ forum, and somebody told me how to to do this: use a vector. The guy included it in the code, but he didn't tell me how the heck to use a vector. Help would be appreciated. If you look towards the end, what I'm trying to do is see how many millitants are in battalion 2. Add and delete code as you want if it'll help me understand what I'm aim to do. Thanks guys.
EDIT: Also, I want each battalion to generate a random number of millitants. I have somewhat a clue on how to do this, but as I stated above, things get confusing because of the Vector.
-
Your next step would be to do some investigation of the standard vector (std::vector). There is plenty of documentation around.
std::vector has a resize() member function that is used to resize the vector to a specified number of elements. It also has a size() member function that retrieves the current number of elements.
It would probably also help readability of your code if you spelt "militant" correctly.
-
You've got a semicolon inside your sizeof expression. But the whole sizeof idea is the wrong thing to do here anyway. You want to use ...millitant.size() rather than sizeof(...millitant) to get the number of items in a vector.
You should also stop writing (void) in several places. That is an old C throwback. In C++ you should just write ().