gpt4 book ai didi

c - c中的开关函数

转载 作者:行者123 更新时间:2023-11-30 18:53:51 25 4
gpt4 key购买 nike

尝试创建一个菜单驱动的员工数据程序。我不知道如何创建一个功能菜单,并且也无法使菜单选项正常工作,例如编辑之前输入的员工信息。如果我能在这方面得到任何帮助,我将不胜感激。目前出现的错误是

  1. 编辑员工功能:重新定义;不同的基本类型。

  2. main中——调用我正在使用menu(&payroll)的菜单函数时,错误消息是,无法从input*<转换输入

  3. 同样在 main 中,函数 employeeInfo(&payroll) 给出错误消息,无法从 input* 转换为 input .

我确信我还犯了很多其他错误,如果您发现任何错误,请引导我走向正确的方向。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//This is a macro intended for use with the emplyName array.
#define SIZE 20

//This struct has all the varibles that I will be using in my functions
typedef struct
{
char emplyName[5][SIZE];
float emplyHours[5];
float emplyRate[5];
float emplyGross[5];
float emplyBase[5];
float emplyOvrt[5];
float emplyTax[5];
float emplyNet[5];
float emplyTotal[5];
}input;

void menu(void);
void employeeInfo(input* emply);
void editEmployees(input* emply);
void print(input* emply);

int main(void)
{
input payroll={"",0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f,0.0f};
int choice;
menu(&payroll);
scanf_s("%d", &choice);
switch (choice){
case '1':
employeeInfo(&payroll);
break;
case '2':
editEmployees(&payroll);
break;
case '3':
break;
case '4':
print(&payroll);
break;
default:
printf("Invalid entry\n");
}
system("pause");
}

void employeeInfo(input *emply)
{
int i;

for (i = 0; i < 5; i++){
printf("Enter employee name -1 to end.\n");
scanf_s("%s", &emply->emplyName[i]);
if (strcmp(emply->emplyName[i], "-1") == 0){
break;
}

printf("Enter employee hours.\n");
scanf_s("%f", &emply->emplyHours[i]);
printf("Enter Hourly rate.\n");
scanf_s("%f", &emply->emplyRate[i]);
}
}
void calculations(input *emply)/*Write a method that calculates the gross, base and overtime pay, pass by reference.*/
{
int i;
i = 0;
for (i = 0; i < 5; i++){
if (emply->emplyHours[i] > 40) {
emply->emplyOvrt[i] = (emply->emplyHours[i] - 40) * (emply->emplyRate[i] * 1.5);
}
emply->emplyGross[i] = (((emply->emplyHours[i])*(emply->emplyRate[i])) + emply->emplyOvrt[i]);
emply->emplyBase[i] = (emply->emplyGross[i]) - (emply->emplyOvrt[i]);
emply->emplyTax[i] = ((emply->emplyGross[i])*.2);
emply->emplyNet[i] = (emply->emplyGross[i]) - (emply->emplyTax[i]);
emply->emplyTotal[0] += emply->emplyGross[i];
}


}


void print(input *emply)
{
int i;
for (i = 0; i < 5; i++)
{
if (strcmp(emply->emplyName[i], "-1") == 0){
break;
}
printf("Employee Name:%s\n", emply->emplyName[i]);
printf("Hours Worked:%.2f\n ", emply->emplyHours[i]);
printf("Hourly Rate:%.2f\n", emply->emplyRate[i]);
printf("Gross Pay:%.2f\n", emply->emplyGross[i]);
printf("Base Pay:%.2f\n", emply->emplyBase[i]);
printf("Overtime Pay:%.2f\n", emply->emplyOvrt[i]);
printf("Taxes Paid:%.2f\n", emply->emplyTax[i]);
printf("Net Pay:%.2f\n", emply->emplyNet[i]);
}
printf("Total paid to all employees : %.2f\n", emply->emplyTotal[0]);
}
void editEmployees(input *emply){
int j;
int index = 1;
int i;
printf("Choose an employee.");
for (j = 1; j < 5; j++); {
printf("%d.%s", index, emply->emplyName[j]);
}
scanf_s("%d", &i);
employeeInfo(emply);

}
void menu(void){
printf("Main Menu\n");
printf("1. Add Employee\n");
printf("2. Edit Employee\n");
printf("3. Print Employee\n");
printf("4. Print All EMployees\n");
printf("0. exit\n");

}

最佳答案

首先

1. edit employees function: redefinition; different basic types.

您需要转发声明函数原型(prototype)。

2. menu(&payroll) cannot convert from 'input*' to 'input'.

您的函数声明和定义不匹配。

  • 在声明中,您有void menu(input payroll);
  • 在定义中,您有 void menu(void){//
  • 您使用menu(&payroll);调用

以上三个都是不同的。坚持任何一个。

3. employeeInfo(&payroll) cannot convert from 'input*' to 'input'.

同样,函数声明和定义之间不匹配。更改您的函数声明

void employeeInfo(input payroll);

void employeeInfo(input* payroll);

关于c - c中的开关函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31717987/

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