problem with arrays and structures

This is a discussion on problem with arrays and structures within the C++ Programming forums, part of the General Programming Boards category; hi i was writing a simple program i got from a tutorial that i was trying to modify. I have ...

  1. #1
    Registered User gell10's Avatar
    Join Date
    Jul 2003
    Posts
    46

    problem with arrays and structures

    hi i was writing a simple program i got from a tutorial that i was trying to modify. I have a structure that contains the name age and length of employees. I want the program to ask the user how many employees data they would like to add and then ask the information for the structures.
    Here is my code:
    Code:
    #include <fstream.h>
    
    struct employee //structure for employee data
    {
    	char name[80];
    	int age;
    	float length;
    };
    
    void main()
    {
    	int workers;
    	cout << "How many workers would you like to add? ";
    	cin >> workers;
    	employee worker[workers]; // <----- why cant this work
    	for(int member = 0; member < workers; member++)
    	{
    		cout << "Employee # " << member << ". \n";
    		cout << "Enter name:";
    		cin >> worker[member].name;
    		cout << "Enter age:";
    		cin >> worker[member].age;
    		cout << "Enter length:";
    		cin >> worker[member].length;
    	}
    	cout << "Thank you \n";
    }
    Thanks for anyones help

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    That particular syntax (type name[size]) only works for static sized arrays. It must know how much space to reserve at compile time. To dynamically change size, you need something like this:

    Code:
    int n; // non-const
    cin >> n;
    
    int* array = new int[n];
    ... Use it all you want ...
    delete[] array; // Since you created it dynamically, you are responsible for freeing it.
    *edit: I know this is a bit brief, and less of an explanation than you'd probably like, but I'm a bit too tired right now. It should point you in the right direction though. Look into pointers and the new/new[] and delete/delete[] operators.

    *edit: On further thought, you may want to look into the STL vector class ( www.cppreference.com is a good start ). It is much simpler and safer to use than the dynamically allocated arrays (e.g. no need to clean up memory after yourself).

    Cheers
    Last edited by Zach L.; 11-02-2003 at 10:08 PM.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    Registered User gell10's Avatar
    Join Date
    Jul 2003
    Posts
    46
    ok thanks i am trying to create a vectore and this is what i have
    Code:
    	employee vector <int> worker[workers];
    employee is the name of my structure as in the code above. I don't think that is how you create a vector in a structure. Is there another way? Am I making sense I am really tired too?

  4. #4
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    Try:

    employee vector <int> worker(workers);



    ITSA
    Socket Library!

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Try:

    employee vector <int> worker(workers);
    Are you sure that's right? I'm not sure I understand what he's asking but how would that work? Do you mean:

    Code:
    vector<employee> worker(workers);
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Originally posted by gell10
    ok thanks i am trying to create a vectore and this is what i have
    Code:
    	employee vector <int> worker[workers];
    employee is the name of my structure as in the code above. I don't think that is how you create a vector in a structure. Is there another way? Am I making sense I am really tired too?
    You create a vector like this:

    vector<typename> variable_name(initial_size, initial_value);

    Both the initial size and value are optional.

    E.g.:

    vector<employee> worker(workers);
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

  7. #7
    Registered User gell10's Avatar
    Join Date
    Jul 2003
    Posts
    46
    thank you all!
    it is
    vector<employee> worker(workers);

    that was awesome thanks

  8. #8
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    Sorry about the mispost. Curse of the lazy typist (ctl-c, ctl-v).



    ITSA
    Socket Library!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 26
    Last Post: 06-11-2009, 11:27 AM
  2. Problem with arrays structures sorting.
    By pitifulworm in forum C Programming
    Replies: 42
    Last Post: 02-09-2009, 11:31 AM
  3. Homework problem...structures or arrays?
    By tortan in forum C++ Programming
    Replies: 21
    Last Post: 08-30-2006, 01:26 AM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. errors with arrays and structures
    By ssjnamek in forum C++ Programming
    Replies: 4
    Last Post: 03-03-2002, 10:48 PM

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