0

Here is a function of my program. When you use getline(cin,something) it is supposed to get the whole line doesn't it?

void readMovieData(MovieData *pMovie, int const size)
{
string title;
string director;
int year;
int time;

for(int i = 0; i < size; i++)
{
    cout << "\nPlease enter the Title of the movie: ";
    getline(cin, title);
    pMovie[i].setTitle(title);

    cin.ignore();

    cout << "Please enter the Director name of the movie: ";
    getline(cin, director);
    pMovie[i].setDirector(director);

    cin.ignore();

    cout << "Please enter the year it was released: ";
    cin >> year;
    if(year >= 1900 && year <= 2004)
        pMovie[i].setYear(year);
    else
    {
        cout << "Please enter a year between 1900 and 2004." << endl;
        year = 0;
        system("PAUSE");
        break;
    }

    cout << "Please enter the time the movie last: ";
    cin >> time;
    if(time > 0 && time < 14400)
        pMovie[i].setTime(time);
    else
    {
        time = 0;
        cout << "Please enter a time between 0 and 14400 in minutes." << endl;
        system("PAUSE");
        break;
    }
} 
}

when I run the program and enter two or more words, it seems to not work. My int's takes the input of the words other than the first one. I don't see what is the problem.

This is my output

How many movies did you watch last month? 2

Please enter the information of your Movies!

Please enter the Title of the movie: bobby lee

Please enter the Director name of the movie: very scary

Please enter the year it was released: Please enter a year between 1900 and 2004.

Press any key to continue . . .

Here are the information with your Movies!

Your average time of all your movie is: 0

The oldest Movie you have watched is called...

Title:

Director: obby lee

Release Year: 0

The most newest Movie you have watched is called...

Title:

Director: obby lee

Release Year: 0

Process returned 0 (0x0) execution time : 11.962 s Press any key to continue.

I want the program to get the whole name of title (1 or more words) and the whole name of the director(1 or more words) without error.

user3345335
  • 33
  • 1
  • 1
  • 6
  • Please be more specific, as "it seems to not work. My int's takes the input of the words other than the first one" is not clear. Tell us the input, and the output. Most of the program above is not required to reproduce the problem, probably. – Lightness Races in Orbit Mar 10 '14 at 20:28
  • Could you show a more tangible sample for your inputs and outputs, and what you're expecting, please? – πάντα ῥεῖ Mar 10 '14 at 20:28
  • 1
    What do you think happens to the CRLF you entered by pressing *after* your last input (the time) ? If you think it is picked up by the following `getline()`, you might be on to something. – WhozCraig Mar 10 '14 at 20:28
  • @WhozCraig: Or, rather, _before_ it. – Lightness Races in Orbit Mar 10 '14 at 20:28
  • @LightnessRacesinOrbit The one following the year should be skipped when reading the time (after reading the year, both int). Unless I'm utterly sleep deprived and you see something different (you often do, and I often am) =P – WhozCraig Mar 10 '14 at 20:30
  • Yeah but my program is an array of classes using pointers. If i change the location of the int's and strings, it won't really matter. – user3345335 Mar 10 '14 at 20:32
  • @user3345335 Setting aside the line-termination issue I mentioned for a moment, shall we assume the memory pointed to by `pMovie` is *correctly* allocated for at least `size` MovieData objects? – WhozCraig Mar 10 '14 at 20:37
  • Obligatory: when you used the debugger, what line(s) are giving you issues and please detail the issues (add to your post too). – Thomas Matthews Mar 10 '14 at 20:41
  • 1
    All these `cin.ignore();`appear useless and false –  Mar 10 '14 at 20:41
  • possible duplicate of [getline takes only a piece of the information](http://stackoverflow.com/questions/22315162/getline-takes-only-a-piece-of-the-information) – jfly Mar 11 '14 at 09:35

0 Answers0