1

I need to create a program that takes input for amount of food eaten by each of 3 monkeys for every day of the week. The input needs to be stored in a two dimensional array. Also, any loops must be contained in secondary functions, and not in main(). I get 2 main errors whenever I try to pass the 2-d array to a function.

1) 31:69: error: too many initializers for ‘char []’

2) 39:35: error: invalid types ‘float[int]’ for array subscript

Can someone tell me what I'm doing wrong? I think I'm incorrectly initializing the array of strings, then incorrectly passing it as a parameter, but I can't seem to figure out the correct way.

#include <iostream>     // input/output declarations
#include <iomanip>      // i/o manipulator declarations
using namespace std;

const int NUMBER_OF_MONKEYS = 3;

typedef char day[];
void getAmountEaten(float &);

int main()
{
    float monkeyFood[NUMBER_OF_MONKEYS][7];     // 3 rows for monkeys, 7 columns for days
    getAmountEaten(monkeyFood);

    return 0;
}

void getAmountEaten(float &monkeyFood)
{
    day    dayOfWeek = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
    int    monkeyNumber,
           dayNum;

    for (monkeyNumber = 0; monkeyNumber < NUMBER_OF_MONKEYS; monkeyNumber++)
        for (dayNum = 0; dayNum < 7; dayNum++)
        {
            cout << "Enter pounds of food eaten by monkey " << monkeyNumber << "on " << dayOfWeek[dayNum];
            cin  >> monkeyFood[monkeyNumber][dayNum];
        }
}
1
  • Why do they keep teaching raw C arrays before teaching the proper way to do it with container classes... And especially the intricacies of array parameters are something of advanced topic, because normally they should not be used, so why waste limited course time on teaching them. Commented Dec 2, 2013 at 6:34

2 Answers 2

1

The main problem is the argument of the function. You are trying to pass by reference a 2D array to the function but the function can only pass a single variable as an argument using pass by reference.

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

Comments

0

Since you are passing a 2D array as a reference and also you have the size of the 2D array in compile time !

use this void getAmountEaten(float (&)[3][7]);

void getAmountEaten(float (&monkeyFood)[3][7])
{
    /****/
}

instead of void getAmountEaten(float &);

For the char part :

  1. You are creating an array of characters. But you are initializing the char array of each element with an array of characters.

    here: dayOfWeek = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};

    "Sun" "Mon" occupy more space than a single character size.

  2. You are using wrong syntax of creating an array of characters.

  3. If you are creating an array of 'array of characters' then use:

    typedef char* day

    day dayOfWeek[7] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};

    But the above code would throw warnings which can be avoided using string class.

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.