【发布时间】:2018-11-24 14:17:44
【问题描述】:
我收到了错误
运行时检查失败 #2 - 变量“IDU”周围的堆栈已损坏。
我已经用谷歌搜索并尝试了所有方法,并且我读到我需要增加数组大小。但是,对于这个特定的任务,我不允许这样做。我也尝试改变我的状况,它似乎适用于一个变量“newsal”,现在显示“IDU”的这个错误。
请帮帮我。
#define _CRT_SECURE_NO_WARNINGS
#define SIZE 4
#include <stdio.h>
// Define Number of Employees "SIZE" to be 2
// Declare Struct Employee
struct employee {
int ID;
int age;
double salary;
};
/* main program */
int main(void) {
int option = 0;
int count = 0, count2 = 0, valid = 0;
int IDU;
double newsal = 0;
struct employee emp[SIZE] = { { 0 } };
// Declare a struct Employee array "emp" with SIZE elements
// and initialize all elements to zero
printf("---=== EMPLOYEE DATA ===---\n\n");
do {
// Print the option list
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: ");
// Capture input to option variable
scanf("%d", &option);
printf("\n");
switch (option) {
case 0: // Exit the program
printf("Exiting Employee Data Program. Good Bye!!!\n");
break;
case 1: // Display Employee Data
// @IN-LAB
printf("EMP ID EMP AGE EMP SALARY\n");
printf("====== ======= ==========\n");
for (count = 0; count < count2; count++) {
if (emp[count].ID > 0) {
printf("%6d%9d%11.2lf", emp[count].ID, emp[count].age, emp[count].salary);
printf("\n");
}
}
printf("\n");
// Use "%6d%9d%11.2lf" formatting in a
// printf statement to display
// employee id, age and salary of
// all employees using a loop construct
// The loop construct will be run for SIZE times
// and will only display Employee data
// where the EmployeeID is > 0
break;
case 2: // Adding Employee
// @IN-LAB
printf("Adding Employee\n");
printf("===============\n");
if (valid == SIZE) {
printf("ERROR!!! Maximum Number of Employees Reached\n");
printf("\n");
break;
}
printf("Enter Employee ID: ");
scanf("%d", &emp[count2].ID);
printf("Enter Employee Age: ");
scanf("%d", &emp[count2].age);
printf("Enter Employee Salary: ");
scanf("%lf", &emp[count2].salary);
valid++;
count2++;
printf("\n");
// Check for limits on the array and add employee
// data accordingly.
break;
case 3:
printf("Update Employee Salary\n");
printf("===============\n");
do {
printf("Enter Employee ID: ");
scanf("%d", &IDU);
for (count2 = 0; count2 <= SIZE; count2++) {
if (IDU == emp[count2].ID) {
printf("The current salary is %.2lf\n", emp[count2].salary);
printf("Enter Employee New Salary: ");
scanf("%lf", &newsal);
emp[count2].salary = newsal;
}
}
} while (IDU == emp[count2].ID);
break;
case 4:
printf("Remove Employee\n");
printf("===============\n");
do {
printf("Enter Employee ID: ");
scanf("%d", &IDU);
for (count2 = 0; count2 <= SIZE; count2++) {
if (IDU == emp[count2].ID) {
printf("Employee %d will be removed\n", emp[count2].ID);
emp[count2].ID = 0;
emp[count2].age = 0;
emp[count2].salary = 0;
printf("\n");
valid--;
}
}
} while (IDU == emp[count2].ID);
break;
default:
printf("ERROR: Incorrect Option: Try Again\n\n");
}
} while (option != 0);
return 0;
}
//PROGRAM OUTPUT IS SHOW BELOW
/*
---=== EMPLOYEE DATA ===---
1. Display Employee Information
2. Add Employee
0. Exit
Please select from the above options: 2
Adding Employee
===============
Enter Employee ID: 111
Enter Employee Age: 34
Enter Employee Salary: 78980.88
1. Display Employee Information
2. Add Employee
0. Exit
Please select from the above options: 2
Adding Employee
===============
Enter Employee ID: 112
Enter Employee Age: 41
Enter Employee Salary: 65000
1. Display Employee Information
2. Add Employee
0. Exit
Please select from the above options: 2
Adding Employee
===============
ERROR!!! Maximum Number of Employees Reached
1. Display Employee Information
2. Add Employee
0. Exit
Please select from the above options: 1
EMP ID EMP AGE EMP SALARY
====== ======= ==========
111 34 78980.88
112 41 65000.00
1. Display Employee Information
2. Add Employee
0. Exit
Please select from the above options: 0
Exiting Employee Data Program. Good Bye!!!
*/
【问题讨论】:
-
请描述您尝试过的“一切”。
-
乍一看,
count2和valid似乎在做同样的事情?为什么不使用一个名为numEmployees的变量? -
count2 <= SIZE应该是count2 < SIZE;您经常循环一次并访问数组之外的项目。, -
C 还是 C++?选一个。看起来像 C。