Problems with Two-Dimensional String Matrix

Asked

Viewed 185 times

-2

My objective in this exercise is that in each loop to make a new snack order, the informed data be added in the final table (matrix[i][j]). In the second "for" is where I try to assign the variables [int Cod] [char lanche] [int preco] into the matrix. I believe that there is an incompatibility of variables, but even replacing the variables with "text" still goes wrong. In short, I need help to identify the error and if possible, indicate me an alternative to solve the incompatibility between the matrix and the variables I want to put in it. Thanks already to everyone. (my first question here at the Stack/).

int main(int argc, char *argv[]) {
setlocale(LC_ALL, "Portuguese");

int cod, preco, total, quant, cont, i, j;
char lanche[100], matriz[i][j], novo_pedido;

total=0;
cont=1;
for(i=0; i<cont; i++){          
            puts("+--------+-------------------+----------------+");
            puts("| Código |      Lanche       |   Preço Unit.  |");
            puts("+--------+-------------------+----------------+");
            puts("|  100   |  Cachorro Quente  |    R$ 5,00     |");
            puts("|  101   |  Bauru Simples    |    R$ 6,00     |");
            puts("|  102   |  Bauru com Ovo    |    R$ 8,00     |");
            puts("|  103   |  Hamburguer       |    R$ 5,00     |");
            puts("|  104   |  Cheeseburger     |    R$ 7,00     |");
            puts("|  105   |  Refrigerante     |    R$ 2,00     |");
            puts("+--------+-------------------+----------------+");

            puts("\n\n+---------------Faça seu Pedido---------------+");

            cod=1;

            do{

            printf("Código do Lanche: ");
            scanf("%d", &cod);

                if(cod==100){
                    total=(total+5);
                    strcpy(lanche,"Cachorro Quente  ");
                }else if(cod==101){
                    total=(total+6);
                    strcpy(lanche,"Bauru Simples    ");
                }else if(cod==102){
                    total=(total+8);
                    strcpy(lanche,"Bauru com Ovo    ");
                }else if(cod==103){
                    total=(total+5);
                    strcpy(lanche,"Hamburguer       ");
                }else if(cod==104){
                    total=(total+7);
                    strcpy(lanche,"Cheeseburger     ");
                }else if(cod==105){
                    total=(total+2);
                    strcpy(lanche,"Refrigerante     ");
                }else{
                    puts("\n**Código Inválido!**\n");
                }
            }while ((cod<100)||(cod>105));

            printf("Quantidade: ");
            scanf("%d", &quant); getchar();

            puts("+---------------------------------------------+");

    for(j=0; j<3; j++){
        if(j==0){
            matriz[i][j]="cod";
        }
        if(j==1){
            matriz[i][j]="lanche";
        }
        if(j==2){
            matriz[i][j]="preco";
        }
    }       

            printf("Deseja realizar outro pedido? ");
            fflush(stdin);
            novo_pedido=getchar();

                if(novo_pedido=='s'){
                    cont=cont+1;
                    system("cls");
                }

            puts("+---------------------------------------------+");
            puts("");

}

        puts("\n\n+-------------Resultado do Pedido-------------+");
        puts("| Código |      Lanche       |  Quantidade"); 

for (i=0; i<cont; i++){
    for (j=0; j<3; j++){        
        //printf("|   %d  |  %s|     %d\n", cod, lanche, quant);
        printf("%s", matriz[i][j]);
    }
}

        puts("+---------------------------------------------+");
        printf("|                     TOTAL  |   R$ %d,00", (total*quant));
        puts("\n+---------------------------------------------+\n");

return 0;
}
  • How you have such a big code if at first you have an error? matriz[i][j] cannot define a matrix with variables.

  • Need to indicate if you want to do with static memory matriz[100][100] or dynamic memory **matriz then using memory allocation

  • I was just reusing a code from an exercise to test the use of matrices and vectors, but in my course I didn’t even get to this subject yet so I’m studying on my own. Thank you so much for the help, if you can fix it I put the result.

  • I can’t help because I still don’t understand much of what you want. Have you learned about dynamic memory? Can I store the quantity and code in a vector and the product in a matrix? Putting everything in a matrix would be more complicated

  • i managed, using only vectors and pointers, another haha implemento matrices hr.

