Can't fix my errors.

Hi, I have this code where I'm basically just using a struct to take in information. I'm close to being done but i can't figure out my errors. Keeps saying I need things before semicolons but that doesn't make sense to me. If you could help me figure out whats wrong I'll be forever grateful. Thank you. :)

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
#include <iostream>
#include <string>
#include <cstdlib>


using namespace std;


struct netflix 
{

	string name;
	int stars;
	int members;
	string *cast; //This is a dynamic array of strings 
	string rating;

};

int main()
{

	struct netflix movie;
	char yesno = 'y';

	while ( yesno == 'y' )
	{
	
		cout << "Would you like to enter a movie to the database? y or n: ";
		cin >> yesno;
		cout << endl;

		if ( yesno == 'y' )
		{
		
			cout << "Enter the name of the movie: ";
			cin.ignore();
			getline( cin, movie.name );

			cout << "Number of stars: ";
			cin >> movie.stars;

			cout << "How many cast members? ";
			cin >> movie.members;

			for ( int i = 0; i < movie.members; ++i )
			{
			
				cout "Enter cast member #" << i << ": ";
				cin >> movie.cast;
			
			}

			cout << "Enter movie rating: ";
			cin >> movie.rating;
		
		}
		else
			exit(0);

	}	
	

	return 0;

}
Line 49. cout "Enter cast member #" << i << ": ";. You missed << after cout

About line 15, check how to dynamically allocate arrays.
http://www.cplusplus.com/doc/tutorial/dynamic/
Thank you!
I'm still having a bit of a problem. I think I allocated the array correctly but it's saying my array, c, isn't declared in the scope. Maybe I'm not understanding the array allocation correctly, or is there something different I should be doing since I'm using a struct.

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
#include <iostream>
#include <string>
#include <cstdlib>

using namespace std;


struct netflix 
{

	string name;
	int stars;
	int members;
	string *cast; //This is a dynamic array of strings 
	string rating;

};

int main()
{

	struct netflix movie;
	char yesno = 'y';
	movie.cast = new string c[]; // also tried [c], &c[], *c[]

	while ( yesno == 'y' )
	{
	
		cout << "Would you like to enter a movie to the database? y or n: ";
		cin >> yesno;
		cout << endl;

		if ( yesno == 'y' )
		{
		
			cout << "Enter the name of the movie: ";
			cin.ignore();
			getline( cin, movie.name );

			cout << "Number of stars: ";
			cin >> movie.stars;

			cout << "How many cast members? ";
			cin >> movie.members;

			for ( int i = 0; i < movie.members; ++i )
			{
			
				cout << "Enter the name of cast member #" << i << ": ";
				getline( cin, c );
			
			}

			cout << "Enter movie rating: ";
			cin >> movie.rating;
		
		}
		else
			exit(0);

	}

	cout << "Name: " << movie.name << endl;
	cout << "Stars: " << movie.stars << endl;
	cout << "# of cast members: " << movie.members << endl;
	cout << "Cast members: " << movie.cast << endl;
	cout << "Rating: " << movie.rating << endl;	

	return 0;

}
Last edited on
i would try to use vectors if possible.

try to initialize it with some value.
on line 24 replace with

movie.cast = new string [10];

on line 50 replace with

getline( cin, movie.cast[i] );
Last edited on
Ohhh! Crap that's like the one thing I didn't try. :)
Alrighty! So all that up there worked nicely. Of course now I learned about splitting up structs, functions, and main into different files combined with a .h file, and I have completely redesigned my code. So naturally I now have other issues. I'll post my code and output, and I'll ask my questions at the end of the post.

.h file;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>

using namespace std;

struct netflix
{

	string name;
	int stars;
	int members;
	string *cast;
	string rating;

};

void set_netflix_info( netflix & );
void print_netflix_info ( netflix );


main:
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
#include <iostream>
#include <string>
#include "./netflix.h"

using namespace std;

int main()
{

	netflix movie;
	char yesno;

	cout << endl << "======================================================" << endl;
	
	do
	{
	
		cout << endl;
		set_netflix_info( movie );
		cout << "------------------------------------------------------" << endl;
		print_netflix_info( movie );
		cout << endl;

		cout << "Would you like to enter another movie? y or n: ";
		cin >> yesno;
		cin.ignore();

		cout << endl << "======================================================" << endl;
	
	}
	while ( yesno == 'y' );

	return 0;

}


