Here's the problem, questions at the bottom.

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.

I'm given

// Chapter 7 - Assignment 1, Movie Data
// This program stores movie information in a structure. 
// The structure is passed to a function to display the data.

#include <iostream>
#include <string>
using namespace std;


	// --------------------------------
	// ----- ENTER YOUR CODE HERE -----
	// --------------------------------


	// --------------------------------
	// --------- END USER CODE --------
	// --------------------------------


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

I've got a question right here. movie1 and movie2 are both variables no?(Cannot find an example of this in my book) It looks like a function there being parenthesis. How does it work?

what my structure looks like right now

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


};

My display message function

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

Am I along the right tracks or am I doing something totally wrong? I missed class this week and so I'm having a hard time grasping what I'm supposed to do. What am I doing wrong and what do i need to do to get this running. I've been messing around and looking through my book to understand how it works and no luck so far. Hints would be helpful

Recommended Answers

All 5 Replies

Found a problem, and now it's executable code. Still has problems though. The year/time is showing up as negative eighty thousands and the director/title are blank. two warnings with the year and time variables not getting initialized so theres something wrong with the movie1/2 values reaching the displayMovie function.

#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)
{ 
	string title, 
		 director;
	int year, time;

	cout << "Title        : " << title << endl;
	cout << "Director     : " << director << endl;
	cout << "Year Released: " << year << endl;
	cout << "Running time : " <<  time << " minutes" << 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;
}

Nobody? I've got it where it displays
Title :
Director :
Year Released: -858993460
Running time : -858993460 minutes
Title :
Director :
Year Released: -858993460
Running time : -858993460 minutes
Press any key to continue

and I have no idea why the variables aren't being included

you don't initialize title etc before outputting the variable so they have random, junk values. You don't need them anway. You want to use the dot operator to output the appropriate member variables of the object you sent to the function. You also need to give the function definition the name of the MovieData object it is going to use, not just the type, just like you do if you use a prototype before main() and define the function after main().

you don't initialize title etc before outputting the variable so they have random, junk values. You don't need them anway. You want to use the dot operator to output the appropriate member variables of the object you sent to the function. You also need to give the function definition the name of the MovieData object it is going to use, not just the type, just like you do if you use a prototype before main() and define the function after main().

I've been trying to use dot operators but I can't get anything to work with whats in main(which cannot be changed).

And about the function definition data type. With the setup I have to use, the data type is both string and int values that are in the movie1/2 parenthesis, no?

>>the data type is both string and int values that are in the movie1/2 parenthesis

fine.

void displayMovie(MovieData & md)
{ 
  cout << "Title    : " << md.title << endl;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.