Structure help

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 22
Reputation: Foe89 is an unknown quantity at this point 
Solved Threads: 0
Foe89 Foe89 is offline Offline
Newbie Poster

Structure help

 
0
  #1
Nov 16th, 2008
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

  1. // Chapter 7 - Assignment 1, Movie Data
  2. // This program stores movie information in a structure.
  3. // The structure is passed to a function to display the data.
  4.  
  5. #include <iostream>
  6. #include <string>
  7. using namespace std;
  8.  
  9.  
  10. // --------------------------------
  11. // ----- ENTER YOUR CODE HERE -----
  12. // --------------------------------
  13.  
  14.  
  15. // --------------------------------
  16. // --------- END USER CODE --------
  17. // --------------------------------
  18.  
  19.  
  20. int main()
  21. {
  22. MovieData movie1("War of the Worlds", "Byron Haskin", 1953, 88),
  23. movie2("War of the Worlds", "Stephen Spielberg", 2005, 118);
  24.  
  25. displayMovie(movie1);
  26. displayMovie(movie2);
  27.  
  28. return 0;
  29. }

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
  1. struct MovieData
  2. {
  3. string title,
  4. director;
  5. int year,
  6. time;
  7. MovieData(string t, string d, int y, int t2)
  8. { title = t,
  9. director = d,
  10. year = y,
  11. time = t2;
  12. }
  13.  
  14.  
  15. };

My display message function

  1. void displayMovie()
  2. {
  3. string title, director;
  4. int year, time;
  5.  
  6. cout << "Title : " << title << endl;
  7. cout << "Director : " << director << endl;
  8. cout << "Year Released: " << year << endl;
  9. cout << "Running time : " << time << endl;
  10. }

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
Last edited by Foe89; Nov 16th, 2008 at 11:46 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 22
Reputation: Foe89 is an unknown quantity at this point 
Solved Threads: 0
Foe89 Foe89 is offline Offline
Newbie Poster

Re: Structure help

 
0
  #2
Nov 17th, 2008
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.

  1. #include <iostream>
  2. #include <string>
  3. using namespace std;
  4.  
  5. struct MovieData
  6. {
  7. string title,
  8. director;
  9. int year,
  10. time;
  11. MovieData(string t, string d, int y, int t2)
  12. { title = t,
  13. director = d,
  14. year = y,
  15. time = t2;
  16. }
  17.  
  18.  
  19. };
  20.  
  21.  
  22. void displayMovie(MovieData)
  23. {
  24. string title,
  25. director;
  26. int year, time;
  27.  
  28. cout << "Title : " << title << endl;
  29. cout << "Director : " << director << endl;
  30. cout << "Year Released: " << year << endl;
  31. cout << "Running time : " << time << " minutes" << endl;
  32. }
  33.  
  34.  
  35. int main()
  36. {
  37. MovieData movie1("War of the Worlds", "Byron Haskin", 1953, 88),
  38. movie2("War of the Worlds", "Stephen Spielberg", 2005, 118);
  39.  
  40. displayMovie(movie1);
  41. displayMovie(movie2);
  42.  
  43. return 0;
  44. }
Last edited by Foe89; Nov 17th, 2008 at 12:34 am.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 22
Reputation: Foe89 is an unknown quantity at this point 
Solved Threads: 0
Foe89 Foe89 is offline Offline
Newbie Poster

Re: Structure help

 
0
  #3
Nov 17th, 2008
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
Last edited by Foe89; Nov 17th, 2008 at 1:20 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,681
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 264
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Structure help

 
0
  #4
Nov 17th, 2008
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().
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 22
Reputation: Foe89 is an unknown quantity at this point 
Solved Threads: 0
Foe89 Foe89 is offline Offline
Newbie Poster

Re: Structure help

 
0
  #5
Nov 17th, 2008
Originally Posted by Lerner View Post
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?
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,681
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 264
Lerner Lerner is offline Offline
Posting Virtuoso

Re: Structure help

 
0
  #6
Nov 17th, 2008
>>the data type is both string and int values that are in the movie1/2 parenthesis

fine.
  1. void displayMovie(MovieData & md)
  2. {
  3. cout << "Title : " << md.title << endl;
  4. }
Klatu Barada Nikto
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC