-5

Code:

void case1();
void case2();
void case3();
void case4();
void case5();

//Global variables
const int MAXROW = 5;
const int MAXCOL = 5;

int main()
{
    ........
    ...
MENU(with do while)
}

void case1()
{
    int A[MAXROW][MAXCOL] = { 0 };

    for (int r = 0; r < MAXROW; ++r)
        for (int c = 0; c < MAXCOL; ++c) {
            cout << "\n A[" << r << "][" << c << "]= ";
            cin >> A[r][c];
        }
}

void case2()
{
    ...
}

//Function to find average of negatives
void case3()
{
    int negNumber = 0;
    double average = 0;

    for (int r = 0; r < 6; ++r) {
        for (int c = 0; c < 6; ++c) {
            if...
}

void case4()
{//Function to find product of numbers different from 0
    ...
}

void case5()
{
    ...
}

As you can see the input array is in case1(). What I wonder is how to use this array in all other functions (case2, case3, case4, case5).

How can I do this?

How to call them in the menu? For example case2():

case '2': {
    case2(......);

For now my error list is full of messages like

'A': undeclared identifier

anatolyg
  • 26,506
  • 9
  • 60
  • 134
Fingolfin
  • 17
  • 7
  • 2
    Please [edit] your question with a [mcve]. – YSC Jan 05 '16 at 16:55
  • You got it slightly shorter by dropping complete completely, but are still not that much nearer to minimal. I suggest you delete it, take some time reformulating (and rebuilding your example), and only when you are ready undelete and edit it. (As-is, you'll just collect down- and close-votes...) – Deduplicator Jan 05 '16 at 17:19
  • Any arrays defined locally in functions can't be seen inside other functions. As you're asking you need to define that array at global scope (not that I consider this a good idea). – πάντα ῥεῖ Jan 05 '16 at 17:28

2 Answers2

0

You can pass array by reference, for example:

void case1(int (&A)[MAXROW][MAXCOL])
{
  // do use array here
}
0

You should declare your array in the main function, not in one of your case... functions. In C++, objects (including arrays) are destroyed once the execution passes out of the block in which the object is defined. So in your code

void case1()
{
    int A[MAXROW][MAXCOL] = { 0 };
    ...
}

once the execution leaves the function case1, the array A is destroyed. The compiler tries to tell you this, when it complains about unknown name A. This is not what you want! Instead, do this:

int main()
{
    int A[MAXROW][MAXCOL] = { 0 };
    ...
    // your menu code
    ...
    switch (menu_result)
    {
    case 1: case1(A); break;
    case 2: case2(A); break;
    case 3: case3(A); break;
    case 4: case4(A); break;
    case 5: case5(A); break;
    }
}

Here, you are passing the array A to your functions. In order to make this code work, you should declare that your functions receive the array as a parameter:

void case1(int (&A)[MAXROW][MAXCOL])
{
  // do use array here
}

(code taken from this answer)


BTW the pass-by-reference syntax is rarely used for arrays, because the code works even if you omit the reference sign &:

void case2(int A[MAXROW][MAXCOL]) // also works; does the same
{
}

In this case, you can also omit the outer dimension of the array:

void case3(int A[][MAXCOL]) // also works; does the same
{
}

The reason these two variants also work is a bit technical - in these variants, the thing being passed is a pointer to a one-dimensional array, not a reference to a two-dimensional array. I wanted to avoid mentioning them, but it's not really possible, because they are widely used (possibly because they can be compiled by a C compiler).

Community
  • 1
  • 1
anatolyg
  • 26,506
  • 9
  • 60
  • 134