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;
}
Re: Inputing data of a struct member(declared as an array) from a file
Quote:
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];