I can get my file to load 1 full struct. But when I try to iterate through the file, i have an endless loop and no data is loaded. I have attached my code and the flat file.
#include <iostream>
#include <string.h>
#include <vector>
#include <fstream>
using namespace std;
typedef struct video_items{
string Title;
int YOP;
string Category;
float Gross;
}video;
void LoadMovies (vector<video> &v);
void SearchMovies (vector <video> &v, string s);
int main()
{
vector <video> v;
LoadMovies(v);
cout<<"Total number of movies: "<<v.size()<<endl;
for (unsigned int i=0; i<v.size(); ++i) {
cout<<"----------------------------------------------------"<<endl;
cout<<v[i].Title<<endl;
cout<<"Category: "<<v[i].Category<<endl;
cout<<"Year of Publication: "<<v[i].YOP<<endl;
cout<<"Gross: "<<v[i].Gross<<endl;
cout<<"----------------------------------------------------"<<endl<<endl;
}
string WantMovie;
cout<<"Please type in what movie you want."<<endl;
cin>>WantMovie;
SearchMovies(v,WantMovie);
return 0;
}
void LoadMovies (vector<video> &v)
{
video L;
ifstream myfile;
myfile.open ("Movies.txt");
if (myfile.is_open())
{
cout <<"Loading Movie Catalog..."<<endl<<endl;
int i=0;
while (!myfile.eof())
{
myfile.ignore();
//cout<<i<<endl<<endl;
v.push_back(L);
getline(myfile,v[i].Title);
getline(myfile,v[i].Category);
myfile>>v[i].YOP;
myfile>>v[i].Gross;
i++;
}
myfile.close();
}
else cout<<"Unable to open file."<<endl;
}
void SearchMovies (vector <video> &v, string s)
{
s[0] = toupper(s[0]);
unsigned int i;
for (i=0; i<v.size(); i++)
{
if (v[i].Title.compare(0,s.size(),s)==0)
{
break;
}
}
if (i >=v.size()){
i =0;
}
switch(i)
{
case 1:cout<<"You have chosen "<<s<<endl;
break;
default:
cout<<"That movie is not currently in the library, please choose a different one."<<endl;
break;
}
}
First character ignored in flat file.
=========Data File==========
Captain America: The First Avenger
Action, Adventure, Sci-Fi
2011
1786.65
Iron Man
Action, Adventure, Sci-Fi
2008
585.2
The Incredible Hulk
Action, Adventure, Sci-Fi
2008
134.52
Iron Man 2
Action, Adventure, Sci-Fi
2010
312.43
Thor
Action, Adventure, Fantasy
2011
181.03
The Avengers
Action, Adventure, Sci-Fi
2012
623.28
Iron Man 3
Action, Adventure, Sci-Fi
2013
409.01