c++ - If语句打印 worker 姓名

标签 c++ c

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

这是我的

#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");

我得到的是 worker 不来

最佳答案

如果您只检查名称,是否不需要在 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);
}

关于c++ - If语句打印 worker 姓名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29398550/

相关文章:

c++ - 从文件中填充对象 vector

c++ - 这段C++代码有什么问题?

C:释放二叉搜索树

c - 混合 OpenMP+MPI : I need an explanation from this example

c++ - realloc() 的性能消耗

c++ - 给一个词加一个数字来调用一个变量

c++ - Makefile,C++ 构建错误 : forward declaration

c++ - 创建二维数组类 : How to access element in a 2D array class like array[x][y]

python - 如何在 ../src/x86/ffi64.c :158 处修复 Python 函数 "classify_argument"中的段错误

c - 如何检索 3D 数组的用户输入