Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,673 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,170 people online right now. Registration is fast and FREE... Join Now!




struct sorting problem

 
Reply to this topicStart new topic

struct sorting problem

ynnhoj67
post 17 Jul, 2007 - 04:24 PM
Post #1


New D.I.C Head

*
Joined: 13 Jul, 2007
Posts: 11


My Contributions


i have a struct contains employee and age, im trying to sort it by age. there is a compile problem, can anyone help me?
the code is below:
CODE

#include<stdio.h>
#include<stdlib.h>

#define max 50;

struct employee
{
       char name[20];
       int age;
};
      
main()
{    
     int i,j,n;
     struct employee info[max],temp;   //wonder if the temp declare correct
     printf("\nHow many employee will be enter: ");
     scanf("%d",&n);
     for(i=0;i<n;++i)
     {
     printf("\nPlease enter #%d employee's name: ",i+1);
     gets(info[i].name);
     fflush(stdin);
     printf("\nPlease enter #%d employee's age: ",i+1);
     scanf("%d",&info[i].age);
     fflush(stdin);
     }
     for(i=0;i<n-1;i++);
     for(j=i+1;j<n;j++);
     {
     if(info[i].age>info[j].age)
     {
     temp=info[i].age;             //here the compiler shows an error
     info[i].age=info[j].age;
     info[j].age=temp;
     }
     for(i=0;i<n;i++)
     {
     printf("\n%s %d",info[i].name,info[i].age);
     }
     system("pause");
}


i need a temp as a struct data type to store the temporary value, is that my declaration for temp was wrong?
User is offlineProfile CardPM

Go to the top of the page

Amadeus
post 17 Jul, 2007 - 04:28 PM
Post #2


g++ -o drink whiskey.cpp

Group Icon
Joined: 12 Jul, 2002
Posts: 12,178



Thanked 33 times

Dream Kudos: 25
My Contributions


Please post the error(s) you are receiving.

You have declared temp to be of type employee, while you are trying to assign a value of integer to it.
User is offlineProfile CardPM

Go to the top of the page

ynnhoj67
post 17 Jul, 2007 - 04:56 PM
Post #3


New D.I.C Head

*
Joined: 13 Jul, 2007
Posts: 11


My Contributions


QUOTE(Amadeus @ 17 Jul, 2007 - 05:28 PM) *

Please post the error(s) you are receiving.

You have declared temp to be of type employee, while you are trying to assign a value of integer to it.


the error was: no match for 'operator=' in 'temp = info[i].employee::age'
User is offlineProfile CardPM

Go to the top of the page

dogboi
post 17 Jul, 2007 - 06:43 PM
Post #4


D.I.C Head

Group Icon
Joined: 10 Jul, 2007
Posts: 93



Dream Kudos: 25
My Contributions


QUOTE(ynnhoj67 @ 17 Jul, 2007 - 08:56 PM) *

QUOTE(Amadeus @ 17 Jul, 2007 - 05:28 PM) *

Please post the error(s) you are receiving.

You have declared temp to be of type employee, while you are trying to assign a value of integer to it.


the error was: no match for 'operator=' in 'temp = info[i].employee::age'



The error is, as Amadeus stated correctly, you declare temp as type employee, and then you try to assign an integer value to it (ie, info[i].age)

The specific message the compiler is trying to tell you is different though. That's the compiler telling you it doesn't know how to evaluate "=" in relation to type employee. (Are you using a C++ compiler?)

Anyhow, you can't assign an integer value to an employee variable. That's the error.
User is offlineProfile CardPM

Go to the top of the page

Louisda16th
post 17 Jul, 2007 - 07:01 PM
Post #5


 

Group Icon
Joined: 3 Aug, 2006
Posts: 1,790



Thanked 1 times

Dream Kudos: 755
My Contributions


There's more than just one error actually.
Firstly
CODE

#define max 50;

should be
CODE

#define max 50

No semi-colon is used to terminate a preprocessor directive. Remember the #define directive just replaces max with the number 50. Its not a variable. If you use a semicolon, max would be replaced by 50; so your code declaring the info variable of type employee would become:
CODE

struct employee info[50;]

which would produce a compiler error.
Secondly, as you thought correctly, your declaration of temp is wrong.
CODE

temp=info[i].age;             //here the compiler shows an error
info[i].age=info[j].age;
info[j].age=temp;

Yo have declared temp as type employee. However ince you're usin temp to store info[i].age (and not the entire structure), temp should be an int.
so your code should change to:
CODE

int i,j,n,temp;
struct employee info[max];

Next, you don't put a semicolon after the for loop. In doing so the compiler will assume that a null statement follows the loop. Also (assuming its bubble sort you are using) there are a few corrections to be made in the sorting algorithm you have used.
With all those changes your code should be.
CODE

#include<stdio.h>
#include<stdlib.h>

#define max 50

struct employee
{
       char name[20];
       int age;
};
      
int main()
{    
     int i,j,n,temp;
     struct employee info[max];

     printf("\nHow many employee will be enter: ");
     scanf("%d",&n);

     for(i=0;i<n;++i)
     {
        printf("\nPlease enter #%d employee's name: ",i+1);
        fflush(stdin);
        gets(info[i].name);
        printf("\nPlease enter #%d employee's age: ",i+1);
        scanf("%d",&info[i].age);
     }

     for(i=1; i<n; i++)
     {
        for(j=0; j<n-i;j++)
        {
            if(info[j].age>info[j+1].age)
                {
                    temp=info[j].age;            
                    info[j].age=info[j+1].age;
                    info[j+1].age=temp;
                }
        }
     }

    for(i=0;i<n;i++)
    {
        printf("\n%s %d",info[i].name,info[i].age);
    }
    system("pause");

    return 0;
}


This post has been edited by Louisda16th: 17 Jul, 2007 - 07:02 PM
User is offlineProfile CardPM

Go to the top of the page

ynnhoj67
post 17 Jul, 2007 - 07:16 PM
Post #6


New D.I.C Head

*
Joined: 13 Jul, 2007
Posts: 11


My Contributions


QUOTE(dogboi @ 17 Jul, 2007 - 07:43 PM) *

The error is, as Amadeus stated correctly, you declare temp as type employee, and then you try to assign an integer value to it (ie, info[i].age)

The specific message the compiler is trying to tell you is different though. That's the compiler telling you it doesn't know how to evaluate "=" in relation to type employee. (Are you using a C++ compiler?)

Anyhow, you can't assign an integer value to an employee variable. That's the error.


oh..i c, but i have to sort the struct by age which is integer in the employee struct. i did little bit change this time didn't have errors but the output couldn't be sorted
the code is
CODE

#include<stdio.h>
#include<stdlib.h>

#define max 50;

struct employee
{
       char name[20];
       int age;
};
      
main()
{    
    
    
     struct employee info[max],temp[max];
     int i,j,n;
     printf("\nHow many employee will be enter: ");
     scanf("%d",&n);
     for(i=0;i<n;++i)
     {
     printf("\nPlease enter #%d employee's name: ",i+1);
     gets(info[i].name);
     fflush(stdin);
     printf("\nPlease enter #%d employee's age: ",i+1);
     scanf("%d",&info[i].age);
     fflush(stdin);
     }
     for(i=0;i<n-1;i++);
     for(j=i+1;j<n;j++);
     {
     if(info[i].age>info[j].age)
     {
     temp[i].age=info[i].age;
     info[i].age=info[j].age;
     info[j].age=temp[i].age;
     }
     for(i=0;i<n;i++)
     {
     printf("\n%s %d",info[i].name,info[i].age);
     }
     system("pause");
}

User is offlineProfile CardPM

Go to the top of the page

ynnhoj67
post 17 Jul, 2007 - 07:25 PM
Post #7


New D.I.C Head

*
Joined: 13 Jul, 2007
Posts: 11


My Contributions



thx..Louisda16th. cuz i'm doing my assignmet which is more complicated, i was stuck at this kind of problem then i just made up an similer situation want to figure it out first....it was in a hurry so didn't check that much mistakes. smile.gif
User is offlineProfile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 06:14AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month