Ah, alright yeah it works. For this code I was trying out Xcode instead of just using vim in a terminal window. Works fine in terminal, but not at all in Xcode. Strange but oh well, easiest fix yet. :)
So Here's my new .h and .cpp that successfully enters multiple movies to the text file:
.h
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
|
#ifndef MOVIE_H
#define MOVIE_H
#include <iostream>
#include <vector>
using std::string;
using std::vector;
class movie
{ string name;
int stars;
int num_cast;
vector<string> cast;
string rating;
int copies;
public:
movie();
~movie();
void menu ();
void enter_movie ();
void rent_movie();
void to_database();
void from_database();
void set_name ();
void set_stars ();
void set_num_cast ();
void set_cast ();
void set_rating ();
void set_copies ();
string get_name() const;
int get_stars() const;
int get_num_cast() const;
string get_cast_member (int n) const;
string get_rating () const;
int get_copies() const;
};
#endif
| |
.cpp
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 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <vector>
using std::cout;
using std::cin;
using std::endl;
using std::vector;
using std::string;
#include "movie.h"
movie::movie()
{ stars = 0;
copies = 0;
}
movie::~movie()
{}
void movie::menu ( )
{
string choose;
do
{ cout << "Would you like to enter or rent a movie?" << endl;
cout << "( 1 - enter, 2 - rent, 3 - exit ): ";
cin >> choose;
cout << endl;
}
while (choose.at(0) < 49 || choose.at(0) > 51);
if ( choose.at(0) == '1' )
enter_movie();
if ( choose.at(0) == '2' )
rent_movie();
if ( choose.at(0) == '3' )
exit(1);
}
void movie::enter_movie()
{
set_name();
set_stars();
set_num_cast();
set_cast();
set_rating();
set_copies();
char yn;
cout << "Would you like to save this movie? (y or n): ";
cin >> yn;
if (yn == 'y')
to_database();
else if (yn == 'n')
menu();
else
exit(1);
cout << "Would you like to enter another movie? (y or n): ";
cin >> yn;
if (yn == 'y')
enter_movie();
else if ( yn == 'n')
menu();
}
void movie::rent_movie()
{
from_database();
}
void movie::to_database()
{
std::ofstream data_out;
data_out.open("database.txt", std::ios::app);
data_out << name << "|" << stars << "|" << num_cast << "|" << cast[0] << "|" << rating << "|" << copies << endl;
data_out.close();
}
void movie::from_database()
{
std::ifstream data_in;
data_in.open("database.txt");
data_in >> name >> stars >> num_cast >> cast[0] >> rating >> copies;
data_in.close();
}
void movie::set_name ()
{
cout << "Movie title: ";
cin.ignore();
getline (cin, name);
}
void movie::set_stars ()
{
cout << "Number of stars: ";
cin >> stars;
}
void movie::set_num_cast ()
{
cout << "Number of cast members: ";
cin >> num_cast;
}
void movie::set_cast ()
{
string cast_member;
for (int i = 0; i<num_cast; ++i)
{
cout << "Cast member #" << i+1 << ": ";
cin.ignore();
getline (cin, cast_member);
cast.push_back (cast_member);
}
}
void movie::set_rating ()
{
cout << "Rating: ";
getline (cin, rating);
}
void movie::set_copies ()
{
cout << "Number of copies: ";
cin >> copies;
}
string movie::get_name() const { return name; }
int movie::get_stars() const { return stars; }
int movie::get_num_cast() const { return num_cast; }
string movie::get_cast_member (int n) const { return cast[n]; }
string movie::get_rating() const { return rating; }
int movie::get_copies() const { return copies; }
| |
There's actually still one bit of a problem that I almost forgot about. In the movie info that is printed, for the cast members, only the first cast member makes it on there. I can see why, because when it's calling
cast[0]
it's of course just the one position in the cast array. I was about to throw that bit of code in a for loop but then realized it would just print all the movie info as many times as there are cast members. That's when I realized I have no idea how I would get it to only loop for the cast members, that is unless there's some other way of doing it without a loop?
And then my next problem is the searching through the text file part. I've been looking at some tutorials on searching but then I remembered I need to rent the movie which would then require me to modify the number of copies left in the text file. I really don't know how to go about these things. So I figured I'd ask again about any recommended tutorials or videos. The last one you sent me a link to was really good, sadly it was the last video on the list. Thanks as always for everything. :)