Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.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
C打印时变量丢失值_C_Variables_Logic - Fatal编程技术网

C打印时变量丢失值

C打印时变量丢失值,c,variables,logic,C,Variables,Logic,发生的情况如下:我给变量“pay”一个值。16例如。我用printf打印pay,然后我得到16。然后我再次打印它,得到0。如果我第三次打印,我会得到16 此外,无论我做什么,我似乎总是得到0的员工类型 感谢您的帮助。谢谢 #include<stdlib.h> #include<stdio.h> int main(void) { float pay; int employees; int type; int hours; int items; int i; int j

发生的情况如下:我给变量“pay”一个值。16例如。我用printf打印pay,然后我得到16。然后我再次打印它,得到0。如果我第三次打印,我会得到16

此外,无论我做什么,我似乎总是得到0的员工类型

感谢您的帮助。谢谢

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

int main(void)
{

float pay;

int employees;
int type;
int hours;
int items;
int i;
int j;

printf("How many employees? ");
scanf("%d", &employees);

float array[employees][2];

for(i = 0; i < employees; i++)
{
    printf("\nWhat type of employee is employee #%d?", i + 1);
    printf("\nEnter \"1\" for Manager.\nEnter \"2\" for Hourly Worker.\nEnter \"3\" for Commission Worker.\nEnter \"4\" for Pieceworker.");
    scanf("%d", &type);

    switch (type) 
    {
        case 1:
            array[i][0] = 1;
            printf("\nWhat is the weekly pay for employee #%d (manager)?", i + 1);
            scanf("%f", &pay);

            array[i][1] = pay;
            printf("\nEmployee #%d earned $%.2f this week.", i + 1, array[i][1]);

            break;

        case 2:
            array[i][0] = 2;
            printf("\nWhat is the hourly pay for employee #%d (Hourly Worker)?", i + 1);
            scanf("%f", &pay);

            printf("\nHow many hours did employee #%d work this week?", i + 1);
            scanf("%d", &hours);

            if (hours > 40)
            {
                pay = ((hours - 40) * pay * 1.5) + (hours * pay);
            }
            else
                pay *= hours;

            array[i][1] = pay;
            printf("\nEmployee #%d earned $%.2f this week.", i + 1, array[i][1]);

            break;

        case 3:
            array[i][0] = 3;
            printf("\nWhat were the gross sales for employee #%d (Commission Worker)?", i + 1);
            scanf("%f", &pay);

            pay = (pay *.057) + 250;

            array[i][1] = pay;
            printf("\nEmployee #%d earned $%.2f this week.", i + 1, array[i][1]);

            break;

        case 4:
            array[i][0] = 4;
            printf("\nHow many items did employee #%d (Pieceworker) produce this week?", i + 1);
            scanf("%d", &items);

            printf("\nHow much does employee #%d get paid for every item they produce?", i + 1);
            scanf("%f", &pay);

            pay *= items;

            array[i][1] = pay;
            printf("\nEmployee #%d earned $%.2f this week.", i + 1, array[i][1]);

            break;
    }
    array[i][0] = type;
    printf("\n\nEmployee type: %d", array[i][0]);
    printf("\nEmployee #%d earned $%.2f this week.", i + 1, array[i][1]);


}

printf("\n\nEmployee # | Employee Type | Weekly Pay");

for(j = 0; j < employees; j++)
{
    printf("\n%10d | %13d | $%.2f", j + 1, array[j][0], array[j][1]);
}

}
#包括
#包括
内部主(空)
{
浮动工资;
int员工;
int型;
整小时;
国际项目;
int i;
int j;
printf(“有多少员工?”);
scanf(“%d”、&employees);
浮动数组[employees][2];
对于(i=0;i40)
{
工资=((小时数-40)*工资*1.5)+(小时数*工资);
}
其他的
工资*=小时;
数组[i][1]=支付;
printf(“\nEmployee#%d本周赚了%.2f美元。”,i+1,数组[i][1]);
打破
案例3:
数组[i][0]=3;
printf(“\n员工#%d(佣金工)的总销售额是多少?”,i+1);
scanf(“%f”、&pay);
薪酬=(薪酬*.057)+250;
数组[i][1]=支付;
printf(“\nEmployee#%d本周赚了%.2f美元。”,i+1,数组[i][1]);
打破
案例4:
数组[i][0]=4;
printf(“\n员工#%d(计件工人)本周生产了多少件物品?”,i+1);
scanf(“%d”项和项目);
printf(“\n员工#%d从他们生产的每件商品中获得多少报酬?”,i+1);
scanf(“%f”、&pay);
支付*=项目;
数组[i][1]=支付;
printf(“\nEmployee#%d本周赚了%.2f美元。”,i+1,数组[i][1]);
打破
}
数组[i][0]=类型;
printf(“\n\n雇员类型:%d”,数组[i][0]);
printf(“\nEmployee#%d本周赚了%.2f美元。”,i+1,数组[i][1]);
}
printf(“\n\n员工#|员工类型|周工资”);
对于(j=0;j
编辑:现在我可以打印工资值了。但是,当我尝试打印员工类型时,仍然得到0

另外,打印图表的最后一个循环仍然会在两个值上返回0。有什么想法吗?(以上代码已更新)

浮点数组[employees-1][2]; : 对于(i=0;i 您正在访问数组的边界之外

当您定义类似于
array[42]
的内容时,数组元素由
0
41
的值索引。C使用基于零的索引

您似乎在两个维度上都超越了界限,首先是使用
employees-1
创建第一个维度,其次是使用
2
作为第二个维度的索引,尽管您只应使用
0
1

在尝试解决任何其他问题之前,我会先修复未定义的行为。

float数组[employees-1][2];
:
对于(i=0;i
您正在访问数组的边界之外

当您定义类似于
array[42]
的内容时,数组元素由
0
41
的值索引。C使用基于零的索引

您似乎在两个维度上都超越了界限,首先是使用
employees-1
创建第一个维度,其次是使用
2
作为第二个维度的索引,尽管您只应使用
0
1

在尝试解决任何其他问题之前,我会先修复未定义的行为。

float数组[employees-1][2];
:
对于(i=0;i
您正在访问数组的边界之外

当您定义类似于
array[42]
的内容时,数组元素由
0
41
的值索引。C使用基于零的索引

您似乎在两个维度上都超越了界限,首先是使用
employees-1
创建第一个维度,其次是使用
2
作为第二个维度的索引,尽管您只应使用
0
1

在尝试解决任何其他问题之前,我会先修复未定义的行为。

float数组[employees-1][2];
:
对于(i=0;i
您正在访问数组的边界之外

当您定义类似于
array[42]
的内容时,数组元素由
0
41
的值索引。C使用基于零的索引

您似乎在两个维度上都超越了界限,首先是使用
employees-1
创建第一个维度,其次是使用
2
作为第二个维度的索引,尽管您只应使用
0
1

我会修好那个不设防的
float array[employees - 1][2];
:
for(i = 0; i < employees; i++)
    :
    blah blah blah ... array[i][2] ...