Hello people. I have a favor to ask. My programming challenge is to write a program that uses a structure named movie data to store the following, title, director, year released, running time.
this 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.

This is what i wrote for the code.

#include <iostream>
using namespace std;

const int SIZE = 50; //Array size.

struct Moviedata
{
   char movieTitle[SIZE]; //Movie Title.
   char movieDirector[SIZE]; //The director of the movie.
   int yearReleased; //The year the movie was released.
   int runningTime; //Running time in minutes
};

int main()
{

	Moviedata first = {"The Hangover", "Todd Phillips", 2009, 99};
	Moviedata second = {"Transformers", "Michael Bay", 2007, 135};

	cout << "\nHere is the data for the first movie.\n";

	//Display data stored for the first movie.
	cout << "Movie Tittle: " << first.movieTitle << endl;
	cout << "Director of the movie: " << first.movieDirector << endl;
	cout << "The year the movie was released: " << first.yearReleased <<endl;
	cout << "The running time of the movie in minutes: " << first.runningTime << endl;
	
	cout << "\nHere is the data for the second movie.\n";

	//Display data stored for the second movie.
	cout << "Movie Title: " << second.movieTitle << endl;
	cout << "Director of the movie: " << second.movieDirector << endl;
	cout << "The year the movie was released: " << second.yearReleased << endl;
	cout << "The running time of the movie in minutes: " << second.runningTime << endl;
	
	return 0;
}

My first question is if i answered the question correctly? if not please tell me if i missed something.
Second im pretty confident that i wrote the program correctly, there are no build errors, but for some reason it does not want to run the program. If i change something on purpose to get an error it will pick that up and tell me, but it won't run or even attempt to run when the code is like above. Any suggestions or those it run for you guys as is.

Nick Evan commented: Congratulations on an excellent first post! +23

Recommended Answers

All 5 Replies

and pass each one in turn to a function that displays the information about the movie in a clearly formatted manner.

You did not yet make a function to display the structs yet. So you will need to create somethink like:

void Display(Moviedata moviein){
 // display the thing here instead of in main
 return;
}

Also I'd recommend that you lose the char-array's and use std::strings instead. What will happen if you have a title with more then 50 characters?

The reason (probably) that you don't see any data is because the console closes before you can see output. Put a cin.get() just before the p return 0; in your code.

Ok I have tried to change it to contain the functions. This is what i came up with so far.

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

const int SIZE = 50; //Array size.

struct Moviedata
{
   char movieTitle[SIZE]; //Movie Title.
   char movieDirector[SIZE]; //The director of the movie.
   int yearReleased; //The year the movie was released.
   int runningTime; //Running time in minutes
};


	//Function prototypes
void displayMovie(first);
void displayMovie(second);


int main()
{

	Moviedata first = {"The Hangover", "Todd Phillips", 2009, 99};
	Moviedata second = {"Transformers", "Michael Bay", 2007, 135};

	displayMovie(first);
	displayMovie(second);
	
	return 0;
}
void displayMovie(Moviedata &first)
{
	string movieTitle,
		movieDirector;
	int yearReleased, runningTime;

	cout << "\nHere is the data for the first movie.\n\n";

	//Display data stored for the first movie.
	cout << "Movie Tittle: " << first.movieTitle << endl;
	cout << "Director of the movie: " << first.movieDirector << endl;
	cout << "The year the movie was released: " << first.yearReleased <<endl;
	cout << "The running time of the movie in minutes: " << first.runningTime << endl;

	return 0;
}

void displayMovie(Moviedata &second)
{
	string movieTitle,
		movieDirector;
	int yearReleased,
		runningTime;

	cout << "\nHere is the data for the second movie.\n\n";

	//Display data stored for the second movie.
	cout << "Movie Title: " << second.movieTitle << endl;
	cout << "Director of the movie: " << second.movieDirector << endl;
	cout << "The year the movie was released: " << second.yearReleased << endl;
	cout << "The running time of the movie in minutes: " << second.runningTime << endl;
	cout << endl;

	return 0;
}

