Array to vector

Pages: 12
I was converting some arrays in my code to vectors.
But I dont know how I should convert this.
Could someone help me out :D?

The code:
Its about: " member_vector[amountofmembers] = member; "


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void store_data (vector <Member> & member_vector , int& amountofmembers)
{
    //Precondities
    assert (true);
    ifstream infile ("Members.txt");

    Member member;

    for (;read_member_struct_from_file(infile, member);amountofmembers++)
    {
        member_vector[amountofmembers] = member;
    }
    if (amountofmembers==0)
    {
        cout << "There are no members found!" << endl;
    }
    else
    {
        cout << "From " << amountofmembers << " members their data are known." << endl;
    }
}
There's nothing to convert, element access is the same with vectors.
Edit: but of course only once they exist as a result of calling resize or push_back/insert.
Last edited on
Something like this I guess
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void store_data (vector <Member> & member_vector)
{
    //Precondities
    assert (true);
    ifstream infile ("Members.txt");

    Member member;

    while (read_member_struct_from_file(infile, member))
    {
        member_vector.push_back(member);
    }
    if (member_vector.empty())
    {
        cout << "There are no members found!" << endl;
    }
    else
    {
        cout << "From " << member_vector.size() << " members their data are known." << endl;
    }
}
Thanks for response :D.
Im gonna try it
If member_vector already has the required number of elements in it, then the code will work as written and will just replace the existing values.

But seeing as you're filling up the vector from empty, you want to use the vector::push_back() method. Then your line 11 becomes:

member_vector.push_back(member);

Cheers,
Jim
I got this now:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void store_data (vector <Member> & member_vector)
{
    //Precondities
    assert (true);
    ifstream infile ("Members.txt");

    Member member;

    while (read_member_struct_from_file(infile, member))
    {
        member_vector.push_back(member);
    }
    if (member_vector.empty())
    {
        cout << "There are no members found!" << endl;
    }
    else
    {
        cout << "From " << member_vector.size() << " members their data are known." << endl;
    }
}


But its not loading my members.txt :(
When i execute the program, i press 1 to show members.txt
But it shows nothing..
Gonna need to see the rest of your code, in particular the definition of read_member_struct_from_file()

Jim
This is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
bool read_member_struct_from_file ( ifstream& file, Member& member )
// precondition:  the ifstream is already opened for reading
// postcondition: if no data left in file (i.e. encountering EOF), then 'false' is returned as boolean function-value
//                else : 1) the 'member'-reference parameter will be filled with file-data for one member-struct
//                   and 2) 'true' will be returned as boolean function-value
{
	getline ( file, member.surname );
	if ( !file )				// in case of encountering EOF-token
		return false ;

	getline ( file, member.forename    );
	getline ( file, member.street_name );
	getline ( file, member.postcode    );
	getline ( file, member.city_name   );

	char birth_year_string [6] ; 		// auxiliary variable!
	file.getline( birth_year_string, 6 ) ;	// use of 'getline' not possible to read in a number!
	member.year_of_birth = atoi ( birth_year_string ) ;	  //  .. atoi(..)  :   conversion array -> int
                           // atoi ( ...) only works with c-strings and not with 'string-class'-variables

	string token_string ;			// auxiliary variable!
	getline ( file, token_string );	// use of 'getline' not possible to read in an 'enum'- nor a 'bool'-value

	char token = token_string [ 0 ] ;	// only first token!
	if ( token == 'M' )					// also possible: if ( token_string [ 0 ] == 'M' )
		member.gender = Male ;
	else
		member.gender = Female;

	getline( file, token_string );
	token = token_string [ 0 ] ;		// only first token!

	member.paid =  ( token == 'T') ;	// assigning bool-value
	return true ;
}


I have added some conditions to make it more clear.
Last edited on
Did it work before you change to use vectors?
Yes it did.
What exactly does it say? Nothing at all, or "There are no members found!"

Are you sure you only changed the store_data() method and nothing else?
Yes i am sure that i did not change anything else.
It correctly shows the output table, but the table is empty.
The data from members.txt should be in it.
What output table?
Do you mean "There are no members found!"?
You didn't delete/rename you Members.txt file, did you?

Try checking the status of infile before you try reading from it; make sure you are actually opening the file.
I don't know what the output table is. Did it print "There are no members found!" or not?

You must have change the calling code to not pass in the amountofmembers parameter. Did you change the rest of your code so that you use the vector size() function instead of that other variable?
Output table:
http://gyazo.com/b6274efb8830bc67dcf97c2cef2fae60

I have not deleted members.txt
It is still in the same folder as my .cpp is.
Based on your code, that shouldn't be possible. store_data always prints a message at the end, which I don't see in the screenshot. So you're probably not calling the function at all (or you forgot to recompile).
Edit: by the way, store_data is a strange name for a function that loads data from a file. While one could argue that it stores it in the vector, this is against conventions.
Last edited on
Output table:
http://gyazo.com/b6274efb8830bc67dcf97c2cef2fae60


I'm not seeing any "There are no members found!" or "From <x> members their data are known" output. I'd say you're not calling the function you think you are. Can you post the rest of the code that calls store_data().

Jim
o. Sorry.
This is the output:
http://gyazo.com/8f432a5f0b7df26525253da853d80c52

And this is the code for the function to display the data from members.txt

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
void display_members(vector <Member> member_vector, int amountofmembers)
{
    cout << left << endl
         << setw ( 14 ) << "Surname"
         << setw ( 10 ) << "Forename"
         << setw ( 17 ) << "Street+nr"
         << setw ( 10 ) << "Postcode"
         << setw ( 13 ) << "Town"
         << setw (  6 ) << "Born"
         << setw (  5 ) << "M/F"
         << setw (  4 ) << "Paid" << endl
         << "_________________________________________________________________________________" << endl;


    for(int i=0 ; i < amountofmembers ; i++)
    {
        cout << left
             << setw ( 14 ) << member_vector[i].surname
             << setw ( 10 ) << member_vector[i].forename
             << setw ( 17 ) << member_vector[i].street_name
             << setw ( 10 ) << member_vector[i].postcode
             << setw ( 13 ) << member_vector[i].city_name
             << setw (  6 ) << member_vector[i].year_of_birth;
        if(member_vector[i].gender)
        {
            cout << left << setw ( 5 ) << "M";
        }
        else
        {
            cout << left << setw ( 5 ) << "F";
        }

        if(member_vector[i].paid)
        {
            cout << left << setw ( 4 ) << "Yes" << endl;
        }
        else
        {
            cout << left << setw ( 4 ) << "No" << endl;
        }
    }
}


EDIT:
GOT IT :D.

i changed this:
amountofmembers -> member_vector.size()
Last edited on
Another question:
how could i make a function that sorts the data on name or postal code for example?
I have to index them first?

edit: sorry, double post...
Last edited on
Pages: 12