delete people name from an array in C

0

Hello. I have an algorithm in C to delete a user, but not to interact in the array:

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

#define numeroUSER 100  

char nome[numeroUSER][100] = {"João Carlos","Maria Luisa","Pedro José", "Gerente"};  

int main() {  
excluirUsuario();  
}  

void excluirUsuario() {  
int indice=0;  
printf("informe o indice: \n");  
scanf("%d", &indice);  

for (int i=0; i<numeroUSER; i++) {  
nome[i]100] = nome[i+1][100];  
}  
}  

If I enter index 0, I want you to delete john from the list, since it is the first one and remain the rest. The algorithm does not delete and I end up deleting some of the names, that is, I can position myself in the string, but I can not position myself between the indexes with the names. How do I exclude this. Obs, it's a programming logic.

    
asked by anonymous 01.06.2018 / 02:33

2 answers

2

Your code some errors and confusions.

  • nome[i]100] = nome[i+1][100]; - here was a [ missing so that the syntax is correct, but I suspect that this will be lost when constructing the question here?

    In any case, each string is given only by the first index, thus nome[i] , since 100 already has to do with the letters of each. When doing nome[i][100] it would only copy the letter in position 100 not moving the rest.

    And also could not nome[i] = nome[i+1] because it is an array of chars , so you have to use strcpy , which is the function indicated to copy strings . So:

    strcpy(nome[i], nome[i+1]);
    

    That copies the string in nome[i+1] to nome[i] .

  • for (int i=0; i<numeroUSER; i++) { - If you want to copy the names to the previous positions one by one, you must start with the one you want to delete and not the 0 .

    If you are using the front element in for you can not go to the last one because there is no one at the front, so the end should be numeroUser - 1 .

Correcting these points your for would look like this:

for (int i=indice; i < numeroUSER - 1; i++) {
    strcpy(nome[i], nome[i+1]); //copia o de i+1 para i
}

Now it's important to remember that when you show the names that are left over, you have to show less 1 , which corresponds to the updated size after deletion:

printf("Usuarios restantes: \n");
int tamanho = numeroUSER - 1; //tamanho atualizado devido ao usuario removido
for (int i=0; i< tamanho; i++) {
    printf("%s\n", nome[i]);
}

See this example working on Ideone

Recommendations:

  • You have included the header locale.h but you did not use the setlocale function to set the encoding to be used.
  • Avoid using conio.h because it is specific for windows. In the code shown there is no function depending on this header .
  • Enter your code. This is far more important than you might think, and the code you have in the question has no indentation.
  • Do not set functions down from your calls. Note that the excluirUsuario function comes after main , when it is used in main . You must declare the function before main , or move it fully to before main .
01.06.2018 / 03:44
0

1 Create an auxiliary char variable;

2º You should get the index that enters parameter in the function and go through the array from , to , corresponding; (If there are numerous "," it might be easier to create an int vector to store the positions that occur in the ",")

3º The moment you find the name you can inside the auxiliary;

4º Make a loop of repetition (similar to a bubble sort) and pass the last position from , to first

Or you can use the strtok (or strtoke) function to separate ...

Example:

/* strtok example */
#include <stdio.h>
#include <string.h>

int main ()
{
  char str[] ="- This, a sample string.";
  char * pch;
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str," ,.-");
  while (pch != NULL)
  {
    printf ("%s\n",pch);
    pch = strtok (NULL, " ,.-");
  }
  return 0;
}
    
01.06.2018 / 02:59