Need help with structures and functions

I need to write a program that uses structure movieData to store the release date, duration, director and title name of the movie. One movie needs to be statically allocated (all 4 fields) and the other dynamically (all 4 fields). And finally two functions to call to display what is stored.My question is how do I get the function to display the results? And also how to i us the same structure but for two different movies? This is what i have so far. If you see any other errors or styling mistakes please let me know. Thank you for your help in advance!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
  //Chapter 11 Program 1: Movie Data
//Written by

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

struct movieData
{
	string title;
	string director; 
	int releaseDate;
	int runTime;
};
movieData *m = nullptr;
movieData movie1, movie2;
//*********************************************************************************************************
//Function for getting movie data                                                                         *
//*********************************************************************************************************
void getMovieData(movieData *m)
{
	//Get movie title
	cout << " What is the title of the movie?\n";
	getline(cin, m->title);

	//Get director's name
	cout << " Who directed the movie?\n";
	cin >> (cin, m->director);

	//Get movie release date
	cout << " When was this movie released?\n";
	cin >> m->releaseDate;

	//Get movie runtime 
	cout << " How long is the movie?\n";
	cin >> m->runTime;
}


int main()
{
	cout << " Hi, today we will create a structure called movieData and store information\n into it.\n ";
	getMovieData(*m)
	 
}
Last edited on
Ive gotten further and now i need to know how to display each movie data. I know im suppose to displayMovie(movie1) but im not sure how to go about this. This is what i have so far. Instead of movie1.title i had moviedata.title but i kept getting an error. Also it is not letting me statically allocate the 4 fields in movieData.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
//Chapter 11 Program 1: Movie Data
//Written by 
#include <iostream> 
#include <string>
using namespace std; 

struct movieData
{
	string title;
	string director; 
	int releaseDate;
	int runTime;
};
movieData *m = nullptr;
movieData movie1, movie2;
//*********************************************************************************************************
//Function for getting movie data                                                                         *
//*********************************************************************************************************
void getMovieData(movieData *m)
{
	//Get movie title
	cout << " What is the title of the movie?\n";
	getline(cin, m->title);

	//Get director's name
	cout << " Who directed the movie?\n";
	cin >> (cin, m->director);

	//Get movie release date
	cout << " When was this movie released?\n";
	cin >> m->releaseDate;

	//Get movie runtime 
	cout << " How long is the movie in minutes?\n";
	cin >> m->runTime;
};

//******************************************************************************************
//Function to display what is in the struct                                                *
//******************************************************************************************
void displayMovie1()
{
	cout << " Movie: " << movie1.title <<endl;
	cout << " Director: " << movie1.director << endl;
	cout << " Release date: " << movie1.releaseDate << endl;
	cout << " Run time: " << movie1.runTime << endl;
}

int main()
{
	movieData movie2
		(
		"District 9",
		"Neil BlomKamp",
		2009,
		112
		);
	cout << " Hi, today we will create a structure called movieData and store information\n into it.\n ";
	getMovieData(&movie1);
	displayMovie1();
	 
}
}
Last edited on
The function you have is close, but you don't want to have to write a new function for every movie you declare. This is what parameters are for.

Instead of saying
1
2
3
4
5
6
7
void displayMovie1()
{
	cout << " Movie: " << movie1.title <<endl;
	cout << " Director: " << movie1.director << endl;
	cout << " Release date: " << movie1.releaseDate << endl;
	cout << " Run time: " << movie1.runTime << endl;
}
use a parameter so that you can call this function with any movie you make:
1
2
3
4
5
6
7
void displayMovie(movieData movieParameter)
{
	cout << " Movie: " << movieParameter.title <<endl;
	cout << " Director: " << movieParameter.director << endl;
	cout << " Release date: " << movieParameter.releaseDate << endl;
	cout << " Run time: " << movieParameter.runTime << endl;
}

Then, in main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
    movieData movie1, movie2, badMovie, goodMovie, scaryMovie;
    //fill up each of these movie objects with data, might I suggest
    //your handy getMovieData function that you already have?

    //after each movie has data, you can call the same function
    //to display each movie
    displayMovie(movie1);
    displayMovie(goodMovie);
    displayMovie(badMovie);
    displayMovie(movie2);
    displayMovie(scaryMovie);
}


