String reading problem

4

I am making an algorithm that gets cases , then a name with surname , a and a size .

It should make a comparison and print them sorted by color in ascending order, in descending order and the names in ascending order .

This part of the comparisons is working well I believe, I think the problem is in reading data that always has some being skipped.

Example of input and output:

3
Maria Jose
branco P
Cezar Torres Mo
branco P
Baka Lhau
vermelho P
0

// A saída ficaria:

branco P Cezar Torres Mo
branco P Maria Jose
vermelho P Baka Lhau

The code I made is this:

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

typedef struct             // Estrutura que contem as variáveis
{
    char nome[100];
    char cor[100];
    char tam[100];
}camisetas;

camisetas pessoa[100];     // Método de acesso a estrutura

int casos, d = 0, A;

int comparaNome(const void * a, const void * b){

    int r;
    //r = strcmp((*(struct camisetas*)a).nome, (*(struct camisetas*)b).nome);
    const camisetas *elemA = a;
    const camisetas *elemB = b;

    if (elemA -> cor == elemB -> cor){
        if(elemA -> tam == elemB -> tam) {
            return strcmp(elemA -> nome, elemB -> nome);
        }
        else return (elemA -> tam[0] - elemB -> tam[0]) * (-1);
    }
    else{
        return elemA -> cor[0] - elemB -> cor[0];
    }
}
int main(void)
{
    int count, i, number;    // Variáveis auxiliares
    //char* array_string [number];

    scanf("%dn", &casos);
    while(casos != 0){       // Comando para encerramento do algoritmo
        fflush(stdin);


        //leitura do numero de casos
        for(i = 0; i < casos; i++)
        {
            scanf("%[^n]", pessoa[i].nome );
            scanf("%s %s", pessoa[i].cor, pessoa[i].tam );
            //fgets(pessoa[i].cor, 15, stdin);
            //fgets(pessoa[i].tam, 10, stdin);
            fflush(stdin);
        }
        qsort(pessoa, 3 , sizeof(camisetas), comparaNome);

        for(i = 0; i < casos; i++){
            printf("%s %s %sn", pessoa[i].cor, pessoa[i].tam, pessoa[i].nome);
        }
        scanf("%dn", &casos);
        fflush(stdin);
    }
}

If someone can tell me the error thank you right away.

    
asked by anonymous 13.06.2015 / 15:56

1 answer

2

The best solution is to always compile with Warnings, your code was full of useless variables. For better sorting results improve your compare function, this mine is also weak, it will only compare the first letters of each field, but the reading is correct. Avoid using fflush and global variables.

Code:

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

typedef struct    
{
   char nome[100];
   char cor[100];
   char tam[100];
} Camisetas;             

int comparaNome(const void *a, const void *b){

   const Camisetas *elemA = a;
   const Camisetas *elemB = b;

   int valor;

   if (elemA->cor[0] > elemB->cor[0]) 
       valor = 1;   
   else if (elemA->cor[0] < elemB->cor[0]) 
       valor = -1;
   else if (elemA->tam[0] < elemB->tam[0])
       valor = 1;
   else if (elemA->tam[0] > elemB->tam[0])
       valor = -1;
   else if (elemA->nome[0] > elemB->nome[0])
       valor = 1;
   else if (elemA->nome[0] < elemB->nome[0])
       valor = -1;
   else
       valor = 0;

   return valor;         
}

void clearStdin(void){  

   int c;

   while(( c = getchar() ) != '\n' && ( c != EOF ));
}

int main(void)
{
  int i, casos;     
  Camisetas pessoa[100];                   

  printf("Digite o número de casos (0 para encerrar): "); 
  scanf(" %d", &casos);
  clearStdin();

  while(casos != 0){ 

    printf("Digite os campos:\n"); 

    for(i = 0; i < casos; i++)
    {
       scanf(" %[^\n]", pessoa[i].nome);

       scanf(" %99s", pessoa[i].cor);

       scanf(" %99s", pessoa[i].tam );     
    }
    qsort(pessoa, casos, sizeof(Camisetas), comparaNome);

    printf("Saída:\n"); 

    for(i = 0; i < casos; i++)
       printf("%s %s %s\n", pessoa[i].cor, pessoa[i].tam, pessoa[i].nome);

       printf("Digite o número de casos (0 para encerrar): ");
       scanf(" %d", &casos);
       clearStdin();    
   }
   return 0;
}

Use the clearStdin function to clear entries, always place a space before the scanf arguments. Try a function that actually sort alphabetically the colors and names completely, as I just did for the first letters and names with the first letters of the same letter will not be sorted. This since his biggest doubt was in reading, enjoy as an exercise and reinforce this sorting with loops to compare colors and names completely.

    
13.06.2015 / 17:09