简体   繁体   English

为什么程序不打印第二名员工的姓名?

[英]why the program is not printing the 2nd employee's name?

This program tells that which employee has tenure equal to 3 years or more than 3 years.该程序告诉哪个员工的任期等于 3 年或超过 3 年。 So why the program is not printing the 2nd employee's name?那么为什么程序不打印第二名员工的姓名呢?

Code代码

#include <stdio.h>
#include <string.h>
int main()
{
 struct employee
 {
    char name[30]; //name of employee
    int emp_code;  // employee code
    int date[10];  // year of joining
 } e,e3;

 struct employee e1 = {"Mr.ABCDEF", 123, 2002};
 struct employee e2 = {"Mr.UVWXYZ", 456, 2004};

 printf ("Enter current date DD MM YYYY: ");
 scanf ("%d %d %d" , &e3.date, &e3.date ,&e.date);

 if ((e.date - e1.date) >= 3)
     printf("Employee Code: %d Name: %s\n", e1.emp_code , e1.name);

 else if ((e.date - e2.date) >= 3)
     printf("Employee Code: %d Name: %s\n", e2.emp_code , e2.name);

 return 0;  }

Remove 'else' from -'else if' statement.从 -'else if' 语句中删除 'else'。
Ie Both statements should be tested for length before printing.即两个语句都应该在打印前测试长度。

if ((e.date - e1.date) >= 3)
 printf("Employee Code: %d Name: %s\n", e1.emp_code , e1.name);

if ((e.date - e2.date) >= 3)
 printf("Employee Code: %d Name: %s\n", e2.emp_code , e2.name);

Although these statements are both logically and syntactically fine, maintainability would be improved by making your intent explicit by using Braces...尽管这些语句在逻辑和语法上都很好,但通过使用大括号使您的意图明确,可提高可维护性......

 if ((e.date - e1.date) >= 3)
 {
       printf("Employee Code: %d Name: %s\n", e1.emp_code , e1.name);
 }
 if ((e.date - e2.date) >= 3;
 {
        printf("Employee Code: %d Name: %s\n", e2.emp_code , e2.name);
 }

Printing the second user is in an Else if block.打印第二个用户位于 Else if 块中。 So if you print the first one, the else if will be skipped.因此,如果您打印第一个,则将跳过 else if。

Turn the else if into an if and it should be better.把 else if 变成 if 应该会更好。

In scanf ("%d %d %d", &e3.date, &e3.date, &e.date);scanf ("%d %d %d", &e3.date, &e3.date, &e.date); you read three ints into the same memory location.您将三个整数读入同一 memory 位置。 Perhaps you meant scanf("%d %d %d", &e3.date[0], &e3.date[1], &e3.date[2]);也许你的意思是scanf("%d %d %d", &e3.date[0], &e3.date[1], &e3.date[2]); so the year, month and day go in subsequent entries in the date array.所以年月日 go 在日期数组的后续条目中。

Under gcc and clang with default command-line arguments, the compiler warns that this is wrong:在使用默认命令行 arguments 的 gcc 和 clang 下,编译器警告这是错误的:

$ clang d.c
d.c:16:22: warning: format specifies type 'int *' but the argument has type 'int (*)[10]' [-Wformat]
 scanf ("%d %d %d" , &e3.date, &e3.date ,&e.date);
         ~~          ^~~~~~~~
d.c:16:32: warning: format specifies type 'int *' but the argument has type 'int (*)[10]' [-Wformat]
 scanf ("%d %d %d" , &e3.date, &e3.date ,&e.date);
            ~~                 ^~~~~~~~
d.c:16:42: warning: format specifies type 'int *' but the argument has type 'int (*)[10]' [-Wformat]
 scanf ("%d %d %d" , &e3.date, &e3.date ,&e.date);
               ~~                        ^~~~~~~

Because you explicitly to the computer to only print one of them, but using the else command.因为您明确地向计算机仅打印其中一个,而是使用else命令。

There is however a bigger problem with your code you should have gotten multiple warning about but probably ignored:但是,您的代码存在更大的问题,您应该收到多个警告但可能会被忽略:
You are using the date field incorrectly!您错误地使用了date字段!

It is defined as an array of integers, but you are trying to use it as a single integer.它被定义为整数数组,但您试图将其用作单个 integer。

Your conditions will always be true, because what you are really compairing is two memory addresses of two arrays, that are each of size sizeof(int) * 10 .您的条件将始终正确,因为您真正比较的是两个 arrays 的两个 memory 地址,每个地址的大小均为sizeof(int) * 10

You should really remove [10] from the date field for your code to work the way you intended it!您真的应该从日期字段中删除[10]以使您的代码按照您的预期工作!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM