FILM DATA HELP!!!

So i've been trying to fix my code for DAYS. It is on a continuous loop and I'm not sure where im going wrong. Also, the getline does not seem to work. Im using XCODE.

https://pastebin.com/iftzVMUn


// THE MAIN .CPP HAS THIS.

#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <fstream>
#include "Film.h"

using namespace std;
int main(){
int id;
int start =0;
std::string name;
std::string description;
int runningTime;
std::string rating;
std::ifstream filmData;

vector <Film> films;

filmData.open("filmData.txt",std::ios::in);

while(!filmData.eof()){

filmData >> id;
filmData.ignore();
getline(filmData, name, ',');
getline(filmData, description, ',');
filmData >> runningTime;
filmData.ignore();
getline(filmData, rating, ',');
films.push_back(Film(id,name, description, runningTime, rating));
films[start].printData();
start++;
}

filmData.close();
return 0;

}

/// FILM HEADER HAS THIS
#ifndef Film_H
#define Film_H
#include <iostream>
#include <string>

using namespace std;


enum FilmRating {G, PG, PG_13, NC_17, R, UNRATED};

class Film{

private:

int id;
string name;
string description;
int runningTime;
string rating;
FilmRating movieRate;

public:

Film(int id, string name, string description, int runningTime, string rating);

Film (int id, string name, string description,int runningTime, FilmRating rating);

int getId();

string getName();

void setName(string);

string getDescription();

void setDescription(string);

int getRunningTime();

void setRunningTime(int);

string getRating();

void setRating(FilmRating);

void stringRatetoFilmRate(string);

void printData();

};

#endif

// FILM.CPP HAS THIS



#include "Film.h"


Film::Film(int anID, std::string aName, std:: string theDescription, int therunningTime, std::string aRating){
id = anID;
name =aName;
description = theDescription;
runningTime = therunningTime;
rating = aRating;
stringRatetoFilmRate(rating);
}


Film::Film(int id, std:: string name, std:: string description, int runningTime, FilmRating movieRate){

setName(name);
setDescription(description);
setRunningTime(runningTime);
setRating(movieRate);
}



int Film:: getId(){

return id;
}

string Film::getName(){

return name;
}

void Film::setName(string aName){

name = aName;
}


string Film::getDescription(){

return description;
}


void Film::setDescription(string theDescription){

description = theDescription;
}

int Film::getRunningTime(){

return runningTime;

}

void Film::setRunningTime(int therunningTime){

runningTime = therunningTime;

}


std::string Film::getRating(){

switch(movieRate){

case 0:
return "G";
break;

case 1:
return "PG";
break;

case 2:
return "PG_13";
break;

case 3:
return "R";
break;

case 4:
return "NC-17";
break;

case 5:
return "UNRATED";
break;
}
}



void Film::setRating(FilmRating aRating){
movieRate = aRating;
}


void Film::stringRatetoFilmRate(std::string rating){

if(rating == "G"){
movieRate = G;
}
else if(rating == "PG"){
movieRate = PG;
}
else if(rating == "PG-13"){
movieRate = PG_13;
}
else if(rating =="NC_17"){

movieRate = NC_17;
}

else if(rating == "R"){

movieRate = R;
}
else if(rating == "UNRATED"){

movieRate =UNRATED;
}

}


void Film::printData(){

cout << "ID: " << getId() << endl;
cout << "Name: " << getName() << endl;
cout << "Description: " << getDescription() << endl;
cout << "Running Time: " << getRunningTime() << endl;
cout << "Rating: " << rating << endl;

};


// FILM.TXT HAS THIS

0, Star Wars: A New Hope, The Imperial Forces hold Princess Leia hostage in their effort to quell the rebellion against the Galactic Empire, 125, PG_13,
1, 13 Going on 30, Focuses on a teenage girl who travels to her future through a wish that comes true, 98, PG_13,
2, Deadpool, A fast-talking mercenary with a morbid sense of humor who ends up saving people, 108, R,
3, IT, A shape-shifting evil that emerges from the sewer to prey on the towns children, 148, R
Last edited on
Post it so we can see it/
its on the paste bin! its long! (just added it)
Last edited on
I just don't like going to a website I don't trust.

Try

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
43
44
45
// THE MAIN .CPP HAS THIS. 

#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>
#include <fstream>
#include "Film.h"

using namespace std;
int main()
{
	int id;
	int start =0;
	std::string name;
	std::string description;
	int runningTime;
	std::string rating;
	std::ifstream filmData;
	vector <Film> films;
	filmData.open("film.txt",std::ios::in);
	int count =0;
	
//	while(!filmData.eof())
	while(filmData.good())
		{
//			cout <<  count << endl; count ++;
			filmData >> id;
			filmData.ignore();
			getline(filmData, name, ',');
			getline(filmData, description, ',');
			filmData >> runningTime;
			filmData.ignore();
			getline(filmData, rating, ',');
			films.push_back(Film(id,name, description, runningTime, rating));
			films[start].printData();
			start++;
		}
	
	filmData.close();
return 0;
}




Also check the filename
Last edited on
You can see I added the count value to see what was going on.
I figured it wasn't opening the file and your not checking to see if it's open.
While trying to fix that with the while loop I noticed the file name may not be correct.
Not sure which fixed it but it's all there for you to see.
Topic archived. No new replies allowed.