gpt4 book ai didi

c - 多用途薪资系统 C

转载 作者:太空宇宙 更新时间:2023-11-04 00:01:40 30 4
gpt4 key购买 nike

我有一个程序使用 union 和结构来计算小时工或薪水 worker 的工资。对于小时工,他们的工作时间必须少于 80 小时。我在我的代码中实现了这一点,但我收到此警告并且不知道如何修复它。 This is the Warning I get

下面是我的代码:

#include <stdio.h>
#include <ctype.h>
#define MAXSIZE 4000

union Employee { //union
struct HourlyPaid { //Struct for Hourly Workers
char name[20];
char Gender[20];
int HoursWorked;
float HourlyRate;
} HourlyPaid;

struct Salary { //struct for Salaried workers
char name[50];
int age;
float salary;
float bonus;
} Salary;
} Employee;
int main(void)
{
int i = 0;
int n = 0;
char option[2];
char s[MAXSIZE];
while(1)
{
puts("\nMULTIPURPOSE PAYROLL SYSTEM");
puts("Please Select an Option to Continue:");
puts("Option A: Calculating Pay for an HOURLY worker");
puts("Option B: Calculating Pay for an SALARIED worker\n");
puts("PRESS ANY OTHER BUTTON TO EXIT\n");
fgets(option, MAXSIZE, stdin);
//putchar(option);

if((option[0]=='a')||(option[0]=='A')) //Condition for Hourly Workers
{
puts("Please enter how many salaries you wish to input:\t");
scanf("%d", &n);
struct HourlyPaid x[n];

for(i = 0; i < n; i++)
{
fputs("Enter Name: ", stdout);
scanf("%s", x[i].name);

fputs("Enter Gender: ", stdout);
scanf("%s", x[i].Gender);

fputs("Enter Hours Worked: ", stdout);
scanf("%d", &x[i].HoursWorked);

fputs("Enter Hourly Rate: ", stdout);
scanf("%f", &x[i].HourlyRate);
}

if (&x[i].HoursWorked > 80 )
{
puts ("Sorry please enter a value less than 80 for hours worked");
}
else
for (i = 0; i < n; i++)
{
printf("\n\tCalculated Salary #%d\n", (i+1));
puts("\t===============================");
printf("\n\tName %d.............%s \n", (i+1),x[i].name);
printf("\tGender %d...........%s \n", (i+1), x[i].Gender);
printf("\tHours Worked %d.....%d \n", (i+1), x[i].HoursWorked);
printf("\tHourly Rate %d......%.2f \n", (i+1), x[i].HourlyRate);
printf("\n\tTotal Salary %d.....%.2f \n", (i+1), (x[i].HourlyRate*x[i].HoursWorked));
puts("\t===============================");
} //End of for
} //End of if

else if((option[0]=='b')||(option[0]=='B')) //Condition for Salaried Workers
{
puts("Please enter how many salaries you wish to input:\t");
scanf("%d", &n);
struct Salary x[n];
//int i;
for(i = 0; i < n; i++)
{
printf("Enter Name %d: ", i+1);
scanf("%s", x[i].name);

printf("Enter Age %d: ", i+1);
scanf("%d", &x[i].age);

printf("Enter Salary %d: ", i+1);
scanf("%f", &x[i].salary);

printf("Enter Bonus %d: ", i+1);
scanf("%f", &x[i].bonus);
}

for (i = 0; i < n; i++)
{
printf("\n\tCalculated Salary #%d\n", (i+1));
puts("\t============================");
printf("\n\tName %d is..........%s \n", (i+1),x[i].name);
printf("\tAge %d is...........%d \n", (i+1), x[i].age);
printf("\tSalary %d is........%.2f \n", (i+1), x[i].salary);
printf("\tBonus %d is.........%.2f \n", (i+1), x[i].bonus);
printf("\n\tTotal Pay %d is.....%.2f \n", (i+1), (x[i].bonus+x[i].salary));
puts("\t============================");
}
} //End of else if
else
{
return 0;
}
fgets(option, MAXSIZE, stdin);
} //End of While
} //End of Main

最佳答案

尝试

        for(i = 0; i < n; ) // note no i++ here
{
fputs("Enter Name: ", stdout);
scanf("%s", x[i].name);

fputs("Enter Gender: ", stdout);
scanf("%s", x[i].Gender);

fputs("Enter Hours Worked: ", stdout);
scanf("%d", &x[i].HoursWorked);

fputs("Enter Hourly Rate: ", stdout);
scanf("%f", &x[i].HourlyRate);

if (x[i].HoursWorked > 80 )
{
puts ("Sorry please enter a value less than 80 for hours worked");
} else {
i++; //note increment i only if the value is good
}
}

这不是一个理想的解决方案。如果 HoursWorked > 80

,您可能需要再次输入所有详细信息

关于c - 多用途薪资系统 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40647845/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com