gpt4 book ai didi

c - 为什么总是崩溃?

转载 作者:行者123 更新时间:2023-11-30 21:43:59 25 4
gpt4 key购买 nike

当我选择添加客户端时,一旦输入客户端 ID,该条目的程序就会崩溃,您知道吗?

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

struct client
{
int clID;
char cname;
char caddress;
char cemail;
int cfees;
int ceID;
char cename;
}typedef client;

struct employee
{
int empID;
char ename;
double erate;
double ehours;
double esalary;
int ecID;
}typedef employee;

void mainMenu();
void clientMenu();
void empMenu();
void getClient(client* pcli);
void getEmp(employee* pemp);
void payroll(employee* pemp);
void dispPay(employee* pemp);
void dispClient(client* pcli);
void dispEmployee(employee* pemp);

int main()
{
client cli[100];
client* pcli = &cli[0];
employee emp[20];
employee* pemp = &emp[0];
int answer = -1;
int mchoice;
int cchoice;
int echoice;
int ccount;
int ecount;
int input[9];
int* psearchclientID;
int i;

printf("Do you wish to start the program? 1 for yes 2 for no: ");
scanf("%d", &answer);
if(answer ==1)
{
while(mchoice != 3)
{
mainMenu();
scanf("%d", &mchoice);
switch(mchoice)
{
case 1: while(cchoice != 3)
{
clientMenu();
scanf("%d", &cchoice);
switch(cchoice)
{
case 1: getClient(pcli + i);
ccount++;
break;
case 2: printf("Enter the client ID to search for: ");
psearchclientID = fgets(input, 9, stdin);
strtok(input, "\n");
for(i = 0; i < 1; i++)
{
if(strcmpi(psearchclientID, (pcli->clID + i)) == 0)
printf("Client found at position %d\n", i);
else
printf("Client not found!");
}//end for
break;
}//end client switch
}//end client while
cchoice = 0;
break;
case 2: while(echoice != 4)
{
empMenu();
scanf("%d", &echoice);
switch(echoice)
{
case 1: getEmp(pemp + i);
ecount++;
break;
case 2: payroll(pemp + i);
dispPay(pemp + i);
break;
case 3:
break;
}//end emp switch
}//end emp while
echoice =0;
break;
}//end switch
}//end main while
}//end if
else if(answer ==2)
{
printf("Goodbye!");
exit(0);
}
return 0;
}//end main

void mainMenu()
{
printf("1-Client Menu\n"
"2-Employee Menu\n"
"3-Quit\n");
printf("Enter a choice from the menu: ");
}//end mainMenu

void clientMenu()
{
printf("1-Add a client\n"
"2-Search client\n"
"3-Go Back to Main Menu\n");
printf("Enter a choice from the menu: ");
}//end clientMenu

void empMenu()
{
printf("1-Add an Employee\n"
"2-Process an Employee(payroll)\n"
"3-Search Employee\n"
"4-Go Back to Main Menu\n");
printf("Enter a choice from the menu: ");
}//end empMenu

这是专门用于输入客户信息的代码

void getClient(client* pcli)
{
printf("Enter client ID: ");
scanf("%d", &pcli->clID);
printf("Enter client name: ");
scanf("%s", &pcli->cname);
printf("Enter client address: ");
scanf("%s", &pcli->caddress);
printf("Enter client email: ");
scanf("%s", &pcli->cemail);
printf("Enter monthly service fees:" );
scanf("%d", &pcli->cfees);
}//end getClient

void getEmp(employee* pemp)
{
printf("Enter employee ID: ");
scanf("%d", &pemp->empID);
printf("Enter employee name: ");
scanf("%s", &pemp->ename);
printf("Enter employee hourly rate: ");
scanf("%lf", &pemp->erate);
printf("Enter employee hours worked: ");
scanf("%lf", &pemp->ehours);
}//end getEmp

void payroll(employee* pemp)
{
pemp->esalary = pemp->erate * pemp->ehours;
}//end payroll

void dispPay(employee* pemp)
{
printf("Employee ID %d\nEmployee Salary: %2.2f\n", pemp->empID, pemp->esalary);
}//end dispPay

这是显示信息的地方

  void dispClient(client* pcli)
{
printf("Client ID: %d\n Name: %s\n Address: %s\n Email: %s\n Monthly fees: %d\n Employee assigned: %d\n Employee name: %s\n", pcli->clID, pcli->cname, pcli->caddress, pcli->cemail, pcli->cfees, pcli->ceID, pcli->cename);
}//end dispClient

void dispEmployee(employee* pemp)
{
printf("Employee ID: %d\n Name: %s\n Hourly Rate: %2.2f\n Hours worked: %2.2f\n Salary: %2.2f\n Client(s) assigned: %s\n", pemp->empID, pemp->ename, pemp->erate, pemp->ehours, pemp->esalary, pemp->ecID);
}//end dispEmp

最佳答案

读取时不包含 %s(字符串)的 &(地址)运算符。例如,在函数 getClient 中,使用 printf("请输入员工姓名:"); scanf("%s", pemp->ename);

这是您的程序中的问题之一。

关于c - 为什么总是崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27288626/

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