943,683 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 604
  • C++ RSS
Nov 16th, 2008
0

Structure help

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Foe89 is offline Offline
22 posts
since Oct 2008
Nov 17th, 2008
0

Re: Structure help

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.

C++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Foe89 is offline Offline
22 posts
since Oct 2008
Nov 17th, 2008
0

Re: Structure help

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Foe89 is offline Offline
22 posts
since Oct 2008
Nov 17th, 2008
0

Re: Structure help

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().
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005
Nov 17th, 2008
0

Re: Structure help

Click to Expand / Collapse  Quote originally posted by Lerner ...
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?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Foe89 is offline Offline
22 posts
since Oct 2008
Nov 17th, 2008
0

Re: Structure help

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

fine.
C++ Syntax (Toggle Plain Text)
  1. void displayMovie(MovieData & md)
  2. {
  3. cout << "Title : " << md.title << endl;
  4. }
Reputation Points: 718
Solved Threads: 373
Nearly a Posting Maven
Lerner is offline Offline
2,253 posts
since Jul 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: error dynamic_cast in c++
Next Thread in C++ Forum Timeline: hw help!! struct + arrays + formatting





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC