0

the lines after printf("enter nation\n"); to printf("enter m or f\n"); not executing like the codeblock can't see it as it prints the two line together with no chance for me to enter anything in between.. this problem always happen when i use struct

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct emp
{

int salary;
int age;
char name[10];
char m:1;
char nation:1;

};
int main()
{
int i,k;
char g,n;
char name2[10];
struct emp x[3];
for(i=0;i<3;i++)

    {
         printf("enter name\n");
         scanf("%s",&name2);
         strcpy(x[i].name,name2);


        printf("enter salary\n");
         scanf("%d",&x[i].salary);
        printf("enter age\n");
         scanf("%d",&x[i].age);



          printf("enter nation\n");

cant see the next 3 lines and jump to enter m or f

          scanf("%c",&n);
          if(n=='e'){x[i].nation==0;}
          else {x[i].nation==1;}

          printf("enter m or f\n");

cant see those lines too

          scanf("%c",&g);
          if(g=='f'){x[i].m=0;}
          else if(g=='m'){&x[i]==1;}



    }

    for(k=0;k<3;k++)
        {
            if(x[i].nation=='e')
            {
                puts(x[i].name);
                printf("%d\n",x[i].salary);
                printf("%d\n",x[i].age);
                if(x[i].m==0)
                printf("female\n");
                else {printf("male\n");}
                printf("egyptian");

                }


        }

return 0;

}

1 Answers1

2

You have to change

scanf("%c",&n);

to

scanf(" %c",&n); // Skip leading whitespaces

Arrays in C are passed by pointer, so for

char name2[10];

You dont have to use reference operator

scanf("%s",&name2);

just simply do

scanf("%s",name2);  // Will pass an arrays address

Also it would be nice to limit input, or buffer overflow may occur and it would lead to undefined behavior

scanf("%9s",name2);
kocica
  • 6,412
  • 2
  • 14
  • 35