I cant get my intended result I want my program to display can anyone help me. The program is to print the workers name who are listed in the struct and if you don't enter any of those names I should print worker name doesn't exist. Can someone tell me the code/ syntax to use

Here is what I have

#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 [10];
    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[10]);

    if (Worker_name= worker1,worker2) // this is the error in the program//
    {
        printf(&Worker_name[10]);
    }
    else
    {
        printf ("Worker Name doesn't exist");
    }
}

In C, you cannot directly compare compound structures such as strings the way you can simple values such as ints or doubles; or rather, you can in the case of strings, but you would end up comparing the memory addresses where the strings begin rather than the strings themselves.

What you need to use a function that iterates through the strings and compares them element by element. Not surprisingly, the standard string library includes a function for just this purpose, namely strcmp(). However, there's a catch: strcmp() does not return a boolean result, but instead compares the strings lexicographically. If the strings are equal (i.e., they hold the same characters), then strcmp() returns 0; otherwise, it returns -1 if the first string comes earlier lexicographically than the second, otherwise it returns 1. For example,

strcmp("abc", "abd")

would return -1, while

strcmp("xyz", "mno")

would return 1.

The practical upshot of this is that you need to change the if() statement to read something like this:

if((strcmp(Worker_name, worker1.Worker_name) == 0)
  || (strcmp(Worker_name, worker2.Worker_name) == 0))

As an aside, I would recommend against passing any string directly to printf(), as it can be a security risk in some cases. Instead, use

 printf("%s", Worker_name);

Finally, I would strongly suggest that you remove the reference to <conio.h>; not only aren't you actually using any console functions, the header itself is non-standard, and only a handful of now long outdated compilers ever supported it in the first place. It should never be used in any new code, ever, and if your professor is teaching you about it, you should complain to the dean that the instructor in question is teaching technologies that have been outmoded for over twenty years.

commented: it is printing worker name doesn't exist +0

Edit: Beaten to it by SRL. That'll teach me to type faster :)

Worker_name= worker1,worker2

Worker_name is an array of char. In this situation, it devolves to a char pointer.

worker1,worker2 is basically nonsense in this situation. The , operator simply means that first the thing on the left is evaluated (and comes out as simply worker1), and then that is discarded and the thing on the right is evaluated, which comes out as worker2.

So here is what you wrote, again:

Worker_name= worker2

As you know, the = symbol means "take the value of the thing on the right, and set the thing on the left to the same value". This line of code is trying to set the value of Worker_name. Is that what you're trying to do? I bet it's not. I bet you meant to use ==, which is a test for two variables having the same value. Is that what you meant? Let's write that instead.

Worker_name == worker2

So on the left we have a char*, and on the right we have an object of type workers_struct.

This simply won't work. The compiler has no idea how to compare a char* with a workers_struct object. What a mess this all is.

Perhaps you meant to compare the Worker_name char array inside the struct with Worker_name? I bet you did.

Let's try that instead.

Worker_name == worker2.Worker_name

So now we're comparing the char* Worker_name with the char* worker2.Worker_name. The compiler does know how to compare pointers. What is a pointer? It's an address in memory. So we're now checking to see if these two sets of characters exist in the same place in memory. Is that what you meant to do? I bet it isn't. I bet you meant to actually compare the contents of those memory locations. bet you want to see if the characters are the same in both the arrays. What a mess.

There is a function that compares char arrays. It is named strcmp, and if two char arrays contain the same string, it returns zero. So what you might have really meant was this:
if ( !(strcmp(Worker_name,worker2.Worker_name)))

Finally, this is testing to see if Worker_name is the same string as worker2.Worker_name. The ! is there because strcmp returns zero if they are the same, and you want to your test to say "TRUE" if they are the same.

If you want to test for worker1.Worker_name as well, you're goign to need the || (OR) operator.

this is the message i am constantly getting when i enter

if((strcmp(worker_name, worker1.worker_name) == 0)
  || (strcmp(worker_name, worker2.worker_name) == 0))

or

if ( !(strcmp(Worker_name,worker2.Worker_name)))

sean is not the same as Sean.

commented: Remarkable you noticed that! +15

@moschops i altered the code

Well obviously you've altered the code. We told you a whole lot of things to change.

this the the entire program , try and run it see what u get @moschops

#include <stdio.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 week.

  struct Workers
{
    char worker_name[30];
    int worker_id;
} Workers;

int  main ()




{


    struct Workers worker1; 
    struct Workers worker2;
    strcmp (worker1.worker_name, "sean");
    worker1.worker_id = 1234;
    strcmp (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);

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

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

printf ("Enter you Work Id Number\n");
scanf ("%d", &worker_id);

printf ("Enter Time arrived at work\n");
scanf ("%f", &Time_Arrived);



if (Time_Arrived <= 08.00)
{ 

printf ("You are EARLY and you may take lunch break at 12:30hrs And LEAVE at 16.00hrs\n");

printf ("Worker_Name=:%S\n",worker_name);
printf ("Worker_ID number=:%d\n",worker_id);
printf ("Time_Arrived=:%.2f\n",Time_Arrived);
printf ("Minutes_Early=:%.2f\n",Minutes_Early=8.00-Time_Arrived);

}

else printf ("You're late and you have extra minutes to complete\n\n");

printf ("Worker_Name=:%S\n",worker_name);
printf ("Worker_ID number=:%d\n",worker_id);
printf ("Time_Arrived=:%.2f\n",Time_Arrived);
printf ("Extra_Minutes=:%.2f\n",Extra_Minutes=Time_Arrived-8.00);

printf("You're lunch time start at =:%.2f\n",lunch_time=Extra_Minutes+Time_Arrived+3.30);
printf("You can leave at =:%.2f\n",Departure=lunch_time+5.00);


Minutes_Late = Time_Arrived - 08.00;
Extra_Minutes = Time_Arrived - 08.00;
Minutes_Early = 8.00 - Time_Arrived;
lunch_time = Extra_Minutes + Time_Arrived + 3.30;
Departure = lunch_time + 1.00;
getchar ();
return 0;

}

You never set the value of worker1.worker_name. You did in your original code, but not this new code.

@moschops thanks for ur time ... it worked i guess the problem was with the strcpy and strcmp . i put the strcmp in the struct and in the if statement now i changed it . strcpy is in the struct and strcmp in the if statement

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.