Listing by name in a struct

Asked

Viewed 272 times

0

Good afternoon. I’m stuck with a job I’m doing. The work consists of: creating a student registration system, where the user registers the name and registration of the student. During the data listing, names should be ordered in alphabetical order. The registration and listing part is ok. just the ordination I’m not getting done.

Here’s the code:

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

struct aluno{
int ra;
char nome[20];
struct aluno *anterior;
struct aluno *ant;
int aux;

   };

 void cadastraaluno();
 void listaaluno();
 struct aluno *al=NULL, *ant=NULL;
 struct aluno aux;

main(){
setlocale(LC_ALL,"portuguese");
int opcao;

do{ // execute o codigo...
system("cls");  
printf("============== SISTEMA CADASTRO DE ALUNOS ==============\n\n");
printf("\n 1- Cadastrar");
printf("\n 2- Listar");
printf("\n 3- Sair\n");
printf("\n Qual opção? ");
scanf("%d",&opcao);

switch(opcao)
    {// inicio switch

        case 1:{

        cadastraaluno();
        break;
        }
        case 2:{
            listaaluno();

            break;
        }

        case 3:{
            printf("Saindo...");
            break;
        }

            default:{
                printf("opção inválida.");
        }

    }//end switch

        getch();
        }while(opcao!=3); // 
        getch();

        }
void cadastraaluno(){

al=(struct aluno*) malloc(sizeof(struct aluno));
printf("RA:");
scanf("%d",&(*al).ra);
fflush(stdin);
printf("NOME:");
gets((*al).nome);
(*al).anterior=ant;
ant=al; 
}


void listaaluno(){
printf("\n Foram cadastrados os seguintes dados: \n\n");

while((*al).anterior !=NULL){ // enquanto registro anterior não for nulo.
    printf("\n RA: %d",(*al).ra);
    printf("\n NOME: %s\n",(*al).nome);
    al=(*al).anterior;  

     aux=*ant;
     *ant=*al;
      *al=*ant;

} // termino while

printf("\n RA: %d",(*al).ra);
printf("\n NOME: %s",(*al).nome);

getch();    

}

The listing works ok. but it needs to be in alphabetical order. someone can give me a light?

  • There is a lib that already does this for you in cpp. It organizes your data. But check out this article: https://answall.com/questions/72494/printar-strings-em-ordem-alfab%C3%A9tica It will definitely help you!

  • I appreciate the help! a lib you say would be the string. h?

1 answer

0

you can try using the command strcmp(string1, string2) to compare two strings and try to sort with a pointer helper

Browser other questions tagged

You are not signed in. Login or sign up in order to post.