Functions:
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
#include <iostream>
#include <string>
#include "./netflix.h"

using namespace std;


void set_netflix_info( netflix &movie )
{

	movie.cast = new string [20];

	cout << "Enter the name of the movie: ";
	getline( cin, movie.name );

	cout << "Enter the rating: ";
	getline( cin, movie.rating );

	cout << "How many stars? ";
	cin >> movie.stars;

	cout << "How many cast members? ";
	cin >> movie.members;

	for ( int i = 1; i <= movie.members; ++i )
	{
	
		cout << "Enter the name of cast member # " << i << ": ";
		cin.ignore();
		getline( cin, movie.cast[i] );
	
	}

}

void print_netflix_info( netflix movie )
{

	cout << "Movie: " << movie.name << endl;
	cout << "Rating: " << movie.rating << endl;
	cout << "Stars: " << movie.stars << endl;
//	cout << "Number of cast members: " << i << endl;
	cout << "Cast members: " << movie.cast << endl;

}


Output:

======================================================

Enter the name of the movie: Tron
Enter the rating: pg-13        
How many stars? 5
How many cast members? 2
Enter the name of cast member # 1: Blue Dude
Enter the name of cast member # 2: Red Dude
------------------------------------------------------
Movie: Tron
Rating: pg-13
Stars: 5
Cast members: 0x169b018

Would you like to enter another movie? y or n: n

======================================================


So what am I doing wrong with printing my cast member information? I'm guessing it's because in my functions file, int i in the set function isn't being passed to the print function. If I'm correct on the issue, what would be the best way to pass i. Is it any different with multiple files than if I was just passing between functions in a single file? I think I might have other questions but I'll stick with these for now.
Thank you! :)
its pretty much the same as how you entered the values.

1
2
3
4
5
for ( int i = 1; i <= movie.members; ++i )
{
	cout << movie.cast[i] << endl;
	
}
Oh wow, I must be loosing it. That was kind of obvious wasn't it?
One last problem I think. And I've played around with incrementing/decrementing i and it seems to make things worse. When I have multiple cast members, every cast member after the first gets the first letter skipped or cut off or something of the sort. I tried to think long and hard about what could cause this but I can't come up with anything that makes sense. Well to me anyways. Here's the changed code and the output.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void print_netflix_info( netflix movie )
{

	cout << "Movie: " << movie.name << endl;
	cout << "Rating: " << movie.rating << endl;
	cout << "Stars: " << movie.stars << endl;
//	cout << "Number of cast members: " << i << endl;
	for ( int i = 1; i <= movie.members; ++i )
	{
		
		cout << "Cast members: " << movie.cast[i] << endl;

	}

}


Output:
======================================================

Enter the name of the movie: Tron
Enter the rating: pg13
How many stars? 4
How many cast members? 3
Enter the name of cast member # 1: Blue Dude
Enter the name of cast member # 2: Red Dude
Enter the name of cast member # 3: Random Third Dude
------------------------------------------------------
Movie: Tron
Rating: pg13
Stars: 4
Cast members: Blue Dude
Cast members: ed Dude
Cast members: andom Third Dude

Would you like to enter another movie? y or n: n

======================================================


I just can't think of how the first member is complete but not the rest. Everything I thought of would only work if they all had the first letter cut off.
for your loops replace it with.

for ( int i = 0; i < movie.members; i++ )
I actually tried that, didn't work. The problem remains the same.
you just forgot the cin.ignore() after you asked for the integer input.

http://cpp.sh/6dk4
Ah! got it! Thanks :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
#include <cstdlib>


using namespace std;


struct netflix 
{

	string name;
	int stars;
	int members;
	string *cast; //This is a dynamic array of strings 
	string rating;

};


would a script still compile even with an ; after }
It won't compile without it actually. It's something to do with it being a type rather than a function I think. I could be totally wrong on the reason but it has to be there.
Ah okay, fair enough
Topic archived. No new replies allowed.