C++ 在函数中使用数组
我对函数在收藏夹函数中查找条目总数的部分有问题。编译器说我正在尝试将int转换为int*。我似乎无法理解为什么它认为我正在尝试将数组转换为整数C++ 在函数中使用数组,c++,arrays,function,C++,Arrays,Function,我对函数在收藏夹函数中查找条目总数的部分有问题。编译器说我正在尝试将int转换为int*。我似乎无法理解为什么它认为我正在尝试将数组转换为整数 #include <iostream> using namespace std; enum DrinksType {COKE, PEPSI, SPRITE, DR_PEPPER}; int favorites(int sum[]); void Prompt(); int main () { int sums[4]; int number
#include <iostream>
using namespace std;
enum DrinksType {COKE, PEPSI, SPRITE, DR_PEPPER};
int favorites(int sum[]);
void Prompt();
int main ()
{
int sums[4];
int number;
int total;
DrinksType index;
for (index = COKE; index <= DR_PEPPER; index = DrinksType(index+1))
sums[index] = 0;
Prompt();
cin >> number;
while (number != 4)
{
switch(number)
{
case 0:
sums[0]++;
break;
case 1:
sums[1]++;
break;
case 2:
sums[2]++;
break;
case 3:
sums[3]++;
break;
}
Prompt();
cin >> number;
}
total = favorites (sums[4]);
cout << "Coke: " << sums[0] << endl;
cout << "Pepsi: " << sums[1] << endl;
cout << "Sprite: " << sums[2] << endl;
cout << "Dr. Pepper: " << sums[3] << endl;
cout << "The number of responses is: " << total;
return 0;
}
//*******************************************************
void Prompt()
{
cout << "Enter a 0 if your favorite is a Coke." << endl;
cout << "Enter a 1 if your favorite is a Pepsi." << endl;
cout << "Enter a 2 if your favorite is a Sprite." << endl;
cout << "Enter a 3 if your favorite is a Dr. Pepper." << endl;
cout << "Enter a 4 if you wish to quit the survey." << endl;
}
int favorites (int sum[])
{
int total = 0;
for (int i = 0; i<4; i++)
total = total + sum[i];
return total;
}
#包括
使用名称空间std;
enum饮料类型{可口可乐、百事可乐、雪碧、DR_PEPPER};
整数收藏夹(整数总和[]);
无效提示();
int main()
{
整数和[4];
整数;
整数合计;
饮品类型指数;
对于(指数=焦炭;指数>数量;
while(数字!=4)
{
开关(编号)
{
案例0:
和[0]++;
打破
案例1:
和[1]++;
打破
案例2:
和[2]++;
打破
案例3:
和[3]++;
打破
}
提示();
cin>>数量;
}
总计=收藏夹(总计[4]);
cout将数组传递给函数时,不需要使用[]
运算符:
total = favorites(sums); // not sums[4]
方括号从整数数组中取一个整数,因此编译器正在抱怨
注意:这段代码
switch(number)
{
case 0:
sums[0]++;
break;
case 1:
sums[1]++;
break;
case 2:
sums[2]++;
break;
case 3:
sums[3]++;
break;
}
可以缩短为一行:
sums[number]++; // Yes, that's it :)
最后,在进入此循环之前,您应该检查用户输入:
while (number != 4) {
...
}
因为如果恶意最终用户输入五,此循环将不会停止。将数组传递给函数时,不需要使用[]
运算符:
total = favorites(sums); // not sums[4]
方括号从整数数组中取一个整数,因此编译器正在抱怨
注意:这段代码
switch(number)
{
case 0:
sums[0]++;
break;
case 1:
sums[1]++;
break;
case 2:
sums[2]++;
break;
case 3:
sums[3]++;
break;
}
可以缩短为一行:
sums[number]++; // Yes, that's it :)
最后,在进入此循环之前,您应该检查用户输入:
while (number != 4) {
...
}
因为如果恶意最终用户输入5,此循环将不会停止。您正在调用收藏夹(sum[4])
。这就是错误。它只发送索引为4的sum数组中的值。但是,由于您需要整个数组,因此正确的语句将是
total = favourites(sum);
这将为您提供您正在调用的收藏夹(sum[4])的答案。这就是错误。它只发送索引为4的sum数组中的值。但是,由于您需要整个数组,因此正确的语句是
total = favourites(sum);
这将为您提供答案我建议严格使用数组作为输入参数,如下所示。
根据需要在
int
之前添加或删除const
template <size_t size>
void Function1(const int (&input)[size])
{
for (int i = 0; i < size; ++i)
{
std::cout << input[i] << std::endl;
}
}
模板
void Function1(常量int(&input)[size])
{
对于(int i=0;i std::cout我建议严格使用数组作为输入参数,如下所示。
根据需要在int
之前添加或删除const
template <size_t size>
void Function1(const int (&input)[size])
{
for (int i = 0; i < size; ++i)
{
std::cout << input[i] << std::endl;
}
}
模板
void Function1(常量int(&input)[size])
{
对于(int i=0;i std::你是不是在向收藏夹传递int而不是int数组
你是在向收藏夹传递int而不是int数组
谢谢你,我现在感觉自己很傻。谢谢你,我现在感觉自己很傻。