Sign in to follow this  
chch19

Allegro and sort - struct

Recommended Posts

chch19    125
I have little question for you. How to sort some struct value. For example I have struct: struct base { char id[3]; char word[10]; } data[4]; Now I load some value to to data[(0 to 4)].id and some to data[(0 to 4)].word from some file. How to sort this by id - id is number but wrote in char(using atoi or something what's change char to int) that after sort when i type data[2].id - data[2].word - before data[4].id - data[4].word. Bouble sort? how?
#include <allegro.h>
#include <iostream>
#include <stdlib.h>
#include <fstream.h>
#include <string.h>

#define LIST 1
using namespace std;
struct base {
       char id[10];
       char word[10];
} data[10]; 

struct base2 {
       char id2[10];
       char word2[10];
} data2;

int number = 2;

void load()
{
     int i=0, j=0;
     ifstream file("file.txt");
     while(!file.eof())
     {
                       file >> data[i].id;
                       i++;
     }
     file.close();
     ifstream file2("file2.txt");
     while(!file2.eof())
     {
                       file2 >> data[j].word;
                       j++;
     }
     file2.close();
}

char *list1(int index, int *w_listy)
{
   if (index < 0) {
      *w_listy = 5;
      return NULL;
   }
   else
      return data[index].id; 
}
char *list2(int index, int *w_listy)
{
   if (index < 0) {
      *w_listy = 5;
      return NULL;
   }
   else
      return data[index].word; 
}

int sort11();

DIALOG dlg[] =
{
   /* (proc)        (x)  (y)  (w)  (h)  (fg) (bg) (key) (flags) (d1) (d2) (dp)           (dp2) (dp3) */
   { d_clear_proc,  0,   472, 32,  8,   0,   0,   0,    0,      0,   0,   NULL,          NULL, NULL },
   { d_list_proc,   184, 128, 120, 216, 0,   65535,   0,    0,      0,   0,   (void*)list1,  NULL, NULL },
   { d_list_proc,   360, 128, 120, 216, 0,   65535,   0,    0,      0,   0,   (void*)list2,  NULL, NULL },
   { d_button_proc, 264, 408, 144, 32,  0,   65535,   0,    D_EXIT, 0,   0,   (void*)"Exit", NULL, NULL },
   { d_button_proc, 264, 360, 144, 32,  0,   65535,   0,    0, 0,   0,   (void*)data[number].id, NULL, NULL },
   { NULL,          0,   0,   0,   0,   0,   0,   0,    0,      0,   0,   NULL,          NULL, NULL }
};


int myCompareByWord( const void *obj1, const void *obj2 ) {
   base *base1 = (base *) obj1;
   base *base2 = (base *) obj2;
   return strcmp( base1->word, base2->word );
}

int myCompareById( const void *obj1, const void *obj2 ) {
   base *base1 = (base *) obj1;
   base *base2 = (base *) obj2;
   return strcmp( base1->id, base2->id );
}

int main(void)
{
   if (allegro_init() != 0)
      return 1;
   install_keyboard(); 
   install_mouse();
   install_timer();
   set_color_depth(16);
   set_gfx_mode(GFX_AUTODETECT, 640, 480, 0, 0);
   show_mouse(screen);

   load();


// Sort a list of elements by name (10 elements) // 
qsort( data, 10, sizeof( base ), myCompareByWord );

   
   do_dialog(dlg, -1);

   return 0;
}

END_OF_MAIN()
And this file is:
//file.txt
3
4
2
1
5
//file2.txr
three
four
two
one
five
Why program show me nothing after sort?

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this