Write a program that uses a stuct named MovieData to store the following information about a movie.

//The 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.

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

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

struct MovieData
{
string title;
string director;
int year;
float time;
};

int index;
const int num = 2;
void showInfo(MovieData info[num]);

int main(int argc, char** argv)
{
int index;
MovieData info[num];

cout << "Enter the two movies informations.\n" << endl;

for (index = 0; index < num; index++)
{
cout << "Enter the title of the movie #" << (index + 1);
cout << ": ";
cin.ignore();
getline(cin, info[index].title);

cout << "Enter the director's name of movie #" << (index + 1);
cout << ": ";
cin.ignore();
getline(cin, info[index].director);

cout << "What year did the movie #" << (index + 1);
cout << " released: ";
cin >> info[index].year;

cout << "What is the running time (in minutes) in movie #" << (index + 1);
cout << ": ";
cin >> info[index].time;
cout << "\n";
}

showInfo(MovieData info[num]);

return 0;
}

void showInfo(info[num])
{
for (index = 0; index < num; index++)
{
cout << "\nHere are the movie's informations: \n";
cout << "Title movie #" << (index + 1);
cout << ": " << info[index].title << endl;
cout << "Director movie #" << (index + 1);
cout << ": " << info[index].director << endl;
cout << fixed << showpoint << setprecision(2);
cout << "Year released: " << info[index].year << endl;
cout << "Running time (in minutes): " << info[index].time << endl;
}
}
What is your question?

That's not how you pass arrays to functions. Just do
showInfo(info);
Hello luqcpp,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



I have yet to compile the program because there are noticeable errors that need fixed first.

It appears the you do not have a good understanding of functions yet. Have a look at https://www.learncpp.com/cpp-tutorial/introduction-to-functions/

The function prototype and function definition must match. You are partly correct in what you have done, but not complete.

The line showInfo(MovieData info[num]);. This is almost a prototype. All that is missing is the return type. As a function call it does not work. All you need for a function call is the name of the variable(s).

Your global variable "index" is a bad idea. Any line of code that follows could change its value and that becomes more difficult to track down where that happened.

Do not feel that using a variable like "index" in more than 1 function requires a global variable.

In your for loops: for (index = 0; index < num; index++). It is best to define the variable in the for loop and keep it local to the for loop. Also when the for loops is complete the local variable(s) used are destroyed.

The comments in the following code should help.
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
64
65
66
67
68
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

struct MovieData
{
    string title;
    string director;
    int year;
    float time;
};

//int index;  // <--- Should not be here.
const int MAXSIZE = 2;
void showInfo(MovieData info[MAXSIZE]);

int main(int argc, char** argv)
{
    int index;  // <--- Defines a local variable to "main". Does not know about or have any effect on line 17. Better defined in the for loops.
    MovieData info[MAXSIZE];

    cout << "Enter the two movies informations.\n" << endl;

    for (index = 0; index < MAXSIZE; index++)
    {
        cout << "Enter the title of the movie #" << (index + 1);
        cout << ": ";
        cin.ignore();
        getline(cin, info[index].title);

        cout << "Enter the director's name of movie #" << (index + 1);
        cout << ": ";
        cin.ignore();
        getline(cin, info[index].director);

        cout << "What year did the movie #" << (index + 1);
        cout << " released: ";
        cin >> info[index].year;

        cout << "What is the running time (in minutes) in movie #" << (index + 1);
        cout << ": ";
        cin >> info[index].time;
        cout << "\n";
    }

    showInfo(info);

    return 0;
}

void showInfo(MovieData info[MAXSIZE])  // <--- Changed. Size for a 1D array not needed, but OK if you leave. Degrades to a pointer anyway.
{
    for (int index = 0; index < MAXSIZE; index++)
    {
        cout << "\nHere are the movie's informations: \n";
        cout << "Title movie #" << (index + 1);
        cout << ": " << info[index].title << endl;
        cout << "Director movie #" << (index + 1);
        cout << ": " << info[index].director << endl;
        cout << fixed << showpoint << setprecision(2);
        cout << "Year released: " << info[index].year << endl;
        cout << "Running time (in minutes): " << info[index].time << endl;
    }
}


Looking at the function I would suggest using "\n" over "endl" as much as possible.

Also you could do:
1
2
3
4
5
6
7
cout
    << "\nHere are the movie's informations: \n"
    << "Title movie #" << (index + 1) << info[index].title << '\n'
    << "Director movie #" << (index + 1)<< ": " << info[index].director << '\n'
    << fixed << showpoint << setprecision(2)
    << "Year released: " << info[index].year << '\n'
    << "Running time (in minutes): " << info[index].time << endl;

The "cout" and "endl" for each line is not necessary.

Now that it compiles I give it a few tests.

One last note: In your struct you define "time" as a float. This is more than you need. I can not think of any reason to need the decimal part. Most movie run times I have seen are in minutes, so a simple "int" will work.

Andy
Topic archived. No new replies allowed.