在函数中使用数组

时间:2022-09-21 22:50:16

I am having problems with the part where the functions finds the total number of entries in the favorites function. The compiler says i am trying to convert an int into an an int*. I cant seem to understand why it thinks i am trying to convert the array into an integer.

我在函数查找favorites函数中条目总数的部分遇到了问题。编译器说我正在尝试将一个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;
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;
}

3 个解决方案

#1


3  

When you pass an array to a function, you do not need to use the [] operator:

将数组传递给函数时,不需要使用[]运算符:

total = favorites(sums); // not sums[4]

Square brackets take one integer from an array of integers, so the compiler is complaining.

方括号从整数数组中取一个整数,因此编译器会抱怨。

Note: this piece of code

注意:这段代码

switch(number)
{
case 0:
    sums[0]++;
    break;
case 1:
    sums[1]++;
    break;
case 2:
    sums[2]++;
    break;
case 3:
    sums[3]++;
    break;
}

can be shortened to a single line:

可以缩短为一行:

sums[number]++; // Yes, that's it :)

Finally, you should check user input before going into this loop:

最后,在进入这个循环之前,您应该检查用户输入:

while (number != 4) {
    ...
}

because if a malicious end-user enters five, this loop will not stop.

因为如果恶意终端用户进入5,这个循环不会停止。

#2


1  

You are calling favourites(sum[4]). That is the error. It only sends the value in the sum array with index 4. But there since you need the whole array, the correct statement will be,

你叫最爱(和[4])。这是错误的。它只发送索引4的和数组中的值。但是既然你需要整个数组,正确的语句应该是,

total = favourites(sum);

This will render you the answer

这将使你得到答案

#3


0  

I'll suggest strictly use array as input parameter like below.
Add or remove const before int depends on your need.

我建议严格使用数组作为输入参数,如下所示。根据需要,在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;
    }
}

If your array is fixed size then you can remove the template thing.

如果数组是固定大小的,那么可以删除模板。

#1


3  

When you pass an array to a function, you do not need to use the [] operator:

将数组传递给函数时,不需要使用[]运算符:

total = favorites(sums); // not sums[4]

Square brackets take one integer from an array of integers, so the compiler is complaining.

方括号从整数数组中取一个整数,因此编译器会抱怨。

Note: this piece of code

注意:这段代码

switch(number)
{
case 0:
    sums[0]++;
    break;
case 1:
    sums[1]++;
    break;
case 2:
    sums[2]++;
    break;
case 3:
    sums[3]++;
    break;
}

can be shortened to a single line:

可以缩短为一行:

sums[number]++; // Yes, that's it :)

Finally, you should check user input before going into this loop:

最后,在进入这个循环之前,您应该检查用户输入:

while (number != 4) {
    ...
}

because if a malicious end-user enters five, this loop will not stop.

因为如果恶意终端用户进入5,这个循环不会停止。

#2


1  

You are calling favourites(sum[4]). That is the error. It only sends the value in the sum array with index 4. But there since you need the whole array, the correct statement will be,

你叫最爱(和[4])。这是错误的。它只发送索引4的和数组中的值。但是既然你需要整个数组,正确的语句应该是,

total = favourites(sum);

This will render you the answer

这将使你得到答案

#3


0  

I'll suggest strictly use array as input parameter like below.
Add or remove const before int depends on your need.

我建议严格使用数组作为输入参数,如下所示。根据需要,在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;
    }
}

If your array is fixed size then you can remove the template thing.

如果数组是固定大小的,那么可以删除模板。