I'm in a computer programming class and having trouble. The assignment is:
Write a program that uses a structure named MovieData to store the following information about a movie:
Title
Director
Year Released
Running Time (in minutes)
The program should create two MovieData variables, store values in their members, and pass each one, in turn, to a function that displays the information about the movie in a clearly formatted manner.
I created my program, but getting errors. I guess I don't know exactly what they are asking for. Thanks for any help you can give.
#include <iostream>
#include <iomanip>
usingnamespace std;
constint SIZE = 50;
struct MovieData
{
char title[SIZE];
char director[SIZE];
int year;
int minutesRunning;
};
void GetMovieInfo(MovieData&);
void MovieDisplay(MovieData);
int main()
{
MovieData member1, member2;
GetMovieInfo(member1, member2);
MovieDisplay(member1, member2);
return 0;
}
void GetMovieInfo(MovieData &m1, &m2)
{
//Get movie title
cout << "Enter the title of the movie: ";
cin.ignore();
cin.getline(m1.title, SIZE);
//Get director's name
cout << "Enter the Director's name of the movie: ";
cin.ignore();
cin.getline(m1.director, SIZE);
//Get the release year
cout << "Enter the year the movie was released: ";
cin >> m1.year;
//Get the movie runtime in minutes
cout << "Enter runtime of the movie in minutes: ";
cin >> m1.minutesRunning;
//Get movie title
cout << "Enter the title of the movie: ";
cin.ignore();
cin.getline(m2.title, SIZE);
//Get director's name
cout << "Enter the Director's name of the movie: ";
cin.ignore();
cin.getline(m2.director, SIZE);
//Get the release year
cout << "Enter the year the movie was released: ";
cin >> m2.year;
//Get the movie runtime in minutes
cout << "Enter runtime of the movie in minutes: ";
cin >> m2.minutesRunning;
}
void MovieDisplay(MovieData m1, m2)
{
//Display the movie information
cout << "Below is the data of the desired movie:\n";
cout << "Movie Title: " << m1.title << endl;
cout << "Director's Name: " << m1.director << endl;
cout << "Release Year: " << m1.year << endl;
cout << "Movie Runtime in minutes: " << m1.minutesRunning << endl;
//Display the movie information
cout << "Below is the data of the desired movie:\n";
cout << "Movie Title: " << m2.title << endl;
cout << "Director's Name: " << m2.director << endl;
cout << "Release Year: " << m2.year << endl;
cout << "Movie Runtime in minutes: " << m2.minutesRunning << endl;
}
You forgot to mention the datatype before m2 here: void GetMovieInfo(MovieData &m1, &m2)
and here void MovieDisplay(MovieData m1, m2)
I think you mistakenly thought that you could you use initializer lists as argument lists, or something like that.
You can use initializer lists like this: MovieData m1, m2, m3;
to have m1, m2 and m3 usable as MovieData variables.
But with argument lists you are not able to do such a thing.
It seems that the rest of the errors are caused because you use m2 because the syntax to make m2 was wrong.
#include <iostream>
#include <iomanip>
usingnamespace std;
constint SIZE = 50;
struct MovieData
{
char title[SIZE];
char director[SIZE];
int year;
int minutesRunning;
};
void GetMovieInfo(MovieData&, MovieData&);
void MovieDisplay(MovieData, MovieData);
int main()
{
MovieData member1, member2;
GetMovieInfo(member1, member2);
MovieDisplay(member1, member2);
return 0;
}
void GetMovieInfo(MovieData &m1, MovieData &m2)
{
cout << "First Movie\n\n";
//Get movie title
cout << "Enter the title of the movie: ";
cin.ignore();
cin.getline(m1.title, SIZE);
//Get director's name
cout << "Enter the Director's name of the movie: ";
cin.ignore();
cin.getline(m1.director, SIZE);
//Get the release year
cout << "Enter the year the movie was released: ";
cin >> m1.year;
//Get the movie runtime in minutes
cout << "Enter runtime of the movie in minutes: ";
cin >> m1.minutesRunning;
cout << "\n---------------------------------\n";
cout << "Second Movie\n\n";
//Get movie title
cout << "Enter the title of the movie: ";
cin.ignore();
cin.getline(m2.title, SIZE);
//Get director's name
cout << "Enter the Director's name of the movie: ";
cin.ignore();
cin.getline(m2.director, SIZE);
//Get the release year
cout << "Enter the year the movie was released: ";
cin >> m2.year;
//Get the movie runtime in minutes
cout << "Enter runtime of the movie in minutes: ";
cin >> m2.minutesRunning;
}
void MovieDisplay(MovieData m1, MovieData m2)
{
cout << "\n---------------------------------\n";
cout << "First Movie";
//Display the movie information
cout << "\n\nBelow is the data of the first movie entered:\n";
cout << "Movie Title: " << m1.title << endl;
cout << "Director's Name: " << m1.director << endl;
cout << "Release Year: " << m1.year << endl;
cout << "Movie Runtime in minutes: " << m1.minutesRunning << endl;
cout << "\n---------------------------------\n";
cout << "Second Movie";
//Display the movie information
cout << "\n\nBelow is the data of the second movie entered:\n";
cout << "Movie Title: " << m2.title << endl;
cout << "Director's Name: " << m2.director << endl;
cout << "Release Year: " << m2.year << endl;
cout << "Movie Runtime in minutes: " << m2.minutesRunning << endl;
}
for me there's no reason for using cin.ignore() after all i don't know how it use :) and when i compile it looks like there's a bug in your code but i'm a little bit confused. this line:
1 2 3 4 5 6
//Get the movie runtime in minutes
cout << "Enter runtime of the movie in minutes: ";
cin >> m1.minutesRunning;
//Get movie title
cout << "Enter the title of the movie: ";
cin.getline(m2.title, SIZE);
seems like contain a bug. it will print both commands in single line. btw, i modified it wihout cin.ignore() and my compiler is dev c++
I'm not sure why your MovieDisplay() function takes two parameters here. Wouldn't it be better to take one parameter and pass both your variables to it in separate calls?
void MovieDisplay(MovieData m)
{
cout << "\n---------------------------------\n";
cout << "First Movie";
//Display the movie information
cout << "\n\nBelow is the data of the first movie entered:\n";
cout << "Movie Title: " << m.title << endl;
cout << "Director's Name: " << m.director << endl;
cout << "Release Year: " << m.year << endl;
cout << "Movie Runtime in minutes: " << m.minutesRunning << endl;
}
int main()
{
MovieData member1, member2;
// .. get movie info
MovieDisplay(member1); // output first
MovieDisplay(member2); // output second
return 0;
}
And the same applies to GetMovieInfo().
The benefit of computers is that they can do the repetitive stuff easily so you shouldn't need to repeat code the way you have. Just call it twice.