【发布时间】:2015-04-01 18:52:21
【问题描述】:
我无法获得我希望我的程序显示的预期结果,谁能帮助我。该程序将打印结构中列出的工人姓名,如果您不输入任何这些名称,我应该打印工人姓名不存在。有人能告诉我要使用的代码/语法吗
这就是我所拥有的
#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");
工人不是我得到的
【问题讨论】:
-
您需要使用字符串比较函数,例如
strcmp或更安全的strncmp,限制为 10,因为这是字符串的最大长度。 -
缩进 4 个空格(或选择您的代码并按 Ctrl-K)以格式化它。不要对代码块使用反引号。
-
等等,不只是
strcmp()你为什么要这样做Worker_name= worker1,worker2??? -
@helpme 。在 scanf 中,您必须使用 Worker_name 而不是 Worker_name[10]);而不是这个语句 if (Worker_name= worker1,worker2) 你必须使用 if ( strcmp( Worker_name, worker1,worker2 ) == 0 )
-
@iharob 我不知道 if 语句所以我用了那个