用于打印工作人员名称的If语句

If statement to print worker name

本文关键字:If 语句 打印 工作人员 用于      更新时间:2023-10-16

我无法获得预期结果,我希望我的程序能显示出来,有人能帮我吗?该程序将打印结构中列出的工人名称,如果你不输入任何这些名称,我应该打印的工人名称不存在。有人能告诉我使用的代码/语法吗

这是我的

#include <stdio.h>    
#include <conio.h>       
#include <string.h>
//Program Purpose: To accept a specific set of worker names and worker id number and accept the time they came to work and determine if they were early or late for the day.`
struct workers {
    char worker_name[10];
    int worker_id;
}   workers;
int main ()
{
    struct workers worker1; 
    struct workers worker2;
    strcpy (worker1.worker_name, "sean");
    worker1.worker_Id = 1234;
    strcpy (worker2.worker_name,"tajae");
    worker2.worker_Id = 7890;
    char worker_name [30];
    int Worker_Id;
    float Time_Arrived;
    float Minutes_Late;
    float Extra_Minutes;
    float Minutes_Early;
    float lunch_time;
    float Departure;
    printf("******************Produce Pro Time Management System********************nn");
    printf("Good morning. Welcome to Produce Pro, Hope you had a good nights rest and ready to have a successful day at work today.nn");
    printf("Please follow the instruction and answer with the required detailsn");
    printf("Note brief: All time are in army hoursnn");
    printf("Enter your Worker Namen");
    scanf("%S",&worker_name[30]);
    if (worker_name= worker1,worker2) // this is the error in the program//
    {
        printf(&worker_name[30]);
    }
    else
    {
        printf ("Worker Name doesn't exist");
    }
}

当我更改if语句并放入时

if (worker_name == worker1.worker_name || worker_name == worker2.worker_name)
{
printf("Welcome %sn",worker_name);
}
else printf ("Worker Name doesn't existn");

worker不知道我得到的是什么

如果只检查名称,就不需要在If Condition中指定Struct成员吗?请参见下文。

if (Worker_name == worker1.Worker_name || Worker_name == worker2.Worker_name)
{
    printf("Welcome %sn",Worker_name);
}

if (strcmp(Worker_name,worker1.Worker_name) != 0 || strcmp(Worker_name,worker2.Worker_name) != 0)
{
    printf("Welcome %sn",Worker_name);
}

你应该看看这篇文章,它的答案完全相同:

C: 我该如何编写一个搜索函数来在结构数组中查找匹配项,并返回(打印)它匹配的整个结构?

在C中,可以使用strcmp以字典方式比较两个以null结尾的(!)字节字符串:

int err = strcmp(worker1,worker2);
if( err == 0))
    printf("ok,equaln");
else if(err < 0)
    printf("[%s] precedes [%s]n",worker1,worker2);
else if(err > 0)
    printf("[%s] follows [%s]n",worker1,worker2);

我认为您需要这样修改scanf和printf语句:

scanf("%s",&Worker_name); //small letter 's' 

或者可以是

scanf("%s",Worker_name);

printf("%s",Worker_name);

希望这能奏效。。