Errors with array manipulation in C

1

Using 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
   }
   ^
    
asked by anonymous 10.11.2015 / 11:31

2 answers

2

If you can not look at the bugs and find simple problems like this, you will not be able to program. You are missing out on a bit of trouble finding errors, some typos, and many of them happen because the code is not organized.

Perhaps the only error that is due to lack of knowledge is the use of string in a case . Can not. You can do something more complex, but I preferred to simplify.

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

#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: printf("+----BRASILEIRAO 2015---+");
            for (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 (i = 1; i <= 8; i++){
                realpos = opcao - 1;
                printf("Digite o resultado do %dº jogo do %s. (V, E ou D)", i, times[realpos]);
                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 (i = 0; i < 20; i++) {
                printf("%d -  %s  -  %d", (i+1), times[i], pontos[i]);
            }
            break;
        }
        printf("Digite 0 para sair");
        scanf("%d", &opcao);
    }
    return 0;
}

See compiling on ideone .

    
10.11.2015 / 11:54
1

1st - You are declaring the variable jogo as char and using it as a string.

2nd - You can not use switch statement with strings.

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

4th - Where does the function write?

5 ° - In line 39% with% is spelled wrong.

Tips:

I recommend using times to call the menu at least once and if the condition prevails true continue calling the menu.

Divide your code into small pieces to make it easier to understand / maintain, you can split your code into:

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

User chooses the option in menu() directs to function menu() ,

User types the result, resultadoJogo() directs back to resultadoJogo() .

    
10.11.2015 / 12:09