2015-04-01 102 views
0

我不能得到我的預期結果我希望我的程序顯示任何人都可以幫助我。該程序將打印結構中列出的工人名稱,如果您未輸入任何這些名稱,則應打印工人名稱不存在。誰能告訴我的代碼/語法使用如果聲明打印工人名稱

這裏是我有什麼

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

工人不eixt是什麼我得到

+0

您需要使用字符串比較功能,比如'strcmp'或更安全'strncmp'爲10的極限,因爲這是你的字符串的最大長度。 – pstrjds 2015-04-01 18:56:44

+0

縮進4個空格(或選擇您的代碼並按下Ctrl-K)來格式化它。不要使用反引號代碼塊。 – chi 2015-04-01 18:56:45

+0

等待,它不只是'strcmp()'你爲什麼要這樣'Worker_name = worker1,worker2'? – 2015-04-01 18:57:24

回答

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); 
1

難道你需要在你的。如果條件指定結構成員,如果你正在檢查只有名字?請看下面。

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

。它的工作 – 2015-04-01 19:06:48

+0

你的第二個版本是有缺陷的。它使用逗號運算符,它將丟棄第一個操作數。你只是比較'worker2.Worker_name'。 – 0x499602D2 2015-04-01 19:15:18

+0

@ 0x499602D2謝謝!刪除它... – 2015-04-01 19:17:32

0

我想你需要修改的scanf和printf聲明是這樣的:

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

或可能

scanf("%s",Worker_name); 

printf("%s",Worker_name); 

希望這個作品..