Problem reading strings

Asked

Viewed 196 times

4

I’m making an algorithm that gets the number of affairs, then a name with surname, one color and a size.

He must make a comparison and print them ordered by color in ascending order, size in descending order and the names in ascending order.

That part of the comparisons is working well I believe, I think the problem is in reading the 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 anyone can tell me the mistake I thank you in advance.

1 answer

2


The best solution is to always compile with Warnings, your code was full of useless variables. For best result in Sorting improve its function of comparing, this mine is also weak, will only compare the first letters of each field, but the reading is correct. Avoid the use of 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 function clearStdin to clear entries, always put a space before the arguments of the scanf. Try a function that really sorts colors and names in full alphabetical order, because only the first letters and names with equal first letters will not be sorted. Since your biggest doubt was in reading, take advantage as exercise and reinforce this Sorting with loops to compare colors and names completely.

Browser other questions tagged

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