gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 15:29:10 24 4
gpt4 key购买 nike

我的 C++ 类(class)刚刚开始学习开发结构。我遇到了一个家庭作业问题,要求我编写一个程序,该程序使用名为 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;

最佳答案

虽然@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)、两个 Pair 或对象 vector 。

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

关于c++ - 结构新手,我对如何从 void 函数返回值并将其放入另一个函数感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59096354/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com