5 Replies - 17695 Views - Last Post: 16 November 2010 - 10:46 AM Rate Topic: -----

#1 cyber_gurl2010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 19-October 10

New some Suggestions (Movie Data)

Posted 15 November 2010 - 04:35 PM

I am trying to write a program and this is what i have so far. Any suggestions would be helpful.

I am trying to use a structure name MovieData to store the following information about a movie.
- title, director, year released, and running time (in minutes). Also trying to include a constructor that allows all four of these member data values to be specified at the time a MovieData is created.

Right now, when i compile my program it shows an "error:no instance of constructor MovieDate::MovieData matches the argument list. The error shows red underline under the movie title " War of the Worlds".


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

struct MovieData
{
   string movieTitle; 
   string movieDirector; 
   int yearReleased; 
   int runningTime; 
};
  
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;
}

void displayMovie(MovieData movie1)
{
	//Display data stored for movie1.
	cout << "Movie Tittle: " << movie1.movieTitle << endl;
	cout << "Director: " << movie1.movieDirector << endl;
	cout << "Released: " << movie1.yearReleased <<endl;
	cout << "Running Time: " << movie1.runningTime << endl;
}

void displayMovie(MovieData movie2)
{
	//Display data stored for movie2.
	cout << "Movie Title: " << movie2.movieTitle << endl;
	cout << "Director: " << movie2.movieDirector << endl;
	cout << "Released: " << movie2.yearReleased << endl;
	cout << "Running Time: " << movie2.runningTime << endl;
	cout << endl;
}


MOD EDIT: When posting code...USE CODE TAGS!!!

:code:

This post has been edited by JackOfAllTrades: 15 November 2010 - 05:48 PM


Is This A Good Question/Topic? 0
  • +

Replies To: New some Suggestions (Movie Data)

#2 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2015
  • View blog
  • Posts: 5,409
  • Joined: 27-December 05

Re: New some Suggestions (Movie Data)

Posted 15 November 2010 - 04:47 PM

You have to write constructors as part of the definition of your struct, using exactly the same syntax as you would for class constructors. For the example you posted, you need a constructor with 4 parameters. If you also want to be able to declare a MovieData item without specifying the data values, then you will also need to write a default constructor (one that has an empty parameter list).

This post has been edited by r.stiltskin: 15 November 2010 - 04:49 PM

Was This Post Helpful? 0
  • +
  • -

#3 cyber_gurl2010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 19-October 10

Re: New some Suggestions (Movie Data)

Posted 15 November 2010 - 05:00 PM

View Postr.stiltskin, on 15 November 2010 - 03:47 PM, said:

You have to write constructors as part of the definition of your struct, using exactly the same syntax as you would for class constructors. For the example you posted, you need a constructor with 4 parameters. If you also want to be able to declare a MovieData item without specifying the data values, then you will also need to write a default constructor (one that has an empty parameter list).
understand what you saying about having a default constructor. Am still new at this so just trying to understand how I can use constructors.
Was This Post Helpful? 0
  • +
  • -

#4 r.stiltskin  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2015
  • View blog
  • Posts: 5,409
  • Joined: 27-December 05

Re: New some Suggestions (Movie Data)

Posted 15 November 2010 - 05:17 PM

You use it exactly the way you already did in your main function. But before you can use it you have to write it. Here's a link to Elcric's C++ Beginners tutorial which includes an explanation of class constructors. The same syntax applies to structs. (The difference between a class and a struct is simply that in a class, everything is private by default, and in a struct everything is public by default. If you don't know what that means, don't worry about it right now -- just read the tutorial and apply it to your struct.)
Was This Post Helpful? 0
  • +
  • -

#5 cyber_gurl2010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 25
  • Joined: 19-October 10

Re: New some Suggestions (Movie Data)

Posted 16 November 2010 - 10:19 AM

Thanks,rstiltskin. That link you provided helped out alot. Turns out it was alot simpler to do and made a few changes and added some other info. Sometimes i always make things harder than it seems. Well, My program is running now and here it is:

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


struct MovieData
{
   string movieTitle; 
   string movieDirector; 
   int yearReleased; 
   int runningTime;

   MovieData (string movieT, string movieD, int yearR, int runningT)
   { 
	 movieTitle = movieT;
          movieDirector = movieD;
	 yearReleased = yearR;
	 runningTime = runningT;
   }
};
 
void displayMovie(MovieData movie1);

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

void displayMovie(MovieData movie1)
{
	//Display data stored for movie1.
	cout << "Movie Title: " << movie1.movieTitle << endl;
	cout << "Director: " << movie1.movieDirector << endl;
	cout << "Released: " << movie1.yearReleased <<endl;
	cout << "Running Time: " << movie1.runningTime << endl <<endl;
}

This post has been edited by JackOfAllTrades: 16 November 2010 - 10:45 AM
Reason for edit:: Added code tags

Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: New some Suggestions (Movie Data)

Posted 16 November 2010 - 10:46 AM

Once again...if you're posting code, OBEY THE RULES OF THE SITE AND POST IN CODE TAGS!!!

:code:

If that's too complicated for you, then please read this topic.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1