结构新手,对于如何从void函数中返回值并将其放入另一个函数中感到困惑

问题描述 投票:0回答:1

我的C ++班刚刚开始学习有关开发结构的知识。我被困在一个家庭作业问题上,要求我编写一个程序,该程序使用名为movie_data的结构和两个movie_data变量来显示有关电影的信息。我能够正确开发movie_data结构以及两个变量,以取代名为get_movie_info的函数。但是,由于我将其设置为void函数,因此无法将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;

}

c++ function structure
1个回答
3
投票

虽然@Kai使用引用的答案有效并且可以正确回答您的原始问题,但我建议您做其他事情。

首先,使用函数仅读取一个move_data并使其返回该值:

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;
}

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

movie_data movie1 = get_movie_data();

如果您需要具有可编辑的结构,则引用是一个不错的选择。要返回多个值,有更好的选择:适当大小的数组(std :: array),成对的2或对象的向量。

最好避免使用输出参数(根据经验,最好在需要时知道并打破它,因为它们很难从签名中把握并且很难跟踪。)>


3
投票

注意,您要做两次。使用函数的要点是not


0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.