Variable not defined when outside scope

2019-09-20 05:56发布

问题:

Writing​ ​a​ ​program​ ​that​ ​lets​ ​the​ ​user​ ​throw​ ​five​ ​dice​ ​and​ ​to display​ ​the​ ​result​ ​“graphically”​ ​on​ ​the​ ​screen.
The​ ​program​ ​should​ ​start​ ​by​ ​simulating​ ​five​ ​die​ ​throws​ ​by​ ​filling​ ​an​ ​array​ ​with​ ​5​ ​numbers between​ ​1​ ​and​ ​5.​ ​A function ​should​ ​then​ ​“draw”​ ​the​ ​result​ ​by​ ​displaying​ ​characters​ ​on​ ​the​ ​screen and a function who calculate the sum.
I get a error message the first function, it says I have not defined the matrix, which I defined in the "if".

#include <stdio.h> 
int sumOfDie(int inputArray[], int arraySize); 
int drawDie(int inputArray[], int arraySize)
{
int i, row, column=0;

for (i=0; i<arraySize; i++)  //determine the graphic number from the random number
{
    if (inputArray[i]==1)
    {
        char matrix [3][4] = {{"   "},{" * "},{"   "}};
    }
    if (inputArray[i]==2)
    {
        char matrix [3][4] = {{"*  "},{"   "},{"  *"}};
    }
    if (inputArray[i]==3)
    {
        char matrix [3][4] = {{"*  "},{" * "},{"  *"}};
    }
    if (inputArray[i]==4)
    {
        char matrix [3][4] = {{"* *"},{"   "},{"* *"}};
    }
    if (inputArray[i]==5)
    {
        char matrix [3][4] = {{"* *"},{" * "},{"* *"}};
    }

    for (row=0; row<3; row++) //Print out the matrix
    {
        for(column=0; column<4; column++)
        {
            printf("%c     ", matrix[row][column]);
        }
        printf("\n");
    }
}

}
int sumOfDie(int inputArray[], int arraySize) 
{
    int i, sum=0;
    for (i=0; i<arraySize; i++)
    {
        sum=sum+inputArray[i];
    }
    return sum;
}



int main(void)
{
    int i;
    int inputArry[5];
    srand(time(NULL)); 


for(i=0; i<5; i++)
{
    inputArry[i] = rand()%5+1;
}

for (i=0; i<5; i++)
{
    printf("Number:%d\n", inputArry[i]);
}

drawDie(inputArry, 5);

sum = sumOfDie(inputArray,5)
printf("The sum of %i + %i + %i + %i + %i = %i", inputArry[0], inputArry[1], inputArry[2], inputArry[3], inputArry[4], sum);

return 0;
}

回答1:

In the function drawDie, the scope of each of the variables named matrix is limited to the if statement where they are declared, so that they can't be used later to be printed.

You can collect all the strings needed to represent the dices in a single multidimentional array and then print the ones you need.

This is a possible implementation (considering a six sided dice):

#include <stdio.h>

void print_n_times_in_a_row(const char *str, int n)
{
    for ( int i = 0; i < n; ++i )
    {
        printf(" %s", str); 
    }
    puts("");
}

void draw_dices(int* values, int n)
{
    static const char dice_str[][3][8] = {
        {{"       "},{"   *   "},{"       "}},  // 1
        {{" *     "},{"       "},{"     * "}},  // 2
        {{" *     "},{"   *   "},{"     * "}},  // ...
        {{" *   * "},{"       "},{" *   * "}},
        {{" *   * "},{"   *   "},{" *   * "}},
        {{" *   * "},{" *   * "},{" *   * "}}   // 6. Just in case...
    };

    // I'll print all the "dices" in a row   
    print_n_times_in_a_row("+-------+", n);
    for ( int j = 0; j < 3; ++j )
    {
        for ( int i = 0; i < n; ++i )
        {
            printf(" |%s|", dice_str[values[i] - 1][j]); 
        }
        puts("");
    }
    print_n_times_in_a_row("+-------+", n);
}


int main(void)
{
    int dices[] = {4, 2, 5, 6, 1, 3};

    draw_dices(dices, 6);
}

Which outputs:

 +-------+ +-------+ +-------+ +-------+ +-------+ +-------+
 | *   * | | *     | | *   * | | *   * | |       | | *     |
 |       | |       | |   *   | | *   * | |   *   | |   *   |
 | *   * | |     * | | *   * | | *   * | |       | |     * |
 +-------+ +-------+ +-------+ +-------+ +-------+ +-------+


回答2:

You should not declared a conditional array even if the array is always created you should just declare your array before if statement char matrix [3][4];

After leaving the if scope, the variable does not exist anymore



回答3:

The solution to make your code work is like this, you can use a switch case to distinguish what do you want to print on screen. The array only store the index to be used in the switch.

What you did wrong was about where you declared the array inside the scope of the if statement, so, once the block inside if is executed the array does not exist anymore.

Also, the problem about how do you think in storing data inside the char array, you need to specify the sides in advance (at runtime is also possible in C99 but let's skip that), or dynamically allocate it (which is not necessary here).

So, char var can store a single byte, but you were trying to store a string {" "}, so you get error.

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int sumOfDie(int inputArray[], int arraySize);
int drawDie(int inputArray[], int arraySize)
{
    int i, row, column=0;

    char matrix[5][5];
    for (i=0; i<arraySize; i++)  //determine the graphic number from the random number
    {
        if (inputArray[i]==1)
        {
            matrix [3][4] = 0; //{{"   "},{" * "},{"   "}};
        }
        if (inputArray[i]==2)
        {
            matrix [3][4] = 1; //{{"*  "},{"   "},{"  *"}};
        }
        if (inputArray[i]==3)
        {
            matrix [3][4] = 2; //{{"*  "},{" * "},{"  *"}};
        }
        if (inputArray[i]==4)
        {
            matrix [3][4] = 3; //{{"* *"},{"   "},{"* *"}};
        }
        if (inputArray[i]==5)
        {
            matrix [3][4] = 4; //{{"* *"},{" * "},{"* *"}};
        }

        for (row=0; row<3; row++) //Print out the matrix
        {
            for(column=0; column<4; column++)
            {
                switch (matrix[row][column]) {
                    case 0:
                        // change the print accordingly
                        printf("    *     \n");
                        break;
                    case 1:
                        // change the print accordingly
                        printf("    *     \n");
                        break;
                    case 2:
                        // change the print accordingly
                        printf("    *     \n");
                        break;
                    case 3:
                        // change the print accordingly
                        printf("    *     \n");
                        break;
                    case 4:
                        // change the print accordingly
                        printf("    *     \n");
                        break;
                }
            }
            printf("\n");
        }
    }

}

int sumOfDie(int inputArray[], int arraySize)
{
    int i, sum=0;
    for (i=0; i<arraySize; i++)
    {
        sum=sum+inputArray[i];
    }
    return sum;
}



int main(void)
{
    int i, sum;
    int inputArray[5];
    srand(time(NULL));


    for(i=0; i<5; i++)
    {
        inputArray[i] = rand()%5+1;
    }

    for (i=0; i<5; i++)
    {
        printf("Number:%d\n", inputArray[i]);
    }

    drawDie(inputArray, 5);

    sum = sumOfDie(inputArray,5);
    printf("The sum of %i + %i + %i + %i + %i = %i", inputArray[0], inputArray[1], inputArray[2], inputArray[3], inputArray[4], sum);
}