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. :)
#include <iostream>
#include <string>
#include <cstdlib>
usingnamespace 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;
}
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.
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.
#include <iostream>
#include <string>
#include "./netflix.h"
usingnamespace 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! :)
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.
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.