修复某些部分的错误输出

时间:2019-02-19 06:26:32

标签: c switch-statement

在给我的指示中,在接近结尾处,员工数据必须在较早删除列“ 333”之后按222、666、444和555的顺序显示每一列。我需要帮助将“ 666”列放入“ 333”列的原始位置。

#define _CRT_SECURE_NO_WARNINGS
#define SIZE 4

#include <stdio.h>

struct employee {

int number;
int age;
double salary;
};

int main(void) {
struct employee emp[SIZE] = { { 0,0,0 } }; //struct w/ array
int option = 1; //option variable
int nEmp = 0; //counting how many employee we have so far
int empIndex = 0;
int i, check, sNumber;


printf("---=== EMPLOYEE DATA ===---\n\n");

while (option != 0) {
 printf("1. Display Employee Information\n");
 printf("2. Add Employee\n");
 printf("3. Update Employee Salary\n");
 printf("4. Remove Employee\n");
 printf("0. Exit\n\n");
 printf("Please select from the above options: ");
 scanf("%d", &option);
 printf("\n");

 switch (option)
 {
 case 1: //print employee information
  printf("EMP ID  EMP AGE EMP SALARY\n");
  printf("======  ======= ==========\n");

  for (i = 0; i < nEmp; i++) {

   if (emp[i].number > 0 && emp[i].age > 0 && emp[i].salary)

   printf("%d       %d   %.2lf\n", emp[i].number, emp[i].age, emp[i].salary);
  }
  printf("\n");

  break;

 case 2: //add employee
  printf("Adding Employee\n");
  printf("===============\n");

  if (nEmp < SIZE) {

   empIndex = 0;

   while ((emp[empIndex].number != 0) && (empIndex < SIZE)) {
    empIndex++;
   }

   printf("Enter Employee ID: ");
   scanf("%d", &emp[empIndex].number);
   printf("Enter Employee Age: ");
   scanf("%d", &emp[empIndex].age);
   printf("Enter Employee Salary: ");
   scanf("%lf", &emp[empIndex].salary);
   printf("\n");
   nEmp++; 

  }
  else {

   printf("ERROR!!! Maximum Number of Employees Reached\n\n");
  }

  break;

 case 3: //update employee
  printf("Update Employee Salary\n");
  printf("======================\n");

  if (nEmp == 0) { //alternative just in case there is no employee yet
   printf("\nNo employee to update\n\n");

   break;
  }
  do
  {
   check = 1;

   printf("Enter Employee ID: ");
   scanf("%d", &sNumber);

   for (i = 0; i < SIZE; i++)
   {
    if (emp[i].number == sNumber) 

     break;

    else if (i == nEmp - 1) 

     printf("*** ERROR: Employee ID not found! ***\n");
   }
   if (i != nEmp) {

    printf("The current salary is %.2f\n", emp[i].salary);
    printf("Enter Employee New Salary: ");
    scanf("%lf", &emp[i].salary);
    check = 0;
    printf("\n");
   }
  } while (check);

  break;
 case 4: //remove employee

  printf("Remove Employee\n");
  printf("===============\n");

  if (nEmp == 0) { //in case there is no employee yet

   printf("\nNo employee to remove\n\n");

   break;
  }
  do
  {
   check = 1;

   printf("Enter Employee ID: ");
   scanf("%d", &sNumber);

   for (i = 0; i < SIZE; i++)
   {
    if (emp[i].number == sNumber) 

     break;

    else if (i == nEmp - 1) 

     printf("*** ERROR: Employee ID not found! ***\n");
   }

   if (i != nEmp) {
    check = 0;

    printf("Employee %d will be removed\n\n", emp[i].number);
    emp[i].number = 0;
    emp[i].age = 0;
    emp[i].salary = 0.0;

    nEmp -= 1;
   }

  } while (check);

  break;

 case 0: //exiting process

  printf("Exiting Employee Data Program. Good Bye!!!\n");

  break;

 default: //not valid option input

  printf("ERROR: Incorrect Option: Try Again\n\n");

  break;
 }
 //if (option != 0) option = -1;
}
return 1;
}

我希望最终输出为:

EMP ID  EMP AGE EMP SALARY
======  ======= ==========
222       22   22222.22
666       66   66666.66
444       44   44444.44
555       55   55555.55

不是(我现在正在得到什么)

EMP ID  EMP AGE EMP SALARY
======  ======= ==========
222       22   22222.22
444       44   44444.44
555       55   55555.55
666       66   66666.66

退出程序之前。

2 个答案:

答案 0 :(得分:1)

您的for循环(for (i = searchedI; i < nEmp; i++) { ... })将所有元素向前移动一个位置。要获得所需的结果,只需将最后一个元素移到要删除的位置即可(以下代码显示了完整的if块):

if (i != nEmp)
{
    check = 0;
    printf("Employee %d will be removed\n\n", emp[i].number);

    --nEmp; // doing this first spares you additional subtractons later...
    if (i != nEmp) // last element does not have to be moved...
        emp[i] = emp[nEmp];
    emp[nEmp].number = 0; // actually redundant
}

答案 1 :(得分:0)

我猜你的问题实际上是这个。最初,您的数组如下所示:

EMP ID  EMP AGE EMP SALARY
======  ======= ========== 
222       22   22222.22 
333       33   33333.33 
444       44   44444.44 
555       55   55555.55

您希望将其更改为以下内容:

EMP ID  EMP AGE EMP SALARY
======  ======= ========== 
222       22   22222.22 
666       66   66666.66 
444       44   44444.44 
555       55   55555.55

在那种情况下,我建议您执行的操作是在用333删除列时,将所有结构成员替换为0(在这种情况下,0表示该列为空)而不是移动这些列。在用于添加新员工的代码中,检查是否有任何员工编号为0。这将为您提供必须添加新员工的新位置。

因此,基本上,对您的“删除员工”代码进行以下更改

if (i != nEmp) {
    check = 0;

    printf("Employee %d will be removed\n\n", emp[i].number);
    emp[i].number = 0;
    emp[i].age = 0;
    emp[i].salary = 0.0;

    nEmp -= 1;
   }

并将此更改更改为您的“添加员工”代码

  if (nEmp < SIZE) {

   int empIndex = 0; //new variable to find the next possible location to store the employee

   while ((emp[empIndex].number != 0) && (empIndex < SIZE)) {       
        empIndex++;
   }

   printf("Enter Employee ID: ");
   scanf("%d", &emp[empIndex].number);
   printf("Enter Employee Age: ");
   scanf("%d", &emp[empIndex].age);
   printf("Enter Employee Salary: ");
   scanf("%lf", &emp[empIndex].salary);
   printf("\n");
   nEmp++;
  }

确保使用empIndex而不是nEmp来存储新员工。请注意,如果删除员工,但不重新添加任何员工,则将为您删除的条目打印零。另外,请注意,这意味着您需要打印出阵列中的所有成员,而不仅仅是前几个,因为您可能在阵列的最后一个位置有一个雇员,但中间没有一个雇员。