2

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.

#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;
}
1
  • You are passing an int instead of array of ints to favorites Commented Dec 3, 2013 at 4:07

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, I officially feel dumb now.
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,

total = favourites(sum);

This will render you the answer

Comments

0

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

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.