vector won't copy correctly...

This is a discussion on vector won't copy correctly... within the C++ Programming forums, part of the General Programming Boards category; i have an assignment to read city names followed by populations into a vector of structure. the structure is city_data, ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    69

    vector won't copy correctly...

    i have an assignment to read city names followed by populations into a vector of structure. the structure is city_data, and consists of a 25 char string and an int. i pass the vector by reference to the getcity function, where the file input is performed.

    the file appears to be reading correctly, but my char string stops recieving values after the 10th character read from each line.

    upon exiting the function and returning to main, i attempt to output my vector again. this time, it doesn't give me anything useful! here's the sample output from the main function:
    ================================
    here is the output from the city variable:
    ≡║
    ================================
    and that is supposed to run through the entire string.

    i don't think i should be having this much trouble getting this input!

    oh, i also attached the data file if anybody perchance compiles this and wants to help

    Code:
    #include <iostream>
    #include <vector>
    #include <fstream>
    #include <cassert>
    #include <cctype>
    using namespace std;
    
    struct city_data
    {
    	char name[25];
    	int population;
    
    };
    
    int count_lines();
    void getcity(vector<city_data>& data);
    void zero_vector(vector<city_data>& data, int lines);
    
    int main()
    {
    	int initialize=0;
    	int size=0;
    	initialize=count_lines()/2;
    	vector<city_data> city(initialize);
    	
    	size=city.size();
    	zero_vector(city, initialize);
    	getcity(city);
    
    //debug*****************************
    	cout<<"here is the output from the city variable:" <<endl;
    	for (int k=0;k<initialize;k++);
    	{
    		for (int m=0;m<25;m++)
    		{
    			cout<<city[k].name[m];
    		}
    		cout<<endl;
    		cout<<city[k].population <<endl;
    	}
    //debug*****************************
    	system("PAUSE");
    
    	return 0;
    }
    
    void getcity(vector<city_data>& data)
    {
    	ifstream infile;
    	infile.open("lab3.txt");
    	assert(infile);
    	char tempchar;
    	
    	for (int i=0; !infile.eof();i++)
    	{
    	 	tempchar=0;
    		infile.peek();
    /*this is the for loop that seems to be giving me trouble.  it gets chars up to the 10th time, but exits the loop automatically afterwards.*/
    		for (int j=0;(infile.peek()!='\n')&&(j<25); j++)
    		{
    			infile.get(tempchar);
    			if (tempchar!='\n')
    			{
    				data[i].name[j]=tempchar;
    				cout<<tempchar;
    			}
    			
    		}
    		cout<<endl;
    		infile>>ws;
    		infile>>data[i].population;
    		infile>>ws;
    		for (int n=0;n<25&&n!='\n';n++)
    		{
    			cout<<data[i].name[n];
    		}
    		cout<<endl;
    		cout<<data[i].population <<endl;
    	}
    	infile.close();
    }
    
    int count_lines()
    {
    	ifstream infile;
    	infile.open("lab3.txt");
    	assert(infile);
    	char temp=0;
    	int number=0;
    	for (int i=0;!infile.eof(); i++)
    	{
    		if (!infile.eof())
    		{
    			infile.get(temp);
    			if(temp=='\n')
    			{
    				number++;
    			}
    		}
    	}
    	infile.close();
    	return number;
    }
    
    void zero_vector(vector<city_data>& data, int lines)
    {
    	
    	for	(int i=0;i<lines;i++)
    	{
    		for (int j=0;j<25;j++)
    		{
    			data[i].name[j]='0';
    
    		}
    		data[i].population=0;
    	}
    }
    Attached Files Attached Files

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    It seemed to work fine for me except:

    Code:
    int main()
    {
    	int initialize=0;
    	int size=0;
    	initialize=count_lines()/2;
    	vector<city_data> city(initialize);
    	
    	size=city.size();
    	zero_vector(city, initialize);
    	getcity(city);
    
    //debug*****************************
    	cout<<"here is the output from the city variable:" <<endl;
    	for (int k=0;k<initialize;k++);
    	{
    		for (int m=0;m<25;m++)
    		{
    			cout<<city[k].name[m];
    		}
    		cout<<endl;
    		cout<<city[k].population <<endl;
    	}
    //debug*****************************
    	system("PAUSE");
    
    	return 0;
    }
    Is the output supposed to be like:

    Summerside Estates0000000
    1645
    ?

    EDIT:

    I assume you meant to make this line:
    Code:
    void zero_vector(vector<city_data>& data, int lines)
    {
    	
    	for	(int i=0;i<lines;i++)
    	{
    		for (int j=0;j<25;j++)
    		{
    			data[i].name[j]='0';
    
    		}
    		data[i].population=0;
    	}
    }
    this:

    Code:
    data[i].name[j]='\0';
    So you don't get all those zeros outputted
    Last edited by JaWiB; 10-17-2003 at 06:55 PM.
    "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

  3. #3
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    You also have this in your for loop inside getcity:

    n!='\n'

    but n is the loop counter, not a char.
    Last edited by jlou; 10-17-2003 at 06:57 PM.

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    69
    wow, ok, that fixed it up for the inputting. but i'm still not having any luck with the city variable after the getcity function is done.

    this is the part that should output my city vector:
    Code:
    //debug*****************************
    	cout<<"here is the output from the city variable:" <<endl;
    	for (int k=0;k<initialize;k++);
    	{
    		for (int m=0;m<25;m++)
    		{
    			cout<<city[k].name[m];
    		}
    		cout<<endl;
    		cout<<city[k].population <<endl;
    	}
    //debug*****************************
    but it still gives me:

    here is the output from the city variable:
    ≡║
    0
    Press any key to continue . . .

    which doesn't look like the nicely formatted read i now perform inside the getcity function. is there something i'm missing in the pass by reference here?

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    You still have a semicolon after the first for loop:
    Code:
    for (int k=0;k<initialize;k++);
    Don't know if that solves it...
    "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
    Registered User
    Join Date
    Oct 2002
    Posts
    69
    gee, i never noticed that! sure enough, it works now. thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to copy a C struct to a C++ class?
    By Ptbamboo in forum C++ Programming
    Replies: 1
    Last Post: 02-21-2009, 01:11 PM
  2. Copying constant amount of data
    By TriKri in forum C++ Programming
    Replies: 16
    Last Post: 07-12-2008, 06:32 AM
  3. 'Passing by Refrence for Efficiency', Copy Constructors?
    By Zeusbwr in forum C++ Programming
    Replies: 4
    Last Post: 10-23-2004, 07:11 AM
  4. copy constructor
    By paperbox005 in forum C++ Programming
    Replies: 4
    Last Post: 08-20-2004, 01:43 PM
  5. Copy constructors and operator=()
    By filler_bunny in forum C++ Programming
    Replies: 13
    Last Post: 08-25-2003, 07:43 AM

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