Errors with C array manipulation

Asked

Viewed 203 times

1

Through the following code:

#include<stdio.h>
#include<string.h>
int main(){
//Declaração de Variáveis
char jogo;
char times[20][15] = {"Corinthians","Atlético-MG","Grêmio","Santos","São Paulo","Internacional","Sport","Palmeiras","Ponte Preta","Flamengo",
"Cruzeiro","Atlético-PR","Fluminense","Chapecoense","Figueirense","Havaí","Coritiba","Goias","Joinville","Vasco da Gama"};
int pontos[20] = {70,62,56,50,50,50,49,48,47,44,44,42,40,39,35,34,33,31,30,30};
int menu=0, opcao, realpos=0, i=0;
//Entrada de dados
while ((menu > 0) && (menu<3)) {
  switch (menu) {
    case 1: escreva("+----BRASILEIRAO 2015---+");
      for (i=0;i<20;i++) {
      printf("%d + - %d", i+1, times[i], pontos[i]);
      }
      printf("+----FIM---+");
      menu = 666;
      break;
    case 2:
      printf("Digite o a posição do time que deseja simular");
      scanf("%d", &opcao);

      for (i=1; i<=8;i++){
        realpos = opcao-1;
        printf("Digite o resultado do %dº jogo do %s. (vitoria, empate ou derrota)", i, times[realpos]);
        scanf(%c, &jogo);
        switch (jogo) {
          case "vitoria":
            pontos[realpos] = pontos[realpos] + 3;
            break;
          case "empate":
            pontos[realpos] = pontos[realpos] + 1;
            break;
          case "derrota":
            pontos[realpos] = pontos[realpos];
            break;
          default:
            printf("Digite o resultado do %dº jogo do %s. (vitoria, empate ou derrota)", i, time[realpos]);
            scanf(%d, &jogo);
            break;
        }
      }

      for (i=0;i<20;i++) {
      printf("%d -  %s  -  %d", (i+1), times[i], pontos[i]);
      }
      break;
  }
  printf("Digite 0 para sair");
  scanf("%d", &opcao);
  }

we have the following errors when compiling:

    tabelaBrasileirao.c: In function ‘main’:
tabelaBrasileirao.c:15:7: warning: format ‘%d’ expects argument of type ‘int’, but argument 3 has type ‘char *’ [-Wformat=]
       printf("%d + - %d", i+1, times[i], pontos[i]);
       ^
tabelaBrasileirao.c:15:7: warning: too many arguments for format [-Wformat-extra-args]
tabelaBrasileirao.c:27:9: error: expected expression before ‘%’ token
   scanf(%c, &jogo);
         ^
tabelaBrasileirao.c:29:11: error: case label does not reduce to an integer constant
           case "vitoria":
           ^
tabelaBrasileirao.c:32:11: error: case label does not reduce to an integer constant
           case "empate":
           ^
tabelaBrasileirao.c:35:11: error: case label does not reduce to an integer constant
           case "derrota":
           ^
tabelaBrasileirao.c:39:94: error: ‘time’ undeclared (first use in this function)
             printf("Digite o resultado do %dº jogo do %s. (vitoria, empate ou derrota)", i, time[realpos]);
                                                                                              ^
tabelaBrasileirao.c:39:94: note: each undeclared identifier is reported only once for each function it appears in
tabelaBrasileirao.c:40:10: error: expected expression before ‘%’ token
    scanf(%d, &jogo);
          ^
tabelaBrasileirao.c:52:3: error: expected declaration or statement at end of input
   }
   ^

2 answers

2


If you can’t look at the bugs and find simple problems like that, you won’t be able to program. Missing you dedicate a little to trying to find the bugs, some are typos, and many of them happen because the code is not organized.

Perhaps the only mistake that is due to lack of knowledge is the use of string in a case. You can’t. You can do something more complex, but I wanted to simplify.

I decided to solve only the build errors. Now you can test and see if you are doing what you want. Then, if you have questions, ask a new question with the specific problem, telling the problem in detail and everything you tried.

#include<stdio.h>
#include<string.h>
 
int main() {
    char times[20][15] = {"Corinthians", "Atlético-MG", "Grêmio", "Santos", "São Paulo", "Internacional", "Sport", "Palmeiras", "Ponte Preta", "Flamengo",
    "Cruzeiro", "Atlético-PR", "Fluminense", "Chapecoense", "Figueirense", "Havaí", "Coritiba", "Goias", "Joinville" ,"Vasco da Gama"};
    int pontos[20] = {70, 62, 56, 50, 50, 50, 49, 48, 47, 44, 44, 42, 40, 39, 35, 34, 33, 31, 30, 30};
    int menu = 0;
    //Entrada de dados
    while (menu > 0 && menu < 3) {
        int opcao, realpos = 0;
        switch (menu) {
        case 1: printf("+----BRASILEIRAO 2015---+");
            for (int i = 0; i < 20; i++) printf("%d -  %s - %d", i + 1, times[i], pontos[i]);
            printf("+----FIM---+");
            menu = 666;
            break;
        case 2:
            printf("Digite o a posição do time que deseja simular");
            scanf("%d", &opcao);
            for (int i = 1; i <= 8; i++) {
                realpos = opcao - 1;
                printf("Digite o resultado do %dº jogo do %s. (V, E ou D)", i, times[realpos]);
                char jogo;
                scanf("%c", &jogo);
                switch (jogo) {
                    case 'V':
                        pontos[realpos] += 3;
                        break;
                    case 'E':
                        pontos[realpos]++;
                        break;
                    case 'D':
                        break;
                    default:
                        printf("Digite o resultado do %dº jogo do %s. V, E ou D)", i, times[realpos]);
                        scanf("%c", &jogo);
                        break;
                }
            }
            for (int i = 0; i < 20; i++) printf("%d -  %s  -  %d", (i+1), times[i], pontos[i]);
            break;
        }
        printf("Digite 0 para sair");
        scanf("%d", &menu);
    }
}

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

1

- You are declaring the variable jogo as char and using as string.

- You cannot use switch statement with strings.

- On line 27 should look like this: scanf("%c", &game);

- Cade the function write ?

- On line 39 times this spelled wrong.

Tips:

I advise using the do{}while(); to call the menu at least once and if the prevailing condition true continue calling up the menu.

Divide your code into small pieces to facilitate understanding/maintenance, you can split your code into:

do{}while() > calls the function menu().

User chooses the option on menu() directs to the function resultadoJogo(),

User enters the result, resultadoJogo() directs back to the menu().

Browser other questions tagged

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