如果声明打印工人姓名

时间:2015-04-01 18:52:22

标签: c++ c

我无法得到我想要的结果我希望我的程序显示任何人都可以帮助我。该程序将打印结构中列出的工人姓名,如果您不输入任何这些名称,我应该打印工作人员姓名不存在。有人可以告诉我使用的代码/语法

这就是我所拥有的

#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********************\n\n");
    printf("Good morning. Welcome to Produce Pro, Hope you had a good nights rest and ready to have a successful day at work today.\n\n");
    printf("Please follow the instruction and answer with the required details\n");
    printf("Note brief: All time are in army hours\n\n");
    printf("Enter your Worker Name\n");

    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 %s\n",worker_name);
}

else printf ("Worker Name doesn't exist\n");

工人并不是我所得到的

4 个答案:

答案 0 :(得分:1)

如果只检查名称,是否需要在If条件中指定Struct成员?请参阅下文。

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

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

答案 1 :(得分:0)

答案 2 :(得分:0)

在C中,您可以按字典顺序使用strcmpcompare两个以空值终止的(!)字节字符串:

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

答案 3 :(得分:0)

我认为您需要以这种方式修改scanf和printf语句:

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

或可能是

scanf("%s",Worker_name);

printf("%s",Worker_name);

希望这有效..