0

I ran this C program in Visual Studio 2010 but it just performed the 1st for loop and then stopped working. Please help me find a solution. I have also tried to place the second for loop code into the 1st for loop. It then shows the output but I want the output to be separately written for all employees.

#include<stdio.h>
 struct Employee
       {
              int hours;
              char Name[25];
              char date[50];
              long wages;
       };

       void main()
       {
              int i;
            
              struct Employee Emp[2];         //Statement   1

              for(i=0;i<2;i++)
              {

              printf("\nEnter details of %d Employee",i+1);

                    printf("\n\tEnter Employee Name : ");
                    scanf("%s",&Emp[i].Name);

                    printf("\n\tEnter Date in (dd/mm/yyyy) : ");
                    scanf("%s",&Emp[i].date);
                    
                    x:

                    printf("\n\tEnter Employee Hours of Working(You are allowed maximum upto 4) : ");
                    scanf("%d",&Emp[i].hours);

                    if(Emp[i].hours <= 4)
                    {
                        Emp[i].wages=Emp[i].hours*100;
                    }
                    else
                    {
                        printf("\nPlease Enter proper hours of working");
                        goto x;
                    }
            
              }
              for(i=0;i<2;i++)
              {
               printf("\nDetails of Employees");
                  printf("\n%s\t%s\t%ld",Emp[i].Name,Emp[i].date,Emp[i].wages);
              }
              

             
              getchar();

       }
  
Daniel Walker
  • 6,380
  • 5
  • 22
  • 45
  • 3
    Please describe the problem better than "stops working". Give the exact input, expected result and actual result. Also, you have a perfectly good debugger there. Why don't you use it to debug the code by stepping through it to see where things go wrong? – kaylum Nov 07 '20 at 10:51
  • 1
    One suggestion: Put the `"\n"` at the end of the `printf`. Because the output is line buffered and will not show until a newline is encountered. Perhaps that's what you are seeing as the problem but can't be sure as your description is unclear. – kaylum Nov 07 '20 at 10:53
  • 1
    I suggest you not to use goto. It is ok in C but most programmers will avoid using it. (you should use another loop and then add conditions in which the loop will gather all the working hours data) – Ken Lee Nov 07 '20 at 10:54

1 Answers1

0

scanf function expects address of a variable but Employee.Name and Employee.data variables are char arrays. That is, they are already address of their first elements. For example, Employee.Name decays to a pointer to the first element. For further information, please visit this link.

so correct forms are:

scanf("%s",Emp[i].Name); // not &Emp[i].Name

and

scanf("%s",Emp[i].date); // not &Emp[i].date

Moreover, scanf reads characters until a blank space comes across. Suppose a user types Name Surname (with a blank space between words) for Employee.Name, then Employee.Name will be Name, not Name Surname. If you take a string including blank space from user, you can use fgets. Lastly, you can look at my answer from this link to have an idea about a similar issue.

I leave the code below. (I changed goto statement. Instead, I used break and continue statements.)

#include<stdio.h>
struct Employee
{
    int hours;
    char Name[25];
    char date[50];
    long wages;
};

void main()
{
    int i;

    struct Employee Emp[2];         //Statement   1

    for(i=0;i<2;i++)
    {

        printf("\nEnter details of %d Employee",i+1);
        printf("\n\tEnter Employee Name : ");
        scanf("%s",Emp[i].Name);
        printf("\n\tEnter Date in (dd/mm/yyyy) : ");
        scanf("%s",Emp[i].date);
        while(1){

            printf("\n\tEnter Employee Hours of Working(You are allowed maximum upto 4) : ");
            scanf("%d",&Emp[i].hours);

            if(Emp[i].hours <= 4)
            {
                Emp[i].wages=Emp[i].hours*100;
                break;
            }
            else
            {
                printf("\nPlease Enter proper hours of working");
                continue;
            }
        }

    }
    for(i=0;i<2;i++)
    {
        printf("\nDetails of Employees");
        printf("\n%s\t%s\t%ld",Emp[i].Name,Emp[i].date,Emp[i].wages);
    }


    getchar();

}
black
  • 799
  • 1
  • 9
  • 23