【问题标题】:How can I find in an array of objects the main actor I need如何在对象数组中找到我需要的主要角色
【发布时间】:2020-07-15 14:57:17
【问题描述】:

这是我的任务:

我已经完成了一半的代码,但我很挣扎,因为我是 OOP 的初学者,我不确定如何找到 main_actor 是安吉丽娜朱莉的电影。

    for (int i = 0; i < n; ++i)
    {
        string name;
        int year;
        string prod;
        string actor;
        cout << "\nenter the film name " ;
        cin >> name;
        cout << "\nenter the production year ";
        cin >> year;
        cout << "\nenter the producer name ";
        cin >> prod;
        cout << "\nenter the actor name ";
        cin >> actor;
        obs[i].SetName(name);
        obs[i].SetYearP(year);
        obs[i].SetProducer(prod);
        obs[i].SetMaina(actor);

        if (actor == "Angelina Jolie")
        {
            cout << "The movie who has main actor Angelina Jolie is" << name << endl;
        } // Тhis is my attempt.
    }
}

【问题讨论】:

  • 所以要找到演员是安吉丽娜·朱莉的电影,你需要做的第一件事就是编写第二个循环。首先你得到了所有的信息,只有当你得到了这些信息时,你才会尝试用它做点什么。所以删除你已有的if (actor == "Angelina Jolie") 语句并开始一个新的循环。
  • 要获取主要演员和电影名称,您应该使用您编写的 GetNameGetMaina 的 getter 函数,这就是它们的用途。
  • 您是否与您的教授或助教谈过并解释说您在其中一些概念上遇到了问题?他们付钱教你。这确实是他们的工作。

标签: c++ function oop constructor


【解决方案1】:

请使用 getline() 函数进行用户输入,因为cin &gt;&gt; name 将只保存安吉丽娜·朱莉的名字中的安吉丽娜。因为它只读取整个单词,不包括空格。

要使用 getline() 函数,请将其放在 #include&lt;cstring&gt; 之后

#include <string>

所以像这样使用getline:

cout << "\n enter the actor name ";
std::getline (std::cin,actor);

另一个问题是两个输入之间需要 cin.ignore() 。因为您需要将换行符从缓冲区中刷新出来。

在循环之前询问这样的数据:

cout << "how many films ";
cin >> n;
cin.ignore();

在这样的循环中:

cout << "\n enter the film name ";
    getline(cin, name);

    cout << "\n enter the production year ";
    cin.ignore();
    cin >> year;

    cout << "\n enter the producer name ";
    cin.ignore();
    getline(cin, prod);

    cout << "\n enter the actor name ";
    getline(cin, actor);

b) (将此函数放在您的类中字符串 GetMania() 之后的公共部分):

static void FindFilm(Film arr[], int cntFilms, string actor)
{
    for (int i = 0; i < cntFilms; i++)
    {
        if (arr[i].GetMaina() == "Angelina Jolie")
            cout << "The movie who has main actor Angelina Jolie is" << arr[i].GetName() << endl;
    }
}

并且在循环之后从 main 调用它。

string actor = "Angelina Jolie";
Film::FindFilm(obs, n, actor);

另外,最好将换行符 (\n) 的转义序列(或特殊字符)写到输出消息的末尾。像这样:

cout << "The name of movie: \n" << name;

【讨论】:

  • 我收到一条错误消息:标识符“name”未定义
  • 我没有输出,上面写着 The movie who has the main actor Angelina Jolie is ,我不知道为什么。
  • 对不起,我很抱歉,但我必须按照我的任务中所写的“(b)为a)的要求定义findFilm()函数”,我不知道你是否读过。
  • 立即查看我的答案
【解决方案2】:

您应该做的第一件事是使用 C++ 容器,例如 std::vector、std::array,而不是原始数组。当然,那么你应该填写它们。

std::vector<Films> films;

std::array<Films, 100> films;

第二件事是,你应该删除“Films() = default;”部分。该声明改变了 C++ 中的一切。

在这些改动之后,您将能够使用容器的模板成员函数和算法函数(如 find()、find_if()、count() 等)来获得所需的内容。

#include <algorithm>

如果您无法进行这些更改,只需循环即可:

 for(auto film : films){
       //comparisons, if checks, returns
    }

【讨论】:

  • 我不允许使用向量
  • 写你自己的矢量!
【解决方案3】:

您需要创建一个循环遍历数组并检查主要参与者的函数:

bool findFilm(Film* films, int numFilms, string actor)
{
    bool found = false;
    for (int i = 0; i< numFilms; i++) {
        if(!actor.compare(0, films[i].GetyMaina().length(), films[i].GetyMaina()){
            cout<<"Film "<<films[i].GetName()<<" has main actor "<<actor<<"\n";
            found = true;
         }
     }
     return found;
}

【讨论】:

  • 编译时出现错误提示:预期为 ';' .
  • @Hana 编译器应该会告诉你分号在哪里丢失。
  • @Hana 大胆猜测一下,我会说您将上述函数放在代码中的错误位置。如果放置正确,上面的代码中不会缺少分号。
  • 完全正确的地方,我把它放在第一个循环之后。
  • @Hana 把它放在你的main-function 上面。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-06
  • 1970-01-01
  • 1970-01-01
  • 2013-03-14
  • 1970-01-01
相关资源
最近更新 更多