Vector and strcpy problems in C language

Asked

Viewed 257 times

1

I’m trying to make a vector with 23 names, which should draw 11 salts, 6 sweets and 6 drinks without repetition, the draw should be done in numbers 1 to 3 and when it is drawn should be assigned a value in characters, and at the end should be informed to the user the names and the item that was drawn.

The naming part is ok, but it ends without informing the items

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

int main()
{
    char nomes[23];
    char itens[23];
    int i,x,contador1,contador2,contador3;
    char item[10];
    int volta;

    for(i=0;i<=22;i++)
    {
        printf("Digite o nome: ");
        scanf("%s",&nomes[i]);
    }
    for(i=0;i<=22;i++)
    {
        volta = 1;
        while (volta)
        {
            x = rand() % 3;
            if((x==0) && (contador1<11))
            {
                strcpy(item,"salgado");
                contador1 = contador1 + 1;
                volta = 2;
            }
            else if((x==1) && (contador2<6))
            {
                strcpy(item,"doce");
                contador2 = contador2 + 1;
                volta = 2;
            }
            else if((x==2) && (contador3<6))
            {
                strcpy(item,"bebida");
                contador3 = contador3 + 1;
                volta = 2;
            }
            else
            {
                volta = 1;
            }
        }
        itens[i] = item;
    }
    for(i=0;i<=22;i++)
    {
        printf("Aluno: %s ficou com: %s ", nomes[i], itens[i]);
    }
    getch();
    return(0);
}

1 answer

1


There are several errors there and this code doesn’t even compile, come on:

  • Missing include the header string.h to use the function strcpy().
  • Is declaring a string 22 valid characters and not 23 strings. To do what you want you have to reserve space for each item and for the characters, remembering to reserve a byte for the terminator.
  • Then you have to change the way you take the data with scanf().
  • You’re not initiating the counter variables, so you take initial garbage and anything can happen

It’s no mistake, but some things could be better:

  • The loop of repetition is confused and does not need all this, can have less variables
  • Variable names may be better and you don’t need to declare everything first
  • Comparing if you are less than 23 is more idiomatic and intuitive than doing if you are less than or equal to 22
  • Ideally it would be better to use one algorithm Fisher-Yates, this code is very inefficient and has the potential even to enter into infinite loop, although in practice it will not happen
  • The code can be a little more organized.

That’s how it works:

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

int main() {
    char nomes[23][31];
    char itens[23][8];
    int contadorSalgado = 0, contadorDoce = 0, contadorBebida = 0;
    for (int i = 0; i < 23; i++) {
        printf("Digite o nome: ");
        scanf("%s", nomes[i]);
    }
    for (int i = 0; i < 23; i++) {
        while (1) {
            int sorteado = rand() % 3;
            if (sorteado == 0 && contadorSalgado < 11) {
                strcpy(itens[i], "salgado");
                contadorSalgado++;
                break;
            }
            else if (sorteado == 1 && contadorDoce < 6) {
                strcpy(itens[i], "doce");
                contadorDoce++;
                break;
            }
            else if (sorteado == 2 && contadorBebida < 6) {
                strcpy(itens[i], "bebida");
                contadorBebida++;
                break;
            }
        }
    }
    for (int i = 0; i < 23; i++) printf("Aluno: %s ficou com: %s\n", nomes[i], itens[i]);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Browser other questions tagged

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