我试图用C编写一个简单的程序,该程序获取输入的名称,小时费率和小时数,并计算总薪水,税金,净薪水等,但是由于我在制作一个可以打印出结果的函数而陷入困境。
我创建了两个循环:一个循环产生加班,总工资,税金和净工资。打印输出生成的数据的另一个循环。第一个工作正常。它生成的数据很好,但是在我运行程序时,它只是忽略了第二个循环并完成了程序。
这只是一个简单的void函数,没有返回值,但是我被卡住了。我通过引用传递。如果您知道怎么了,请告诉我。
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
#define size 5
char name[size][20];
float rate[size];
float hours[size];
int i = 0;
void input();
float getbasepay(float *rt, float *hr);
float getovtime(float *rt, float *hr);
float getgross(float *base, float *ovt);
float gettax(float *gross);
float getnetpay(float *gross, float *tax);
void printout(char name, float rate, float hours, float base, float ovt, float gross, float tax, float net);
void input()
{
for (i = 0; i < size; i++)
{
puts("\ntype name: (type -1 to quit) \n");
scanf_s("%19s", &name[i], 20);
if (strcmp(name[i], "-1") == 0) { break; }
puts("\ntype hourly rate: (type -1 to quit) \n");
scanf_s("%f", &rate[i]);
if (rate[i] == -1) { break; }
puts("\ntype hours worked: (type -1 to quit) \n");
scanf_s("%f", &hours[i]);
if (hours[i] == -1) { break; }
}
return ;
}
float getbasepay(float *rt, float *hr)
{ float base;
float baseo;
float excessive = (*hr - 40);
if (*hr <= 40)
{
base = (*rt) * (*hr);
return base;
}
if (*hr >= 40)
{
baseo = (*hr - excessive) * (*rt);
return baseo;
}
}
float getovtime(float *rt,float *hr)
{
float time;
float ovt;
if (*hr >= 40)
{
time = (*hr - 40);
ovt = time * (*rt) *1.5;
return ovt;
}
else
{
return 0;
}
}
float getgross(float *base, float *ovt)
{
float gross = *base + *ovt;
return gross;
}
float gettax(float *gross)
{
float tax = *gross * 0.20;
return tax;
}
float getnetpay(float *gross, float *tax)
{
float net;
net = *gross - *tax;
return net;
}
void printout(char *name, float *rate, float *hours, float *base, float * ovt, float *gross, float *tax, float *net)
{
printf("\nPay to: %s \n", *name);
printf("Hourly rate: %f \n", *rate);
printf("Hours: %f \n", *hours);
printf("Base pay: $%f \n", *base);
printf("Overtime: $%f \n", *ovt);
printf("Gross Pay: $%f \n", *gross);
printf("Tax: $%f \n", *tax);
printf("Net Pay: $%f \n", *net);
}
int main(void)
{
int o, z;
float overtime[size] = { 0,0,0,0,0, };
float basepay[size] = { 0,0,0,0,0, };
float tax[size] = { 0,0,0,0,0, };
float grosspay[size] = { 0,0,0,0,0, };
float netpay[size] = { 0,0,0,0,0, };
float sum;
input();
for (o = 0; o < i; o++)
{
basepay[o] = getbasepay(&rate[o], &hours[o]);
overtime[o] = getovtime(&rate[o], &hours[o]);
grosspay[o] = getgross(&basepay[o], &overtime[o]);
tax[o] = gettax(&grosspay[o]);
netpay[o] = getnetpay(&grosspay[o], &tax[o]);
}
for (z = 0; z > i; z++)
{
printout(&name[z], &rate[z], &hours[z], &basepay[z], &overtime[z], &grosspay[z], &tax[z], &netpay[z]);
}
}
答案 0 :(得分:0)
您的代码无法编译:
gcc -o main main.c
答案:
error: conflicting types for ‘printout’
确实,声明中的签名与您的签名不同 写在实现中(例如char * name代替char name等)