我在我的任务中遇到案例3的问题,该案例应该询问用户要更新其薪水的员工的员工ID。 for循环搜索员工ID,然后打印出相应的薪水。
然后打印“输入新薪水”,但是由于某种原因我的scanf被跳过,并且在用户可以输入新薪水之前就消失了。
edit:在格式说明符前添加空格以忽略空白,因为某些提示(谢谢!),现在提示用户输入新薪水,但是当我使用选项1显示表中雇员333s薪水不变时,仍显示以前输入的工资不是用户刚刚输入的新工资
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
// Define Number of Employees "SIZE" to be 2
#define SIZE 4
// Declare Struct Employee
// Empinfo.h
struct Empinfo
{
int IDNo;
int age;
double salary;
};
/* main program */
int main(void)
{
int i;
int count = 0;
int option = 0;
int empIDcheck = 0;
// Declare a struct Employee array "emp" with SIZE elements
// and initialize all elements to zero
struct Empinfo emp[SIZE] = { {0} };
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 (i = 0; i < SIZE; i++)
{
printf("%6d%9d%11.2lf\n", emp[i].IDNo, emp[i].age, emp[i].salary);
}
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");
// Check for limits on the array and add employee
// data accordingly.
if (count < SIZE)
{
printf("Enter Employee ID: ");
scanf("%d", &emp[count].IDNo);
printf("Enter Employee Age: ");
scanf("%d", &emp[count].age);
printf("Enter Employee Salary: ");
scanf("%lf", &emp[count].salary);
printf("\n");
count++;
}
else
{
printf("ERROR!!! Maximum Number of Employees Reached\n");
printf("\n");
}
break;
case 3:
printf("Update Employee Salary\n");
printf("======================\n");
int found = 1;
do {
printf("Enter Employee ID: ");
scanf("%d", &empIDcheck);
for (i = 0; i < SIZE; i++)
{
if (empIDcheck == emp[i].IDNo) {
found = 0;
printf("The current salary is %.2lf\n", emp[i].salary);
}
}
if (found == 0) {
printf("Enter Employee New Salary: ");
scanf("%.2lf", &emp[i].salary);
}
if (found == 1)
printf("*** ERROR Employee ID not found! ***\n");
} while (found == 1);
break;
default:
printf("ERROR: Incorrect Option: Try Again\n\n");
}
} while (option != 0);
return 0;
}
答案 0 :(得分:1)
问题出在寻找empIDcheck
的循环中:
for (i = 0; i < SIZE; i++)
{
if (empIDcheck == emp[i].IDNo) {
found = 0;
printf("The current salary is %.2lf\n", emp[i].salary);
}
}
当您在数组中找到正确的元素时,您不会结束循环,而是继续进行迭代。这意味着循环后的i
的值将等于SIZE
,这超出了数组的范围。
找到正确的元素后,您需要停止循环。这可以通过break
语句完成:
for (i = 0; i < SIZE; i++)
{
if (empIDcheck == emp[i].IDNo) {
found = 0;
printf("The current salary is %.2lf\n", emp[i].salary);
break; // Done with the loop
}
}
或通过更新循环条件以检查found
:
for (i = 0; i < SIZE && found != 0; i++)
{
if (empIDcheck == emp[i].IDNo) {
found = 0;
printf("The current salary is %.2lf\n", emp[i].salary);
}
}
答案 1 :(得分:1)
以下代码有问题,您让for
完成循环
因此i
的值现在与SIZE相同,因此它对于数组而言是超出范围的,
更正它,并在格式说明符前留一个空格,看看它是否有效
do {
printf("Enter Employee ID: ");
scanf("%d", &empIDcheck);
for (i = 0; i < SIZE; i++)
{
if (empIDcheck == emp[i].IDNo) {
found = 0;
printf("The current salary is %.2lf\n", emp[i].salary);
======== <<<<< need the break here>>>>>> ======
}
}
if (found == 0) {
printf("Enter Employee New Salary: ");
scanf("%.2lf", &emp[i].salary); ========> i is going out of bound
and try with having <space> before format like " %.2lf" and see if it helps
}