Hi there,
Thank you for the help!!
From the suggestion below I understand that the two variables within the structures that are having problems are the ones that have to do with characters.
Code:
strcpy(emp1.name, myname);
strcpy(emp1.address, myaddress);
emp1.age = myage;
emp1.salary = mysalary;
I see that the other two int within the structure have no problem in assigning them the value with scanf.
I realize see the problem with the order in which I wrote it. The printf's should have been after the value assignement. After the variable declarations, that is.
However I still don't understand why, If I have declared a string of 30 characters
I cannot use a scanf to store the value in a variable and then print it with an printf within the context of a structure:
example 1:
Code:
char name [30];
scanf("%s",name);
printf("the name is %s",name);
example 2:
Code:
struct employee { // here we declare the structure
char name[30];
char address[100];
int age;
float salary;
} emp1, emp2;
printf("Enter the name of the employee: ");
scanf("%s", myname);
printf("Enter the address of the employee: ");
scanf("%s", myaddress);
printf("Enter the age of the employee: ");
scanf("%d", &myage); // this has no problem
printf("Enter the salary of the employee: ");
scanf("%f", &mysalary); // this has no problem
emp1.name = myname; // error: Incompatible types in assignment
emp1.address = myaddress; //error: Incompatible types in assignment
emp1.age = myage;
emp1.salary = mysalary;
In 'example 2' I declared the structure and within it the chars that will take a value. Then I assigned them with scanf.
In example 1' there is no structure, I just declared the chars that will take a value.
Why does the char assignment (with scanf) work in example 1 and it does not work in example 2? I wander if it would work with getchar();??
Evidently the two int that I declared have no problem only the chars.
Why??
Thank you...