Thread: Structure problem

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    183

    Structure problem

    I was studying today structure i understand but i was trying to make the code smaller and stuff but i get 2 error

    d:\c_lessons_tutrials_made_by_me\test\1lolz.cpp(26 ) : error C2106: '=' : left operand must be l-value
    d:\c_lessons_tutrials_made_by_me\test\1lolz.cpp(27 ) : error C2106: '=' : left operand must be l-value

    anyway here the code
    Code:
    #include <stdio.h>
    struct employee
    {
    	char name[101];
        char postion[4];
    	char postion2[7];
    	char address[101];
    	int age;
    	float salary;
    };
    /*Now for functions prototype*/
    struct employee read_emp_age();
    struct employee read_emp();
    puttingfunction();
    struct employee read_emp_salary();
    print_emp_asker(struct employee emp);
    /*>>>>>>>>>>>>>>>>>>>*/
    puttingfunction(){
    fputs("Please Enter your num: ",stdout);
    return 0;
    }
    print_emp_asker(struct employee emp)
    {
    int x;
    struct employee emp2;
    emp.postion="Age";
    emp2.postion2="Salary";
    puts("DO you want salary or age or both? for both (3) for salary(2) for age press(1)");
    scanf("%d",&x);
    switch(x)
    {
    case 1:
    emp=read_emp_age();//to get the number by returned by the function read_emp
    printf("%s",emp.postion);
    printf(" is %d:\n",emp.postion);
    break;
    case 2:
    emp=read_emp_salary();//to get the number by returned by the function read_emp
    printf("%s",emp.postion2);
    printf(" is : %f\n",emp2.postion2);
    break;
    case 3:
    emp=read_emp_age();
    fflush(stdin);
    emp=read_emp_salary();
    printf("%s",emp.postion);
    printf(" is : %d...",emp.postion);
    printf("%s",emp.postion2);
    printf(" is : %f\n",emp2.postion2);
    break;
    for(;;){
    default:
    	puts("ERROR");}
    }
    return 0;
    }
    struct employee read_emp_age()
    {
    	struct employee emp;
    	puttingfunction();
    	scanf("%d",&emp.age);
    	return emp;
    }
    struct employee read_emp_salary()
    {
    	struct employee emp;
    puttingfunction();
    scanf("%f",&emp.salary);
    return emp;
    }
    int main(void)
    {
    	struct employee emp;
    	print_emp_asker(emp);
    	return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You still can't assign a value to an array, even if it's inside a struct. To copy a string, you still need to use strcpy.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    oh i thought its possible since it return the hole structure ? so its possible only for it to return only int ?

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    This has nothing to do with returning anything. You cannot assign to an array, no matter of what kind.

    Code:
    int anarray[10];
    char anotherarray[20];
    float morearrays[30];
    
    anarray = 5; /* can't do this */
    anotherarray = "bob"; /* or this */
    morearrays = 1.5f; /* or this */
    There is nothing involving a structure in your problem, other than that your structure contains an array.

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    183
    Quote Originally Posted by tabstop View Post
    This has nothing to do with returning anything. You cannot assign to an array, no matter of what kind.

    Code:
    int anarray[10];
    char anotherarray[20];
    float morearrays[30];
    
    anarray = 5; /* can't do this */
    anotherarray = "bob"; /* or this */
    morearrays = 1.5f; /* or this */
    There is nothing involving a structure in your problem, other than that your structure contains an array.
    thanks alot,i used strcpy to copy the name.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. problem getting structure to work in my function
    By Tom Bombadil in forum C Programming
    Replies: 18
    Last Post: 05-28-2009, 09:53 AM
  2. Problem with structure and class
    By Bargi in forum C++ Programming
    Replies: 3
    Last Post: 09-25-2007, 02:30 AM
  3. Problem with arrays inside a structure
    By babu in forum C Programming
    Replies: 4
    Last Post: 07-12-2007, 09:35 AM
  4. accessing structure pointer problem
    By godhand in forum C Programming
    Replies: 2
    Last Post: 04-09-2004, 10:52 PM
  5. Problem checking for numeric value in a structure
    By ronkane in forum C++ Programming
    Replies: 4
    Last Post: 01-20-2002, 02:53 PM