Thread: Sorting number(as a string) by ascending order. Help really urgent

  1. #1
    Registered User
    Join Date
    Jun 2014
    Posts
    41

    Sorting number(as a string) by ascending order. Help really urgent

    hi guys. i need help to make my program sort data.
    in this case number that i declared as char(not string, my bada)
    if i have
    name1
    number 2500
    email

    name 2
    number 2400
    email

    i need to put that this way:
    name 2
    number 2400
    email

    name1
    number 2500
    email

    i saw that can be done with qsort but when i try it it doesn't work.

    it's really urgent

    Code:
    typedef struct {
        char nome[MAX_GERAL], email[MAX_GERAL], morada[MAX_GERAL], postal[MAX_GERAL], numero[MAX_GERAL], geral[MAX_GERAL];
        int telefone, FP, SD, AM1, ALGA, CM;
    }dados;


    code to add info i need to sort "numero"
    Code:
    void adicionar(dados* contacto){
    
    
    
    
        if (i<total)
        {
            printf("\nIntroduza o Nome:  ", i + 1);
            scanf(" %[^\n]s", contacto[i].nome);
            printf("Introduza o Numero de Aluno ISEL:  ", i + 1);
            scanf(" %s", &contacto[i].numero);
            printf("Introduza a Morada:  ", i + 1);
            scanf(" %[^\n]s", contacto[i].morada);
            printf("Introduza o Codigo Postal:  ", i + 1);
            scanf(" %[^\n]s", contacto[i].postal);
            printf("Introduza o email:  ", i + 1);
            scanf(" %[^\n]s", contacto[i].email);
            printf("Introduza o telefone:  ", i + 1);
            scanf(" %d", &contacto[i].telefone);
            printf("Nota Final de FP:  ", i + 1);
            scanf(" %d", &contacto[i].FP);
            printf("Nota Final de AM1:  ", i + 1);
            scanf(" %d", &contacto[i].AM1);
            printf("Nota Final de SD:  ", i + 1);
            scanf(" %d", &contacto[i].SD);
            printf("Nota Final de ALGA:  ", i + 1);
            scanf(" %d", &contacto[i].ALGA);
            printf("Nota Final de CM:  ", i + 1);
            scanf(" %d\n\n\n", &contacto[i].CM);
            i++;
        }
        else
        {
            printf("a lista esta cheia");
        }
    
    
    
    
    }
    Last edited by Ruben Marques; 07-02-2014 at 03:36 PM.

  2. #2
    Registered User
    Join Date
    Jun 2014
    Posts
    41
    i have this code to sort is anything wrong with him
    Code:
    
    int compararPorNumero(const void * a, const void * b){
    	
    	struct dados *ia = (struct dados *)a;
    	struct dados *ib = (struct dados *)b;
    	return (ia->numero - ib->numero);
    
    
    	qsort(contacto, total, sizeof(struct dados), compararPorNumero);

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You've been told in a previous thread about claiming your problem is "urgent".
    Quote Originally Posted by Elkvis View Post
    Second, using words like "urgent" in your thread title won't do you any good. Nobody here cares how urgent your problem is. Your urgency does not translate to anyone else. Use descriptive language when titling your thread, so that people looking at the list will get an idea what it's about.
    Manage your time better, so you have plenty of time to get the help you need.

    As for your code snippet, I'm going to say "no, it doesn't work". Why? First, because you failed to provide all the necessary information. I have no idea what contacto is, or total, or struct dados. Thus, I don't know if your compare function is correct. Second, because it's syntactically invalid. Either the qsort is never called because it's after the return statement (and shouldn't be in the compare function in the first place), or it wont even compile because you're missing necessary punctuation. Third, because your title says "sorting number(as a string)", yet you are in no way using strings.

  4. #4
    Registered User
    Join Date
    Jun 2014
    Posts
    41
    sorry... string is for the file part, i mixing things up...
    numero is a char sorry...

    i need to call it of course and make a function to do it but i what i need to know is if the qsort part is correct(in general)

  5. #5
    Registered User
    Join Date
    Jun 2014
    Posts
    41
    Quote Originally Posted by anduril462 View Post

    Manage your time better, so you have plenty of time to get the help you need.
    it's hard... the program was only out a days ago(less than 1 week) and with the exams going on it's hard to do a program this big(if was only by keyboard it would be fine), and i don't have too much experience to do this quick.

  6. #6
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    The first thing I'm going to say is before you can sort things you need to read them correctly. You need to look at every one of your printf() and scanf() functions and make sure you're using them correctly. You have at least one error, and one problem. Be very careful about putting extra characters into your scanf() specifier string because all of those characters must be in the stream being scanned.

    Jim

  7. #7
    Registered User
    Join Date
    Jun 2014
    Posts
    41
    saw one problem
    printf("Introduza o Numero de Aluno ISEL: ", i + 1); scanf(" %s", &contacto[i].numero); <----- remove &

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Use strcmp() to compare strings, not operators like <.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 6
    Last Post: 12-27-2012, 05:32 AM
  2. Sorting dates into ascending order
    By DarkEmpire in forum C Programming
    Replies: 2
    Last Post: 03-06-2012, 11:01 AM
  3. Replies: 9
    Last Post: 04-01-2011, 04:13 PM
  4. Ascending Numerical Order
    By Yizi in forum C Programming
    Replies: 7
    Last Post: 12-08-2009, 07:05 PM
  5. sorting arrays (ascending order)
    By utoots in forum C++ Programming
    Replies: 3
    Last Post: 04-08-2003, 08:57 AM