Inputing data of a struct member(declared as an array) from a file

This is a discussion on Inputing data of a struct member(declared as an array) from a file within the C++ Programming forums, part of the General Programming Boards category; Is this not allowed or did I do something wrong? Please help! For now, I'm just trying to read the ...

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

    Inputing data of a struct member(declared as an array) from a file

    Is this not allowed or did I do something wrong? Please help!

    For now, I'm just trying to read the moviedescription from the file "inmoviedata.txt." The first 8 lines contain the name of 8 movies which I am trying to output to the display screen via the for() loop at the bottom of my code below. Instead of declaring each movie and other characteristics of the movie line by line using dot notation, I was wondering if it's okay to use an array
    and for loop like I've trying to do.

    Code:
    /*
    Trying to read the first 8 lines from a file which contains a movie
    
    */
    
    #include<iostream>
    #include<fstream>
    #include<string>
    
    using namespace std;
    
    
    
    int main()
    {
       ifstream inmoviedata; //file containg movie data
       inmoviedata.open("moviedata.txt"); //opening the file with movie data
    
       
       struct Videocollection
    	{   
    		string nameofmovie[7];
            string moviedescription[7];
    		string castmember1[7];
    		string castmember2[7];
    		int yearreleased[7];
    		int lengthofvideo[7];
    		//omitting [7] because all movies in the database are in color
            string blackandwhiteorcolor;
    	};
    	
    	Videocollection eightmoviedatabase;
    	int i;
    	for(i=0; i<=7;i++)
    	{
        getline(inmoviedata,nameofmovie[i]);//'nameofmovie' undeclared, why?
        eightmoviedatabase.nameofmovie[i]=nameofmovie[i];
        inmoviedata>>nameofmovie[i];
        cout<<nameofmovie[i];
        }
        
        
    
    
    
    
    
    	return 0;
    }

  2. #2
    Me want cookie! Monster's Avatar
    Join Date
    Dec 2001
    Posts
    680

    Re: Inputing data of a struct member(declared as an array) from a file

    Originally posted by m712
    getline(inmoviedata,nameofmovie);//'nameofmovie' undeclared, why?
    Because it's declared within the Videocollection structure. You need eightmoviedatabase.nameofmovie[i].

    By the way, you need to increase the array size to 8. It should be something like this.

    Code:
    struct Video
    {   
       string nameofmovie;
       string moviedescription;
       string castmember1;
       string castmember2;
       int yearreleased;
       int lengthofvideo;
       string blackandwhiteorcolor;
    };
    
    Video eightmoviedatabase[8];

  3. #3
    Registered User
    Join Date
    Oct 2002
    Posts
    33
    Thanks, I corrected the array problem...but I still am getting compile time errors even with the eightmoviedatabase.moviedata due to the three member selectors in the for loop;
    it says "request for member `nameofmovie' in `eightmoviedatabase', which is of non-aggregate type `main()::Videocollection[8]"

    Code:
    /*
    Trying to read the first 8 lines from a file which contains a movie
    
    */
    
    #include<iostream>
    #include<fstream>
    #include<string>
    
    using namespace std;
    
    
    
    int main()
    {
       ifstream inmoviedata; //file containg movie data
       inmoviedata.open("moviedata.txt"); //opening the file with movie data
    
       struct Videocollection
    	{   
    		string nameofmovie;
            string moviedescription;
    		string castmember1;
    		string castmember2;
    		int yearreleased;
    		int lengthofvideo;
    		string blackandwhiteorcolor;
    	};
       
    	
    	Videocollection eightmoviedatabase[8];
    	int i;
    	for(i=1; i<=8;i++)
    	{
        getline(inmoviedata,eightmoviedatabase.nameofmovie[i]);
        
        inmoviedata>>eightmoviedatabase.nameofmovie[i];
        
        cout<<eightmoviedatabase.nameofmovie[i];
        }
     
        
    
    
    
    
    
    	return 0;
    }

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    33
    Thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  2. Writing an array of struct to file
    By stellastarr in forum C Programming
    Replies: 10
    Last Post: 03-25-2006, 06:59 PM
  3. Replies: 4
    Last Post: 06-14-2005, 06:45 AM
  4. [question]Analyzing data in a two-dimensional array
    By burbose in forum C Programming
    Replies: 2
    Last Post: 06-13-2005, 08:31 AM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 01:04 PM

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