:)
Hello experts, Im taking a C++ class and i was asked to 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)

I've come down to making it compile, but, when it couts the output, the line for "year released" and for "run time" is i think, the reference for those cells. I tried to dereference using the & (i.e &p.year... &p.time) operator and no luck,
it gives me a runtime error, any suggestions?
here is my code:

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

const int TITLE = 50;
const int RELEASED = 2000;
const int RUN_TIME = 300;
const int NAME_SIZE = 50;

struct movieData
{
	char title[TITLE];
	char director[NAME_SIZE];
	int year[RELEASED];
	int time[RUN_TIME];
};
void getInfo(movieData&);
void showInfo(movieData);
int main()
{
	movieData movie;
	getInfo(movie);
	showInfo(movie);
	return 0;
}


//Definition of getInfo
void getInfo(movieData &p)
{
//Get movie title
	cout << "Enter movie Title:\n";
	cin >> p.title;
	//Get movie director
	cout << "Enter movie's Director:\n";
	cin.ignore();
	cin.getline(p.director, NAME_SIZE);
	//Get year released
	cout << "Enter year movie was released:\n";
	cin >> p.year[RELEASED];
	cout << "Enter the movie's Running Time:\n";
	cin >> p.time[RUN_TIME];
}
//Definition of function showInfo
void showInfo(movieData p)
{
	cout << fixed << showpoint << setprecision(2);
	cout << "Movie Title: " << p.title << endl;
	cout << "Director Name: " << p.director << endl;
	cout << "Year Released: " << p.year << endl;
	cout << "Running Time: " << p.time << endl;
}

Dani AI

Generated

Quick diagnosis and why you saw garbage / a crash: the original struct declared year and time as arrays (e.g. int year[RELEASED];). Using p.year[RELEASED] indexes well past your buffer and is undefined behavior (likely the runtime crash). When you stream an int[] it decays to int* and cout prints the pointer value (looks like an address), not the number. Also & is the address-of operator, not a dereference; * is the dereference operator — but you don’t need either if year and time are plain int members. As noted, the simplest fix is to store int year and int runningTime, and was right to suggest std::string for text fields.

A few practical tips:

  • Prefer std::string for title/director to avoid fixed-size buffer issues.
  • If you mix operator>> and std::getline, clear the remainder of the line with cin.ignore(numeric_limits<streamsize>::max(), '\n') after >>. Calling a plain cin.ignore() at the wrong time can eat the first character of the next input.
  • Use input validation for numeric fields (check plausible year range and positive running time).
  • You don’t need fixed/showpoint/setprecision for integers — those are for floating-point formatting.

Example of a safer input pattern (uses std::string and validates numbers):

#include <iostream>
#include <string>
#include <limits>

struct MovieData { std::string title, director; int year = 0, runningMinutes = 0; };

int read_int(const std::string& prompt, int lo, int hi) {
  std::string s;
  while (true) {
    std::cout << prompt;
    if (!std::getline(std::cin, s)) std::exit(1);
    try {
      int v = std::stoi(s);
      if (v < lo || v > hi) throw std::out_of_range("range");
      return v;
    } catch (...) { std::cout << "Please enter a number between " << lo << " and " << hi << ".\n"; }
  }
}

Small extras: give meaningful names (MovieData, runningMinutes), initialize members, and validate inputs. This will avoid the pointer/address output and the runtime crashes you saw.

Recommended Answers

All 3 Replies

Do you really want an array to represent the year and the running time? so leave those as ints. Also, any title over 1 word gets truncated. Why not use a getline for that as well?

Do you really want an array to represent the year and the running time? so leave those as ints. Also, any title over 1 word gets truncated. Why not use a getline for that as well?

jonsca thanks for your help, I tried different things and did not get far. code is working fine now.
thank you, here is the working code:

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

const int TITLE = 50;
const int RELEASED = 2000;
const int NAME_SIZE = 50;

struct movieData
{
	char title[TITLE];
	char director[NAME_SIZE];
	int year;
	int time;
};
void getInfo(movieData&);
void showInfo(movieData);
int main()
{
	movieData movie;
	getInfo(movie);
	showInfo(movie);
	return 0;
}


//Definition of getInfo
void getInfo(movieData &p)
{
//Get movie title
	cout << "Enter movie Title:\n";
	cin.getline(p.title, TITLE);
	//Get movie director
	cout << "Enter movie's Director:\n";
	cin.ignore();
	cin.getline(p.director, NAME_SIZE);
	//Get year released
	cout << "Enter year movie was released:\n";
	cin >> p.year;
	cout << "Enter the movie's Running Time:\n";
	cin >> p.time;
}
//Definition of function showInfo
void showInfo(movieData p)
{
	cout << fixed << showpoint << setprecision(2);
	cout << "Movie Title: " << p.title << endl;
	cout << "Director Name: " << p.director << endl;
	cout << "Year Released: " << p.year << endl;
	cout << "Running Time: " << p.time << endl;
}

how about:

struct movieData
{
	std::string title;
	std::string director;
	int year;
	int time;
};
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.