1 answer

0

So I solved my own exercise. I did not do exactly what I asked in the initial question, n I used two-dimensional matrices, only string and pointer vectors returning a very satisfactory result for me. If you still want to contribute I’ll be glad.

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

        int main(int argc, char *argv[]) {
        setlocale(LC_ALL, "Portuguese");


        int cod, codi[50], preco, pre[50], total, quant, quantidade[50], cont, i;
        char *lanche[10][100], novo_pedido;

        //do{
        total=0;
        cont=1;
            for(i=0; i<cont; i++){          
                        puts("+--------+-------------------+----------------+");
                        puts("| Código |      Lanche       |   Preço Unit.  |");
                        puts("+--------+-------------------+----------------+");
                        puts("|  100   |  Cachorro Quente  |    R$ 5,00     |");
                        puts("|  101   |  Bauru Simples    |    R$ 6,00     |");
                        puts("|  102   |  Bauru com Ovo    |    R$ 8,00     |");
                        puts("|  103   |  Hamburguer       |    R$ 5,00     |");
                        puts("|  104   |  Cheeseburger     |    R$ 7,00     |");
                        puts("|  105   |  Refrigerante     |    R$ 2,00     |");
                        puts("+--------+-------------------+----------------+");

                        puts("\n\n+---------------Faça seu Pedido---------------+");
                        //puts("+---------------------------------------------+");


                        do{

                        printf("Código do Lanche: ");
                        scanf("%d", &cod);

                            if(cod==100){
                                *lanche[i]="Cachorro Quente  ";
                                codi[i]=cod;
                                pre[i]=5;
                            }
                            else if(cod==101){
                                *lanche[i]="Bauru Simples    ";
                                codi[i]=cod;
                                pre[i]=6;
                            }
                            else if(cod==102){
                                total=(total+8);
                                *lanche[i]="Bauru com Ovo    ";
                                codi[i]=cod;
                                pre[i]=8;
                            }
                            else if(cod==103){
                                total=(total+5);
                                *lanche[i]="Hamburguer       ";
                                codi[i]=cod;
                                pre[i]=5;
                            }
                            else if(cod==104){
                                total=(total+7);
                                *lanche[i]="Cheeseburger     ";
                                codi[i]=cod;
                                pre[i]=7;
                            }
                            else if(cod==105){
                                total=(total+2);
                                *lanche[i]="Refrigerante     ";
                                codi[i]=cod;
                                pre[i]=2;
                            }
                            else{
                                puts("\n**Código Inválido!**\n");
                            }
                        }while ((cod<100)||(cod>105));

                        printf("Quantidade: ");
                        scanf("%d", &quant); getchar();

                        quantidade[i]=quant;
                        total=total+(pre[i]*quant);

                        puts("+---------------------------------------------+");



                        printf("Deseja realizar outro pedido? ");
                        fflush(stdin);
                        novo_pedido=getchar();

                            if(novo_pedido=='s'){
                                cont=cont+1;
                                system("cls");
                            }

                        puts("+---------------------------------------------+");
                        puts("");
            }

                        //}while((novo_pedido=='s')||(novo_pedido=='S'));


                    puts("\n\n+-------------Resultado do Pedido-------------+");
                    puts("| Código |      Lanche       |  Quantidade"); 

            for (i=0; i<cont; i++){     
                    printf("|   %d  |  %s|     %d\n", codi[i], *lanche[i], quantidade[i]);
            }


                    puts("\n+---------------------------------------------+");
                    printf("|                     TOTAL  |   R$ %d,00", total);
                    puts("\n+---------------------------------------------+\n");


        return 0;
        }

Browser other questions tagged

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