简体   繁体   中英

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. 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.
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. So if you print the first one, the else if will be skipped.

Turn the else if into an if and it should be better.

In scanf ("%d %d %d", &e3.date, &e3.date, &e.date); you read three ints into the same memory location. Perhaps you meant 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.

Under gcc and clang with default command-line arguments, the compiler warns that this is wrong:

$ 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.

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!

It is defined as an array of integers, but you are trying to use it as a single 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 .

You should really remove [10] from the date field for your code to work the way you intended it!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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