Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/65.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
带数组的Switch语句_C_Arrays_Switch Statement - Fatal编程技术网

带数组的Switch语句

带数组的Switch语句,c,arrays,switch-statement,C,Arrays,Switch Statement,我在代码中遇到了一个我无法理解的错误。我同时使用数组和switch语句。如果用户输入了无效的选择,则程序将设置默认值,然后返回以请求用户输入。默认选择后,不会存储用户输入的数据。这仅适用于紧跟在默认选择之后的循环。下面是我代码的主体 //要求用户最多输入20个数字,找到数字的平均值并向用户显示平均值 main() { double number[SIZE] = { 0 }; int i; int selection = 0, numCount = 0; dou

我在代码中遇到了一个我无法理解的错误。我同时使用数组和switch语句。如果用户输入了无效的选择,则程序将设置默认值,然后返回以请求用户输入。默认选择后,不会存储用户输入的数据。这仅适用于紧跟在默认选择之后的循环。下面是我代码的主体

//要求用户最多输入20个数字,找到数字的平均值并向用户显示平均值

main()
 {

    double number[SIZE] = { 0 };
    int i;
    int selection = 0, numCount = 0;
    double total = 0, average = 0;


    while(selection != 4){

    for (i = 0; i <= SIZE; i++)

    {

        printf("\nPress 1 to enter a number, you may enter up to 20\nPress 2 to display your numbers\nPress 3 to see the average of your numbers\nPress 4 to quit\n");
        scanf_s("%i", &selection);

        switch (selection) {
        case 1:

            if (numCount < SIZE) {
                //prompt user for input and store data
                printf("Please enter your number\n");
                scanf_s("%lf", &number[i]);
                numCount++;
                total += number[i];
            }
            else
                printf("\nThe array is full, choose another selection\n");
            break;

        case 2:

            if (numCount != 0)
        {
            //displays input back to user
            for (i = 0; i < numCount; i++)
                printf("%.2lf\t", number[i]);
        }
        else
            printf("You must input at least one number first\n");
            break;

        case 3:
            if (numCount != 0)

            {
                //calculates average of only numbers entered into the array by the user
                average = total / numCount;
                printf("The average of your numbers is %.2lf\n", average);
                break;
            }
            else
                printf("You must input at least one number first\n");

        case 4:
        {
            printf("Thank you\n\n");
            return;
        }
        default:
        {
            printf("This is not a valid selection, please try again\n\n");
            break;
        }

        } 

    }
}
}   

main()
{
双倍数字[大小]={0};
int i;
int selection=0,numCount=0;
双倍合计=0,平均=0;
while(选择!=4){

对于(i=0;i,您的问题是什么?为什么在案例4中需要大括号,而在案例3中则需要默认值?在哪里是break?有两个问题:(1)您正在为两个不同的循环使用
i
(请参见
case2
)-这将导致各种问题(将内部循环更改为不同的索引)。(2)您在每个循环上增加
i
,并将其用作输入编号的索引,即使在非输入选择之后也是如此。非常感谢!五秒钟的修复。