Bob John
Bob John

Reputation: 3878

Pointers to structures - having trouble understanding specific fragments of code

Here is the code:

// pointers to structures
#include <iostream>
#include <string>
#include <sstream>
using namespace std;

struct movies_t {
  string title;
  int year;
};

int main ()
{
  string mystr;

  movies_t amovie;
  movies_t * pmovie;
  pmovie = &amovie;

  cout << "Enter title: ";
  getline (cin, pmovie->title);
  cout << "Enter year: ";
  getline (cin, mystr);
  (stringstream) mystr >> pmovie->year;

  cout << "\nYou have entered:\n";
  cout << pmovie->title;
  cout << " (" << pmovie->year << ")\n";

  return 0;
}

Taken from http://www.cplusplus.com/doc/tutorial/structures/. I was hoping I could get clarification on a few things.

  1. What is getline and how does it work? I tried looking up the documentation, but I'm still having trouble understanding. Also, what exactly is cin and how is it being used with getline?

  2. If I understand correctly, pmovie->title essentially says that pmovie points to the member title of the object amovie? If so, and it's not already clear from the explanation to #1, how does getline (cin, pmovie->title) work?

  3. Now this (stringstream) mystr >> pmovie->year is giving me the most trouble. What is a stringstream, and are we using it like we would cast a double as a int, for example?

Thank you all!

Upvotes: 0

Views: 89

Answers (3)

David Schwartz
David Schwartz

Reputation: 182761

What is getline and how does it work? I tried looking up the documentation, but I'm still having trouble understanding. Also, what exactly is cin and how is it being used with getline?

The getline function reads a line from a istream. The cin stream refers to your standard input stream, the one you would normally get input from. It is being passed to getline to tell it which input stream to get a line from.

If I understand correctly, pmovie->title essentially says that pmovie points to the member title of the object amovie? If so, and it's not already clear from the explanation to #1, how does getline (cin, pmovie->title) work?

The getline functions reads a line from cin and stores it in pmovie->title which is passed by reference.

Now this (stringstream) mystr >> pmovie->year is giving me the most trouble. What is a stringstream, and are we using it like we would cast a double as a int, for example?

A stringstream is a class that makes a string act like a stream. This is kind of confusing syntax (C-style cast) that makes it a bit harder to understand what it is happening. Basically, a temporary stringstream is created and initialized with the contents of mystr. A stringstream, when initialized with a string, gives you a stream from which you can read those contents. The >> operator reads from an output stream, in this case, into pmovie->year, which is again passed by reference.

By the way, it seems to me like you're trying to understand unusually complex and confusing uses without yet understanding the more basic uses of these objects. That's a very hard way to learn.

Upvotes: 2

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 153830

Most of the questions don't seem to be about structures at all. So, I'm addressing the issue which is related to the title rather than those about streams:

If I understand correctly, pmovie->title essentially says that pmovie points to the member title of the object amovie? If so, and it's not already clear from the explanation to #1, how does getline (cin, pmovie->title) work?

You misunderstand. I would guess, that this is the root of your confusion: pmovie points to a movies_t object. As it happens, in the sample code it is initialized to point to the movies_t object named amovie.

Now, each movies_t object has two members, i.e., subobjects: a title and a year. To access the title component of a movies_t pointed to by a pointer you use pmovie->title. To access the year component instead you'd use pmovie->year.

The one thing I say about streams, though, is this: You should always check that your input was successful before assuming the read was successful. For example, you would check that reading a line was successful using

if (std::getline(std::cin, pmovie->title)) {
    // deal with a successfully read title
}

Upvotes: 1

Linuxios
Linuxios

Reputation: 35803

  1. cin is a special stream defined by C++ to work with standard output (usually the keyboard, but can be almost anything). getline is a function that allows you to read text from a stream into a buffer until the platform's line ending is encountered (Line Feed on UNIX, Carriage Return Line Feed of Windows and DOS).
  2. pmovie->title says that pmove is a pointer to a structure that has a member called title. This refers to that member. Because getline takes a string& (String reference), it happily accepts the string referenced by pmovie->title.
  3. stringstream defines an implicit constructor that converts strings to stringstreams. the >> operator gets input from a string and converts it to the target type (the type of the operand to the right of the >>) and puts it there. This is just a way of converting a string to an integer.

Upvotes: 0

Related Questions