C++ 如何在一系列对象中找到我需要的主要参与者
这是我的任务: 我已经完成了一半的代码,但我很挣扎,因为我是OOP的初学者,我不知道如何才能找到主角是安吉丽娜·朱莉的电影C++ 如何在一系列对象中找到我需要的主要参与者,c++,function,oop,constructor,C++,Function,Oop,Constructor,这是我的任务: 我已经完成了一半的代码,但我很挣扎,因为我是OOP的初学者,我不知道如何才能找到主角是安吉丽娜·朱莉的电影 for (int i = 0; i < n; ++i) { string name; int year; string prod; string actor; cout << "\nenter the film name " ; cin >
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.
}
}
for(int i=0;iprod;
演员;
obs[i].SetName(name);
obs[i].SetYearP(年);
obs[i].SetProducer(prod);
obs[i].SetMaina(演员);
如果(演员=“安吉丽娜·朱莉”)
{
cout请使用getline()函数进行用户输入,因为cin>>name
将仅从名称安吉丽娜·朱莉中保存安吉丽娜。因为它只读取整个单词,不包括空格
要使用函数getline(),请将其置于#include
另外,最好在输出消息的末尾为新行(\n)写入转义序列(或特殊字符)。如下所示:
cout << "The name of movie: \n" << name;
cout您需要创建一个函数,在数组上循环并检查主要参与者:
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;
}
boolfindfilm(Film*films,int numFilms,string-actor)
{
bool-found=false;
对于(int i=0;i CUT首先要做的是使用像STD::vector,STD::Road,而不是原始数组的C++容器。当然,你应该填写它们。
std::vector<Films> films;
std::array<Films, 100> films;
因此,要想找到一部演员是安吉丽娜·朱莉的电影,你首先要做的是编写第二个循环。首先,你得到了所有的信息,只有当你得到了信息后,你才会尝试用它做些什么。因此,删除if(actor==“安吉丽娜·朱莉”)
你已经有了一个新的循环,开始一个新的循环。要得到一部电影的主角和名字,你应该使用你写的getter函数GetName
和GetMaina
,这就是它们的作用。你有没有和你的教授或助教谈过,并解释过你在这些概念上有问题?他们是来教你的。这是他们的工作。我在编译时出错说:应该是“;”。@Hana编译器应该告诉你分号丢失的地方。@Hana胡乱猜测一下,我会说你在代码中把上述函数放错了位置。如果分号放在正确的位置,上面的代码中就不会丢失分号准确地说,我把它放在了的第一个循环之后。@Hana把它放在你的main
-函数的正上方。我收到一个错误,它说:标识符“name”是未定义的。我没有一个输出,它说有主要演员安吉丽娜·朱莉的电影是谁,我不知道为什么。很抱歉,我的不好,但我必须“(b)定义findFilm()函数的要求)“正如我的任务中所写的,我不知道你是否读过。现在检查我的答案我不允许使用矢量写你自己的矢量!
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);
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;
}
}
string actor = "Angelina Jolie";
Film::FindFilm(obs, n, actor);
cout << "The name of movie: \n" << name;
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;
}
std::vector<Films> films;
std::array<Films, 100> films;
#include <algorithm>
for(auto film : films){
//comparisons, if checks, returns
}