Dvd Collection
Jul 9, 2018 at 8:30pm UTC
So, I have written a dvd program for my c++ class and I`ve been getting a segmentation fault. So I found the fault and there is an infinite loop I noticed between line 19 and 34 so i`ve been trying cout<<"test"<<endl; but I don`t know how to fix this problem maybe there is more wrong than I know.
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
#include <iostream>
#include <string>
#include <iomanip>
#include "dvdlibrary.h"
#include <fstream>
using namespace std;
//Constructors
dvd_library::dvd_library()
{
//dvd library[MAX_LIBRARY_SIZE];
used=0;
dvd temp;
ifstream in_stream;
in_stream.open("data.txt" );
in_stream >> temp;
while (! in_stream.eof())
{
//in_stream >> temp; //dvd object
cout<<"test" <<endl;
library[used].set_title(temp.get_title());
cout<<"test1" <<endl;
library[used].set_rating(temp.get_rating());
cout<<"test3" <<endl;
library[used].set_movie_type(temp.get_movie_type());
cout<<"test2" <<endl;
library[used].set_year(temp.get_year());
cout<<"test4" <<endl;
library[used].set_run_time(temp.get_run_time());
cout<<"test5" <<endl;
used++;
in_stream >> temp; //dvd object
}
in_stream.close();
cout << "Loaded " << used << " movies from data.txt." << endl;
}
I know it`s somewhere here because it prints the tests but not the "loaded"
Last edited on Jul 9, 2018 at 9:01pm UTC
Jul 9, 2018 at 8:52pm UTC
Please use code formatting. Edit your post and and add
[co de] and
[/c ode] around your program.
First, you shouldn't be looping on .eof(). Instead, loop on the action itself, which is
in_stream >> temp .
i.e.
1 2 3 4
while (in_stream >> temp)
{
// logic
}
Have an error check for
if used >= MAX_LIBRARY_SIZE .
Topic archived. No new replies allowed.