Structure confusion

A program that I'm creating in a c++ intro class is confusing me. The question is this, and right below is the main function it gives me

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)
Include a constructor that allows all 4 of these member data values to be specified at the time a MovieData variable is created. The program should create two MovieData variables and pass each one, in turn, to a function that displays the information about the movie in a clearly formatted manner.

1
2
3
4
5
6
7
8
9
10
int main()
{
        MovieData movie1("War of the Worlds", "Byron Haskin", 1953, 88),
                  movie2("War of the Worlds", "Stephen Spielberg", 2005, 118);
             
        displayMovie(movie1);
        displayMovie(movie2);
   
        return 0;
}


First of all, movie1() and movie2() are supposed to be variables with multiple/different data types, is that correct? Every example in my book has a variable in that place and I cannot find what that is made like this one is above.

My display function looks like this
1
2
3
4
5
6
7
8
9
10
void displayMovie()
{ 
	string title, 
		 director;
	int year, time;

	cout << "Title        : " << title << endl;
	cout << "Director     : " << director << endl;
	cout << "Year Released: " << year << endl;
	cout << "Running time : " <<  time << endl;

Is that correct?

My structure, which I'm really unsure of, looks like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
struct MovieData
{
	string title,
	       director;
	int year, 
	    time;
	MovieData(string t, string d, int y, int t2)
	{ title = t, 
	  director = d, 
	  year = y, 
	  time = t2;
	}

};


I'm mainly going off what examples show.

What all is wrong and what more do I need to do or change?
Your struct and such looks right, but your displayMovie() is wrong.

You don't pass it any parameters, I assume by your code that you want to pass it a movie. Just put it there like any other parameter, then access the data inside of the parameter (like it was the movie you wanted to look at data from).
Well with any parameter I just state the data type and the var name, but with this theres 2 different data types in the movie1/2 variables. It says that it cannot convert the data so I'm kind of clueless. I've never dealt with a variable(multiple values) yet

Well I fixed that part. It's now partially working code, I'm just getting warnings with the year and time, as well as no strings showing up by putting MovieData into the parameter.

I'm assuming that the values in movie1/2 are not getting to the displayMovie function.

Whats the next course of action?
Last edited on
You need to make displayMovie take one parameter, a variable of type MovieData, and display it's info. You are not going to pass the type of the member variable; rather you want to pass the structure type itself:

void displayMovie(MovieData DisplayMe) {
When I do that the code becomes executable, but it's not working right at all, here's what it looks like:

1
2
3
4
5
6
7
8
9
Title        :
Director     :
Year Released: -858993460
Running time : -858993460 minutes
Title        :
Director     :
Year Released: -858993460
Running time : -858993460 minutes
Press any key to continue
Last edited on
The output is incorrect because you are not printing the title, director, etc. that is stored in the movie structure. To access a variable inside a structure, you need to do something like: structureVariable.insideVariable

For example, to print the title:
 
cout << "Title        : " << movie.title << endl;


The above assumes that 'movie' is the parameter that the display function is getting.
Try this:

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

using namespace std;

struct MovieData
{
	string title, director;
	int year, time;

	MovieData(string t, string d, int y, int t2)
	{
		title = t, 
		director = d, 
		year = y, 
		time = t2;
	}
};

void displayMovie(MovieData &md)
{ 
	string title, 
		director;
	int year, time;

	cout << "Title        : " << md.title << endl;
	cout << "Director     : " << md.director << endl;
	cout << "Year Released: " << md.year << endl;
	cout << "Running time : " << md.time << endl;
}

int main()
{
	MovieData movie1("War of the Worlds", "Byron Haskin", 1953, 88),
		movie2("War of the Worlds", "Stephen Spielberg", 2005, 118);

	displayMovie(movie1);
	displayMovie(movie2);

	return 0;
}
That seems to have fixed it. Do I need to add the &md because there's 2 different data types?

I'm still getting the unreferenced local variables which I don't know how to fix but it is working, thank you all for the help
@kdenisk: We generally do not provide full solutions for peoples homework. Please only offer assistance in future.
Last edited on
The & used in this context makes the parameter a reference parameter. If you've not learned about references yet, then ignore it (or just delete it). If you have learned about references, then the parameter should be a const reference instead.

There aren't two different data types. You are confusing the term data type.

int and char and double -- those are data types.
string is a data type.
MovieData is a data type.

int x; -- x is an instance of data type int.
string foo; -- foo is an instance of data type string
MovieData movie1(...); -- movie1 is an instance of data type MovieData.

Unreferenced local variables are variables that are declared (instantiated) but are never used for anything. That should tell you how to fix it.

Yeah I found the const reference in the chapter, reading up on it now. I was refering to two different datatypes as the title and director were string while year and time were integers

I fixed the warnings as well, I didn't need to list the variables in the void function as they were already listed in the structure/constructor
Last edited on
why don't you move the display function into the struct, then you don't need to pass it anything - also for future code style it is good to hide members in classes, makes later class changes less painful.

with the displayMovie as member you could instead write movie1.displayMovie() movie2.displayMovie() - or even better drop the 'Movie' part since the object already is a movie i.e. movie1.display()

i know its not in the original assignment just giving an alt. approach



Topic archived. No new replies allowed.