1

I´ve been doing a project in C about making a team and saving it in a file so it can be read in the next execution. And right now i´m developing a function that should ask the user for a player in the team and eliminate the one that the user has choosen.

For that, i´m using a for loop that prints every player present in the object of the structure and then im asking the user to choose which player to delete.

Then i have a switch case because, depending on the id of the player that was the user's choice the players above should move one position down.

The code is the following:

    int eliminarjogadores(jogador players[NUM], FILE *fplayers, int nj){
  int a=0;
  int i=0;
  printf("Qual o jogador que pretende eliminar?\n");
  for(i=0;i<nj;i++){
    printf("%i - %s\n",i+1, players[i].nome);
  }
  scanf("%i",a);

  switch (a) {
    case 1:
    printf("fds...\n" );
    while(a<nj&&a<NUM){
      strcpy(players[a-1].nome,players[a].nome);
      a++;
    }
    if(a==NUM-1){
      strcpy(players[a-1].nome,"");
    }

    break;
    case 2:
    break;
    case 3:
    break;
    case 4:
    break;
    case 5:
    break;
    case 6:
    break;
    case 7:
    break;
    case 8:
    break;
    case 9:
    break;
    case 10:
    break;
    default:
    printf("Opção inválida!\n");
    break;
  } 

  return nj-1;
}

The code is not finished yet, but if the user choose option one it should have substituted the name of the first player for the name of player two, the second player for the third and so on until there is no players left on the object of the structure and then the last player should have a empty name like "".

But when i try to run the code and select theese options i have this output: Code output

It does not enter the switch case, and i don't know if the problem is on the switch case itself or in the scanf or in other place, can you guys help me?

  • 1
    Read the compiler warnings. If you are not getting warnings then read the compiler manual to find out how to turn up warnings. Also, use a debugger. One big problem should be obvious if you just do some basic debugging such as examining/printing variable values. – kaylum May 28 '21 at 23:01
  • 3
    `scanf("%i",a);` -> `scanf("%i",&a);` – kaylum May 28 '21 at 23:02
  • I already tried to print some variables and the values are just fine, i also tried to print something inside the switch to see if it enters it, but nothing was printed in the console when i did that. – Duochannel YT May 28 '21 at 23:04
  • The 'scanf("%i",&a);' worked for me, thanks, what does the '&' do in that situation? – Duochannel YT May 28 '21 at 23:05
  • 1
    [Why does scanf needs &](https://stackoverflow.com/questions/10597606/why-does-scanf-require) – Yoshikage Kira May 28 '21 at 23:17
  • `&` means 'address of'. Basically you are passing the memory address of your variable `a` to the function `scanf()` so it can modify the content of that memory address, effectively modifying your local variable `a`. – Miguel Sandoval May 29 '21 at 04:51

0 Answers0