sorry.. i just wanna ask about the comment for this coding so that I can easily understand how this coding works. can anyone helps me to put the comments on it?

// array of structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

#define N_MOVIES 3

struct movies_t {
  string title;
  int year;
} films [N_MOVIES];

void printmovie (movies_t movie);

int main ()
{
  string mystr;
  int n;

  for (n=0; n<N_MOVIES; n++)
  {
    cout << "Enter title: ";
    getline (cin,films[n].title);
    cout << "Enter year: ";
    getline (cin,mystr);
    stringstream(mystr) >> films[n].year;
  }

  cout << "\nYou have entered these movies:\n";
  for (n=0; n<N_MOVIES; n++)
    printmovie (films[n]);
  return 0;
}

void printmovie (movies_t movie)
{
  cout << movie.title;
  cout << " (" << movie.year << ")\n";
}

sorry.. i just wanna ask about the comment for this coding so that I can easily understand how this coding works. can anyone helps me to put the comments on it?

Are you asking what the program / code does?

This code creates a structure called movies_t. The variable films is defined and is a static array holding 3 (#define N_MOVIES 3) movies_t structures.

struct movies_t {
string title;
int year;
} films [N_MOVIES];

The user is then asked to supply the titles and years of three movies. The title is stored in the title member of the movies_t structure, and the year in the year member.

for (n=0; n<N_MOVIES; n++)
{
cout << "Enter title: ";
getline (cin,films[n].title);
cout << "Enter year: ";
getline (cin,mystr);
stringstream(mystr) >> films[n].year;
}

Finally, the information the user has inputted is echoed back, and the program ends.

cout << "\nYou have entered these movies:\n";
for (n=0; n<N_MOVIES; n++)
printmovie (films[n]);
return 0;
This article has been dead for over six months. Start a new discussion instead.