我有一个程序,使用工会和结构来计算每小时工人或工资工人的工资。对于一个小时工来说,他们工作的时间必须小于80.我在我的代码中实现了这个但是我得到了这个警告而不知道如何解决它。
以下是我的代码:
#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
答案 0 :(得分:0)
尝试
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
答案 1 :(得分:0)
不工作的原因与&
符号的存在或基本无关,但是因为你的逻辑有点尴尬。
你想要这个:
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);
do
{
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\n");
continue;
}
} while (0);
}
如果用户输入的数字大于80,程序只会显示"Sorry, please..."
,然后再次要求用户输入工作小时数。