我是Stack Overflow和C ++的新手,所以如果我问这个问题的格式不正确,请原谅。我正在努力回答以下问题,我也在下面发布了我的代码,请问我需要一些指导吗?
使用两个并行数组来跟踪其每个订阅的数量 50种出版物。数组“出版物”包含已发行杂志和报纸的名称 和“订阅”数组包含每个相应杂志或 报纸。您必须编写一个称为findMostSubs的void函数来确定哪个发布 拥有最多的订阅者。函数findMostSubs必须返回发布的名称以及该发布的订阅者数量。 假定以下全局常量:NUM_PUBS = 50;
#include <iostream>
using namespace std;
const int NUM_PUBS = 5;
void findMostSubs(string pubsP[NUM_PUBS], int subsP[NUM_PUBS], string mostSubsP, int nrMostSubsP)
{
for (int i; i < NUM_PUBS; i++)
{
cout << "Please enter the name of the publication: ";
cin >> pubsP[i];
cout << "Please enter the number of subscriptions: ";
cin >> subsP[i];
}
for (int i; i < NUM_PUBS; i++)
{
nrMostSubsP = 0;
if (subsP[i] > nrMostSubsP)
{
nrMostSubsP = subsP[i];
cout << nrMostSubsP;
}
}
}
int main()
{
string publications[NUM_PUBS];
int subscriptions[NUM_PUBS];
int nrMostSubscriptions;
string mostSubscriptions;
findMostSubs(publications, subscriptions, mostSubscriptions, nrMostSubscriptions);
return 0;
}
答案 0 :(得分:0)
应该将findMostSub的最后两个参数声明为引用,以将它们用作输出:
void findMostSubs(string pubsP[NUM_PUBS], int subsP[NUM_PUBS], string& mostSubsP, int& nrMostSubsP)
{
for (int i; i < NUM_PUBS; i++)
{
cout << "Please enter the name of the publication: ";
cin >> pubsP[i];
cout << "Please enter the number of subscriptions: ";
cin >> subsP[i];
}
nrMostSubsP = subsP[0];
mostSubsP = pubsP[0];
for (int i = 1; i < NUM_PUBS; i++)
{
if (subsP[i] > nrMostSubsP)
{
nrMostSubsP = subsP[i];
mostSubsP = pubsP[i];
}
}
}
int main()
{
string publications[NUM_PUBS];
int subscriptions[NUM_PUBS];
int nrMostSubscriptions;
string mostSubscriptions;
findMostSubs(publications, subscriptions, mostSubscriptions, nrMostSubscriptions);
cout << "Most Subscriptions : " << mostSubscriptions << " , " << nrMostSubscriptions << endl;
return 0;
}