EDIT: Notice that you're already using a function with a parameter (getMovieData takes a pointer to a movieData object as its parameter).
Last edited on
Thanks for your reply!! i really appreciate it. Im still stuck though. Im getting error C2064. And also did i implement your advice correctly?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//Chapter 11 Program 1: Movie Data
//Written by

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

struct movieData
{
	string title;
	string director; 
	int releaseDate;
	int runTime;
};
movieData *m = nullptr;
movieData movie1, movie2;

//*********************************************************************************************************
//Function for getting movie data                                                                         *
//*********************************************************************************************************
void getMovieData(movieData *m)
{
	//Get movie title
	cout << " What is the title of the movie?\n";
	getline(cin, m->title);

	//Get director's name
	cout << " Who directed the movie?\n";
	cin >> (cin, m->director);

	//Get movie release date
	cout << " When was this movie released?\n";
	cin >> m->releaseDate;

	//Get movie runtime 
	cout << " How long is the movie in minutes?\n";
	cin >> m->runTime;
};

//******************************************************************************************
//Function to display what is in struct                                                    *
//******************************************************************************************
void displayMovie(movieData movieP)
{
	cout << " Movie: " << movieP.title <<endl;
	cout << " Director: " << movieP.director << endl;
	cout << " Release date: " << movieP.releaseDate << endl;
	cout << " Run time: " << movieP.runTime << endl;
}

int main()
{
	movie2 ("District 9", "Neil BlomKamp", 2009, 112);
	cout << " Hi, today we will create a structure called movieData and store information\n into it.\n ";
	getMovieData(&movie1);
	displayMovie(movie1);
	displayMovie(movie2);
	return 0;
}
Last edited on
Could post the exact error message please? It's much more helpful than just an error code.
Is this is the error message?
Error 1 error C2064: term does not evaluate to a function taking 4 arguments

and also one that says:
2 IntelliSense: call of an object of a class type without appropriate operator() or conversion functions to pointer-to-function type

I figured it out! Thanks for everyone's help!!! I am curious how to dynamically allocate one of the movie structures and then im completely done. This is what i have completed so far

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//Chapter 11 Program 1: Movie Data
//Written by 

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

struct movieData
{
	string title;
	string director; 
	int releaseDate;
	int runTime;
};
movieData *m = nullptr;
movieData movie1, movie2;

//*********************************************************************************************************
//Function for getting movie data                                                                         *
//*********************************************************************************************************
void getMovieData(movieData *m)
{
	//Get movie title
	cout << " What is the title of the movie?\n";
	getline(cin, m->title);

	//Get director's name
	cout << " Who directed the movie?\n";
	getline(cin, m->director);

	//Get movie release date
	cout << " When was this movie released?\n";
	cin >> m->releaseDate;

	//Get movie runtime 
	cout << " How long is the movie in minutes?\n";
	cin >> m->runTime;
};

//******************************************************************************************
//Function to display what is in struct                                                    *
//******************************************************************************************
void displayMovie(movieData movieP)
{
	cout << " Movie: " << movieP.title <<endl;
	cout << " Director: " << movieP.director << endl;
	cout << " Release date: " << movieP.releaseDate << endl;
	cout << " Run time: " << movieP.runTime << endl;
}

int main()
{
	movie2.title = "District 9";
	movie2.director = "Neil BlomKamp";
	movie2.releaseDate = 2009;
	movie2.runTime = 112;
	cout << " Hi, today we will create a structure called movieData and store information\n into it.\n ";
	getMovieData(&movie1);
	displayMovie(movie1);
	displayMovie(movie2);
	return 0;
}
Last edited on
In main, use the pointer that you declared on line 15.
1
2
3
4
5
m = new movieData();
m->title = "Pulp Fiction";
m->director = "Quentin Tarantino";
m->releaseDate = 1994;
m->runTime = 154;


Then print it, do whatever, just remember when you're done with it:
delete m;
Ohhhhhh i see that makes sense. That isnt too difficult. Thanks! i appreciate you taking the time to help me!!!
Topic archived. No new replies allowed.