I also tried to use for a struture the following below, but that gave me more problems than the top one. I don't really know if my structure is the one with most of the problems or its the way im trying to use the void functions, the program points mostly to illegal use of void. Any Advice.

struct Moviedata
{
  string movieTitle, movieDirector;
  int yearReleased, runningTime;

  Moviedata(string A, string B, int C, int D)
   {
    movieTitle = A,
    movieDirector = B,
    yearReleased = C,
    runningTime = D;
   }
};

I've never seen structs/class objects initialized using the {} syntax. To my knowledge that only works with arrays, though someone may prove I'm wrong on that. You can, and should, use the initialization operator with an overloaded, non-default constructor to accomplish the same purpose. The last snippet in post 3 is a pretty good attempt at implemtenting a non-default constructor. It would be called using syntax similar to this:

MovieData first(A, B, C, D);

where A, B, C, and D are of the type expected by the constructor.
Also, make sure your user defined type has a default constructor in addition to the non-default constructor.

line 9 has movieTitle as a C style string whereas line 51 has movieTitle as an STL string object. They need to be of the same type, and not only on these two lines, but consistently throughout the program.

You only need a single function to display any object of the user defined type, either freestanding or written as a method within the struct.

i have changed your code a little, but basically it is the same. i also have made the program shorter by using functions. i have used the concept of pointers because i am using structure objects, but i have commented in what i have done so you can understand the code.

the program is working now.

#include <iostream>
#include<string>

using namespace std; 

const int SIZE = 50; //Array size-> 

struct Moviedata{ 
	
string movieTitle; //Movie Title->  i have used string data type because it is more simpler and i do not have to worry about the number of characters in a name.
string movieDirector; //The director of the movie->  
int yearReleased; //The year the movie was released->   
int runningTime; //Running time in minutes

}; 

void insertData(Moviedata *object,string title, string director, int year, int running);
void printData(Moviedata *object);

int main(){ 

	Moviedata first,second;
	
	//*********inserting the values in the respective objects. i am passing in the object by reference that is i am passing the address of the object.
	//any changes made in the function to the object will be directly reflected on to the actual object****************

	insertData(&first,"The Hangover", "Todd Phillips", 2009, 99);	
	insertData(&second,"Transformers", "Michael Bay", 2007, 135); 	
	
	//***********print the data of the two objects*****************

	
	cout << "\nHere is the data for the first movie->\n"; 	//Display data stored for the first movie->
	printData(&first);
	
	cout<<endl<<endl;

	cout << "\nHere is the data for the second movie->\n"; 	//Display data stored for the second movie->
	printData(&second);

	cout<<endl<<endl;



		
	return 0;

}

void insertData(Moviedata *object,string title, string director, int year, int running)
{
//i am using the operator -> instead of the . operator becaus i am using a pointer. if you are just starting with c++ just take this as it is.
	//when ever you declare a sturcture with the * sign that is it is a pointer, the pointer variable contains the adrress to the
	//variable/object where the actual data will be stored. also just assume that we can use . operator but then things become complicated because you have to use the 
	//dereferncing operator which is also the * operator, and you have to use brackets well which can get confusing for a beginner. so for simplicity just use -> operator 
	//when you use pointers.
	

	object->movieTitle=title;
	object->movieDirector=director;
	object->yearReleased=year;
	((*object).runningTime)=running; //this is what i would have written if i would not have used -> operator. see how complicated it is. so just use this, object->runningTime=running instead of ((*object).runningTime)=running.

	

}//end function insertData


void printData(Moviedata *object)
{

	cout << "Movie Tittle: " << object->movieTitle << endl;	
	cout << "Director of the movie: " << object->movieDirector << endl;	
	cout << "The year the movie was released: " << object->yearReleased <<endl;	
	cout << "The running time of the movie in minutes: " << object->runningTime << endl; 
 

}//end function printData

Thanks guys

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.