该程序编译良好,但有一些问题出现......
我包含了所有代码,因为我不确定错误在哪里。
#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 person
{
char emplyName[SIZE];
float emplyHours;
float emplyRate;
float emplyGross;
float emplyBase;
float emplyOvrt;
float emplyTax;
float emplyNet;
float emplyTotal;
}input;
void menu(void);
void editEmployees(input* emply);
void print(input* emply);
void employeeInfo(input* emply);
void calculations(input *emply);
int main(void)
{
struct person payroll[5] = { 0 };
int choice = 0, currentEmployee = 0;
calculations(&payroll);
do
{
menu();
scanf_s("%c", &choice, 1);
switch (choice){
case '1':{
employeeInfo(&payroll[currentEmployee]);
currentEmployee;
break;
}
case '2':{
editEmployees(&payroll[currentEmployee]);
break;
}
case '3':{
print(&payroll[currentEmployee]);
break;
}
case '4':{
printAll(payroll);
break;
}
case '0':{
break;
}
default:
printf("Invalid entry\n");
}
} while (choice != 5);
system("pause");
}
void employeeInfo(input *emply)
{
printf("Enter employee name.\n");
scanf_s("%s", &emply->emplyName,SIZE);
printf("Enter employee hours.\n");
scanf_s("%f", &emply->emplyHours);
printf("Enter Hourly rate.\n");
scanf_s("%f", &emply->emplyRate);
}
void calculations(input *emply)/*Write a method that calculates the gross, base and overtime pay, pass by reference.*/
{
if (emply->emplyHours > 40) {
emply->emplyOvrt = (emply->emplyHours - 40) * (emply->emplyRate * 1.5);
}
emply->emplyGross = (((emply->emplyHours)*(emply->emplyRate)) + emply->emplyOvrt);
emply->emplyBase = (emply->emplyGross) - (emply->emplyOvrt);
emply->emplyTax = ((emply->emplyGross)*.2);
emply->emplyNet = (emply->emplyGross) - (emply->emplyTax);
emply->emplyTotal += emply->emplyGross;
}
void print(input *employees)
{
int i;
for (i = 0; i < 5; i++)
{
printf("Employee Name:%s\n", employees[i].emplyName);
printf("Hours Worked:%.2f\n ", employees[i].emplyHours);
printf("Hourly Rate:%.2f\n", employees[i].emplyRate);
printf("Gross Pay:%.2f\n", employees[i].emplyGross);
printf("Base Pay:%.2f\n", employees[i].emplyBase);
printf("Overtime Pay:%.2f\n", employees[i].emplyOvrt);
printf("Taxes Paid:%.2f\n", employees[i].emplyTax);
printf("Net Pay:%.2f\n", employees[i].emplyNet);
}
printf("Total paid to all employees : %.2f\n", employees[i].emplyTotal);
}
void printAll(input *emply)
{
int i;
for (i = 0; i < 5; i++)
{
if (strcmp(emply->emplyName[i], "-1") == 0){
break;
}
printf("Employee Name:%s\n", emply[i].emplyName);
printf("Hours Worked:%.2f\n ", emply[i].emplyHours);
printf("Hourly Rate:%.2f\n", emply[i].emplyRate);
printf("Gross Pay:%.2f\n", emply[i].emplyGross);
printf("Base Pay:%.2f\n", emply[i].emplyBase);
printf("Overtime Pay:%.2f\n", emply[i].emplyOvrt);
printf("Taxes Paid:%.2f\n", emply[i].emplyTax);
printf("Net Pay:%.2f\n", emply[i].emplyNet);
}
printf("Total paid to all employees : %.2f\n", emply[i].emplyTotal);
}
void editEmployees(input*emply)
{
int edit;
int i;
printf("which employee would you like to edit?\n");
for (i = 0; i < 5; i++){
printf("%d.%s\n", i + 1, emply[i].emplyName);
}
scanf_s("%d", &edit);
employeeInfo(&emply[edit]);
}
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");
}
答案 0 :(得分:-1)
少数事情:
如果菜单无法正常工作,则可能会保留未初始化的值。
您应真正检查scanf
的返回值。此外,您需要在调用scanf
后清除输入缓冲区。 char ch = getchar(); while (ch != EOF && ch != '\n') ch = getchar();
。
我不认为你正在编写警告;你应该。例如,currentEmployee;
绝对没有任何作用。
在editEmployee
内,您只传递一名员工。更好的方法是按值payroll
按值传递。数组将通过指针隐式传递元素 - 如果更改其中一个数组索引的值而不更改数组指向的位置,则该更改将在程序中反映出来返回主页。
您应该在 employeeInfo的末尾调用 计算。现在,它不会被召唤一次。
您应该正确地将payroll
初始化为实际值;否则,计算结果未定义。此外,`计算(&amp; payroll)是完全错误的,因为应该传递一个指向数组的指针,这显然不是你想要的。 (程序如何编译?)
这里有一些编辑代码:
#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 person
{
char emplyName[SIZE];
float emplyHours;
float emplyRate;
float emplyGross;
float emplyBase;
float emplyOvrt;
float emplyTax;
float emplyNet;
float emplyTotal;
}input;
void menu(void);
void editEmployees(input* emply);
void clear_input(void);
void print(input *employee);
void printAll(input* emply);
void employeeInfo(input* emply);
void calculations(input *emply);
int main(void)
{
struct person payroll[5] = {
{
.emplyName = "-1"
},
{
.emplyName = "-1"
},
{
.emplyName = "-1"
},
{
.emplyName = "-1"
},
{
.emplyName = "-1"
}
};
int choice = 0, currentEmployee = 0;
int valid = 0;
do
{
do {
menu();
valid = scanf_s("%c", &choice, 1);
clear_input();
} while (valid != 1);
switch (choice) {
case '1':
employeeInfo(&payroll[currentEmployee]);
break;
case '2':
editEmployees(&payroll[currentEmployee]);
break;
case '3':
print(&payroll[currentEmployee]);
break;
case '4':
printAll(payroll);
break;
case '0':
break;
default:
printf("Invalid entry\n");
break;
}
} while (choice != '0');
system("pause");
/* Required */ return 0;
}
void clear_input(void) {
char ch;
while ((ch = getchar()) != EOF && ch != '\n');
}
void employeeInfo(input *emply)
{
int valid = 0;
do {
printf("Enter employee name.\n");
valid = scanf_s("%s", &emply->emplyName,SIZE);
clear_input();
} while (valid != 1);
do {
printf("Enter employee hours.\n");
valid = scanf_s("%f", &emply->emplyHours);
clear_input();
} while (valid != 1);
do {
printf("Enter Hourly rate.\n");
valid = scanf_s("%f", &emply->emplyRate);
clear_input();
} while (valid != 1);
calculations(emply);
}
void calculations(input *emply)
/*Write a method that calculates the gross, base and overtime pay, pass by reference.*/
{
if (emply->emplyHours > 40) {
emply->emplyOvrt = (emply->emplyHours - 40) * (emply->emplyRate * 1.5);
}
emply->emplyGross = (((emply->emplyHours)*(emply->emplyRate)) + emply->emplyOvrt);
emply->emplyBase = (emply->emplyGross) - (emply->emplyOvrt);
emply->emplyTax = ((emply->emplyGross)*.2);
emply->emplyNet = (emply->emplyGross) - (emply->emplyTax);
emply->emplyTotal += emply->emplyGross;
}
void print(input *employee) {
printf("Employee Name:%s\n", employee->emplyName);
printf("Hours Worked:%.2f\n ", employee->emplyHours);
printf("Hourly Rate:%.2f\n", employee->emplyRate);
printf("Gross Pay:%.2f\n", employee->emplyGross);
printf("Base Pay:%.2f\n", employee->emplyBase);
printf("Overtime Pay:%.2f\n", employee->emplyOvrt);
printf("Taxes Paid:%.2f\n", employee->emplyTax);
printf("Net Pay:%.2f\n", emplyoyee->emplyNet);
printf("Total paid to employee : %.2f\n", employee->emplyTotal);
}
void printAll(input *emply)
{
int i;
float total = 0.0;
for (i = 0; i < 5; i++)
{
// Array index beside struct name, not member
if (strcmp(emply[i].emplyName, "-1") == 0){
continue;
}
printf("Employee Name:%s\n", emply[i].emplyName);
printf("Hours Worked:%.2f\n ", emply[i].emplyHours);
printf("Hourly Rate:%.2f\n", emply[i].emplyRate);
printf("Gross Pay:%.2f\n", emply[i].emplyGross);
printf("Base Pay:%.2f\n", emply[i].emplyBase);
printf("Overtime Pay:%.2f\n", emply[i].emplyOvrt);
printf("Taxes Paid:%.2f\n", emply[i].emplyTax);
printf("Net Pay:%.2f\n", emply[i].emplyNet);
total += emply[i].emplyTotal;
}
printf("Total paid to all employees : %.2f\n", total);
}
void editEmployees(input*emply)
{
int edit;
int i;
int valid = 0;
do {
printf("which employee would you like to edit?\n");
for (i = 0; i < 5; i++) {
printf("%d.%s\n", i + 1, emply[i].emplyName);
}
valid = scanf_s("%d", &edit);
clear_input();
} while (valid != 1);
employeeInfo(&emply[edit]);
}
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 :(得分:-1)
起初
struct person payroll[5] = { 0 };
int choice = 0, currentEmployee = 0;
这里你声明了5个结构的数组。而不是
calculations(&payroll);
你应该使用
calculations(&payroll[0]);
或
calculations(payroll);
将指针传递给数组的第一个元素
变量currentEmployee
不会更改。这条线什么都不做
currentEmployee;
此外,您的代码还包含许多其他问题。您应该在C中了解有关指针和数组的更多信息。