简体   繁体   中英

C++ How do I store a different number of values to my variables in a structure that contains two variables?

Here is my code so far. C++ My code. This is a problem for school that asks 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)

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.

My problem is when the user inputs information for the second movie, the getline(cin, b.title) is skipped and just moves on to the director. I'm looking for why this is happening and how to fix it. Required output vs. My output.

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

    struct MovieData {
        string title;
        string director;
        int year;
        int runningTime;
    };

    void displayMovie(MovieData movie);

    int main()
    {
        MovieData a, b;

        cout << "First Movie" << endl;
        cout << "Enter title: ";
        getline (cin, a.title);
        cout << "Enter Director: ";
        getline (cin,a.director);
        cout << "Enter year: ";
        cin >> a.year;
        cout << "Enter Runing Time: ";
        cin >> a.runningTime;


        cout << "\nSecond Movie" << endl;
        cout << "Enter title: ";
        getline (cin, b.title);
        cout << "Enter Director: ";
        getline (cin, b.director);
        cout << "Enter year: ";
        cin >> b.year;
        cout << "Enter Runing Time: ";
        cin >> b.runningTime;

        displayMovie(a);

        displayMovie(b);

    }

    void displayMovie(MovieData movie)
    {

        cout << "-----------------------";
        cout << "\nTitle: " << movie.title << endl;
        cout << "Director: "<< movie.director << endl;
        cout << "Year: " << movie.year << endl;
        cout << "Runing Time: " << movie.runningTime << " minutes"                 
     << endl;


    }

When a person enters the running time, they will hit enter or return afterwards. Since you have no code to read that enter or return, the next call to getline to read the title of the next movie will get it.

My suggestion is not to use code like this:

    cin >> b.year;

What you really wanted to do was read in a line , but you used code to read in a number. A common workaround is to use a cin.ignore() after each time you read in something that is not a line but where you want to read a line. I personally find this awkward, but it's quite common.

This is another solution to this problem

#include<iostream>
#include<string>

using namespace std;

struct MovieData {
string Title;
string Director;
unsigned short YearReleased;
unsigned short RunningTime;
};

void print(MovieData &md) {
cout << "Title: " << md.Title << endl
<< "Director: " << md.Director << endl
<< "Year Released: " << md.YearReleased << endl
<< "Running Time: " << md.RunningTime << " minutes" << endl;
}

int main() {
MovieData movFirst, movSecond;
movFirst.Title = "Starship Troopers";
movFirst.Director = "Paul Verhoeven";
movFirst.YearReleased = 1997;
movFirst.RunningTime = 129;
movSecond.Title = "Alien vs. Predator";
movSecond.Director = "Paul W. S. Anderson";
movSecond.YearReleased = 2004;
movSecond.RunningTime = 108;
cout << endl;
print(movFirst);
cout << endl;
print(movSecond);
cout << endl;
system("pause");
return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM