结构新手,我对如何从 void 函数中提取 return 值并将其放入另一个函数感到困惑

New to structures, I'm confused about how to return values out of a void function and put it into another function

我的 C++ class 刚刚开始学习开发结构。我遇到了一个家庭作业问题,要求我编写一个程序,该程序使用名为 movie_data 的结构和两个 movie_data 变量来显示有关电影的信息。我能够正确开发 movie_data 结构以及两个变量以外包给名为 get_movie_info 的函数。但是,因为我将它设置为 void 函数,所以我无法 return 将 get_movie_function 生成的任何内容发送到我的 movie_display 函数。我尝试将我的函数重写为 movie_data 结构数据类型,但这似乎让事情变得更糟。第一个函数正确生成所有信息,但第二个函数不输出任何内容。谢谢你的时间。

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


    struct movie_data
    {
        string title;
        string director;
        int year_released;
        int running_time;
    };

    //Function Prototype
    void get_movie_info(movie_data movie1, movie_data movie2);
    void movie_display(movie_data movie1, movie_data movie2);

int main()
{
    movie_data movie1;
    movie_data movie2;

    get_movie_info(movie1, movie2);
    movie_display(movie1, movie2);

    return 0;
}

    void get_movie_info(movie_data movie1, movie_data movie2)
{
    //Get movie_data's title
    cout << "Enter the title for the first movie: ";
    //cin.ignore();
    getline(cin, movie1.title);
    cout << movie1.title << endl;

    //Get movie_data's director
    cout << "Enter the director's name for " << movie1.title << ": ";
    //cin.ignore();
    getline(cin, movie1.director);
    cout << movie1.director << endl;

    //Get movie_data's release year
    cout << "Enter the release year for " << movie1.title << ": ";
    cin >> movie1.year_released;
    cout << movie1.year_released << endl;

    //Get movie_data's running time
    cout << "Enter the runtime of " << movie1.title << " in minutes: ";
    cin >> movie1.running_time;
    cout << movie1.running_time << " minutes" << endl;

    //Get movie_data's title
    cout << "Enter the title for the second movie: ";
    cin.ignore();
    getline(cin, movie2.title);
    cout << movie2.title << endl;

    //Get movie_data's director
    cout << "Enter the director's name for " << movie2.title << ": ";
    //cin.ignore();
    getline(cin, movie2.director);
    cout << movie2.director << endl;

    //Get movie_data's release year
    cout << "Enter the release year for " << movie2.title << ": ";
    cin >> movie2.year_released;
    cout << movie2.year_released << endl;

    //Get movie_data's running time
    cout << "Enter the runtime of " << movie2.title << " in minutes: ";
    cin >> movie2.running_time;
    cout << movie2.running_time << " minutes" << endl;

}

void movie_display(movie_data movie1, movie_data movie2)
{
    //Display movie1 information
    cout << "\nBelow is the data of the first movie:\n";
    cout << "Movie Title:  " << movie1.title << endl;
    cout << "Director's Name:  " << movie1.director << endl;
    cout << "Release Year:  " << movie1.year_released << endl;
    cout << "Movie Runtime in minutes:  " << movie1.running_time << endl;

    //Display the movie information
    cout << "\nBelow is the data of the second movie:\n";
    cout << "Movie Title:  " << movie2.title << endl;
    cout << "Director's Name:  " << movie2.director << endl;
    cout << "Release Year:  " << movie2.year_released << endl;
    cout << "Movie Runtime in minutes:  " << movie2.running_time << endl;

}

更新您的函数签名以获取引用而不是值。

https://www.learncpp.com/cpp-tutorial/72-passing-arguments-by-value/

void get_movie_info(movie_data& movie1, movie_data& movie2)
void movie_display(const movie_data& movie1, const movie_data& movie2)

虽然@Kai 关于使用引用的回答会起作用并且会正确回答您原来的问题,但我建议您做些其他事情。

首先,使用函数只读入一个 move_data 并使其 return 为:

movie_data get_movie_info();

一个可能的实现(使用您的代码)可能是这样的:

movie_data get_movie_info(){
    movie_data movie; 

    cout << "Enter the title for the first movie: ";
    getline(cin, movie.title);

    cout << "Enter the director's name for " << movie.title << ": ";
    getline(cin, movie.director);

    cout << "Enter the release year for " << movie.title << ": ";
    cin >> movie.year_released;

    cout << "Enter the runtime of " << movie.title << " in minutes: ";
    cin >> movie.running_time;

    return movie;
}

现在您可以调用它两次来读取您的信息,它将return电影数据作为正确的结构。

movie_data movie1 = get_movie_data();

如果您需要可以编辑的结构,引用是一个不错的选择。对于 returning 多个值,有更好的选择:一个合适大小的数组 (std::array),一对二,或对象向量。

最好避免使用输出参数(根据经验,如果需要就打破它并知道为什么),因为它们很难从签名中掌握并且很难跟踪。

注意,你每件事都做了两次。使用函数的要点是 而不是 每件事都做两次,所以你应该写一个函数来做一件事,然后用不同的参数调用它。例如在 get_movie_info 中。更好的设计是创建一个函数,它只创建一个 movie_data 和 returns 它:

movie_data get_movie_info()
{
    movie_data result = {}; // That's the variable were we store the data

    //Get movie_data's title ...
    //Get movie_data's director ...
    //Get movie_data's release year ... 
    //Get movie_data's running time ...

    return result; // return the created movie data
}

movie_display也是如此。不要创建一个对两个参数执行完全相同操作的函数,而是创建一个执行一次并调用两次的函数:

void movie_display(movie_data movie)
{
    cout << "Movie Title:  " << movie.title << endl;
    //And so on ...
}

然后在 main 中将两者结合起来,如下所示:

int main()
{
    movie_data movie1 = get_movie_info();
    movie_data movie2 = get_movie_info();

    std::cout << "data of the first movie:\n";
    movie_display(movie1);
    std::cout << "data of the second movie:\n";
    movie_display(movie2);

    return 0;
}