Structure help

This is a discussion on Structure help within the C++ Programming forums, part of the General Programming Boards category; Code: #include <iostream> #include <vector> #include <cstdlib> #include <ctime> #include <fstream> using namespace std; struct soldier { soldier(void); int agility; ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    1

    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.
    Last edited by Suberbored21; 03-01-2009 at 01:39 PM.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,295
    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.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,304
    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 ().
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  2. Replies: 5
    Last Post: 02-14-2006, 08:04 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 09:52 AM
  5. C structure within structure problem, need help
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-30-2001, 04:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21