简体   繁体   English

C++ 如何在包含两个变量的结构中为我的变量存储不同数量的值?

[英]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. C++我的代码。 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这是一个学校的问题,要求: 编写一个程序,使用名为 MovieData 的结构来存储有关电影的以下信息: •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.该程序应创建两个 MovieData 变量,将值存储在它们的成员中,然后依次将每个变量传递给 function,后者以格式清晰的方式显示有关电影的信息。

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.我的问题是当用户输入第二部电影的信息时,getline(cin, b.title) 被跳过并直接转到导演。 I'm looking for why this is happening and how to fix it.我正在寻找为什么会发生这种情况以及如何解决它。 Required output vs. My output.需要 output 与我的 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.由于您没有读取输入或返回的代码,因此下一次调用getline以读取下一部电影的标题将获得它。

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.一个常见的解决方法是在每次读取不是一行而是要读取一行的内容后使用cin.ignore() 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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