error: expected primary-expression before...

Pages: 12
This compiles cleanly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef NETFLIX_H
#define NETFLIX_H

class netflix
{   vector<movie>   movies;

public:
    netflix();
    ~netflix();

    movie get_movie (int n);
    int get_num_movies();
};
#endif    


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
#ifndef MOVIE_H
#define MOVIE_H

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 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  


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <vector>
using namespace std;

#include "movie.h"
#include "netflix.h"

netflix::netflix()
{}

netflix::~netflix()
{}

movie netflix::get_movie (int n) 
{   return movies[n];  }

int netflix::get_num_movies() 
{   return movies.size(); }


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
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <vector>
using namespace std;

#include "movie.h"

movie::movie()
{   stars = 0;
    copies = 0;
}

movie::~movie()
{}

void movie::menu ( )
{   int 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 < 1 || choose > 3);

    if ( choose == 1 )
        enter_movie();
    if ( choose == 2 )
        // Enter search mode
    if ( choose == 3 )
        exit(1);
}

void movie::enter_movie ()
{   set_name();
    set_stars();
    set_num_cast();
    set_cast();
    set_rating();
    set_copies();
}

void movie::set_name ()
{   cout << "Movie title: ";
    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 << ": ";
        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; }


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>
#include <vector>
using namespace std;

#include "movie.h"
#include "netflix.h"

int main()
{   netflix Netflix;
    movie Movie;
    Movie.menu ();
    return 0;
}

Last edited on
Fix the movie title, numbers of stars, number of cast members, rating of copies.
Oooooooooooooh! Wow! Ok I had several things all mixed up. And I had questions about the proper use of consts I guess I should have asked about sooner. So you want to have the getters be the constant functions. That makes since now that I'm seeing it.
So there's two things and then I can move on to file io which is supposed to be super easy.
1) When I run the code the prompt for the title prints but the input is skipped and goes straight to the stars prompt. The same goes for the first cast member. The rest of the cast members allows input just fine. I ran into this problem once before but I can't remember the fix and I can't find that code that had the issue.
2) My error handling loop accounts for numbers 0-9 but not for characters. I'm pretty sure I can figure this one out though, I know I just need to make the while (choose < 1 || choose > 3); part ascii code right? Or was it a type cast thing? I can't remember but I'll play around with that. Thanks again guys, I was loosing hope there for a bit. :)
Yeah play around with those two!
You might want to have error checking as well when you're inputting characters.

Fix those again and see what you got so far along with the menu!
Post it again when you're completely done.
Got it! just needed cin.ignore before the getlines and in the menu I did:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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' )
        // Enter search mode
    if ( choose.at(0) == '3' )
            exit(1);
}

Now I just have to print it to a file. I was told it's as easy as cin and cout so here's hoping. :)

Last edited on
Okay that's good!
I need you to post your entire syntax, so I can see what you can do with your search mode.
By the way, the movie title and numbers of stars are still on the same line and along with the numbers of cast.


Movie title: Number of stars: 7
Number of cast members: 2
Cast member #1: Cast member #2:

it looks like this.
you should fix it
Last edited on
net.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#ifndef NETFLIX_H
#define NETFLIX_H

class netflix
{
    private:
        vector<movie>   movies;
    
    public:
        netflix();
        ~netflix();
    
        movie get_movie (int n);
        int get_num_movies();
};
#endif 

mov.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
#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 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 

net.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "movie.h"
#include "netflix.h"
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <vector>
using namespace std;

netflix::netflix()
{}

netflix::~netflix()
{}

movie netflix::get_movie (int n)
{   return movies[n];  }

int netflix::get_num_movies()
{   return movies.size(); }

mov.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
#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' )
        // Enter search mode
    if ( choose.at(0) == '3' )
            exit(1);
}

void movie::enter_movie ()
{
    set_name();
    set_stars();
    set_num_cast();
    set_cast();
    set_rating();
    set_copies();
}

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; }

main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <cstdlib>
#include <fstream>
#include <vector>

using std::cout;
using std::cin;
using std::endl;
using std::vector;

#include "movie.h"
#include "netflix.h"

int main()
{
    netflix Netflix;
    movie Movie;
    Movie.menu ();
    
    return 0;
}


I fixed the title and cast with cin.ignore(). :)
https://www.youtube.com/watch?v=Iho2EdJgusQ

It seem like you're using fstream to store data in a txt file
Watch this. It shows how to store data and search data within txt file
Last edited on
Ok, so I have an idea that 1) not sure if it'll work and 2) not sure how to do a certain part of it. I want to make a function that calls all my getters and prints the info to the text file. Would this work the same as the enter_movie function but with the file io part included such as something like:
1
2
3
4
5
6
7
void movie::database()
{
     ofstream data_in;
     data_in.open("database.txt");
     data_in << get_name() << "|" << get_stars() << "|" << get_num_cast() << "|" << get_cast_member() << "|" << get_rating() << "|" << get_copies() << endl;
     data_in.close();
}

I'm supposed to have the info separated by the pipe delimiters. Not sure if thats the proper way to do it. My feeling it that it's not. Later on I'm going to have to be able to search for a movie based on say titles or cast members or rating, that kind of thing. Was told that I'd be able to do this by looking between certain delimiters based on their positions or something like that. I couldn't really comprehend what the person was saying at the time about that. And with my previous issue with apparently not knowing how to call functions properly I wasn't sure if the getters would be called the same since they are returning values. Thanks again:)
data_in is a rather odd name for an output file. :)

Since database() is a member of your movie class, it has access to all the class variables. No need to call getters.

 
data_in << name << "|" << stars << "|" << num_cast << "|" << cast[0] << "|" << rating << "|" << copies << endl;


One note about cast. Since cast is a vactor of strings, it can have multiple cast members. In the above snippet, I've output only the first cast member. This assumes the cast vector always has at least one member.

You might want to give some more thought to how you represent a cast with multiple cast members. You can certainly output cast members separated by pipe symbols since you output the number of cast members first. If you do that, you need a loop to iterate through the vector and output each member.

You do not need to call getters.
Last edited on
Oh yeah, I was unsure about that name. I was thinking that for the text file its input but for the program it's output. I'll change that, best to keep the out's together. :)
You must have input and output for your program.

When you enter your movie data information, ask the user whether he/she wants to save it.

If save, you input the data within the textfile.

If they're search the movie, you output the movie that they're searching.


Show us your entire code when you're done along with the fstream.
Ok it doesn't seem to be writing to the text file and I swear I followed the tutorial closely. I didn't think I would have trouble with this part. My only changes are in these functions of the movie.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
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);
}

void movie::to_database()
{
    std::ofstream data_out;
    data_out.open("database.txt");
    data_out << name << "|" << stars << "|" << num_cast << "|" << cast[0] << "|" << rating << "|" << copies;
    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();
}

oh and I added the functions to the public members in the .h:
1
2
    void to_database();
    void from_database();

I tried it before and after adding the prompt to save the movie and I tried it with and without the .close().
What did you put in the search part?
Haven't made it to the search part yet, I was waiting until I could successfully print the info to the text file so that I would have something to search for.
It should work.

1) Enter your movie information

2) save it

3) idk what compiler you're using but type vim or vi database.txt

4) that will show your movie information
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. :)
Topic archived. No new replies allowed.
Pages: 12