-2

So I had to make a vending machine. I did it first in Python and it worked like I wanted (here's the code, sorry but somethings are in Portuguese):

def cadastro(matriz_cadastro):
        quant = int(input("Quantidade do produto:"))
        local = input("Código de localização do produto:")
        preco = float(input("Preço do produto:"))
        control = True
        for x in matriz_cadastro:
            if x[1] == local:
                x[0]+= quant
                x[2] = preco
                control = False
        if control:
         matriz_cadastro.append([quant,local,preco])    
def carga_moedas(troco, moeda, quant_moeda):
        if moeda == 5:
            troco[0] += quant_moeda
        if moeda == 10:
            troco[1] += quant_moeda
        if moeda == 25:
            troco[2] += quant_moeda
        if moeda == 50:
            troco[3] += quant_moeda
def deposito(valor_depositado):
        char_add = input()
        while(char_add == '+'):
            valor_depositado += 0.1
            print("Valor depositado: R$", "%.2f"%valor_depositado)
            char_add = input()
            if char_add == 'x':
                print('Cancelando a compra e devolvendo o valor de R$',"%.2f"%valor_depositado)
                valor_depositado = 0
        return valor_depositado
def moedas_troco(troco_venda):
                troco_50 = troco_venda//0.5
                if troco_50 > troco[3]:
                    troco_50 = troco[3]                    
                troco_venda -= troco_50*0.5
                troco_25 = troco_venda//0.25
                if troco_25 > troco[2]:
                    troco_25 = troco[2]
                troco_venda -= troco_25*0.25
                troco_10 = troco_venda//0.1
                if troco_10 > troco[1]:
                    troco_10 = troco[1]
                troco_venda -= troco_10*0.1
                troco_5 = troco_venda//0.05
                if troco_5 > troco[0]:
                    troco_5 = troco[0]
                troco_venda -= troco_5*0.05
                return troco_5, troco_10, troco_25, troco_50, troco_venda                
def compra(valor_depositado, matriz_cadastro,prod, troco):
        for x in matriz_cadastro:
            if x[1] == prod and x[2] <= valor_depositado and x[0]>0:
                troco_venda = valor_depositado-x[2]
                troco_5, troco_10, troco_25, troco_50, troco_venda = moedas_troco(troco_venda)
                if troco_venda >= 0.05:
                    print('Desculpe, não há troco suficiente')
                    print('Cancelando a compra e devolvendo o valor de R$',"%.2f"%valor_depositado)
                    break
                else:
                    x[0]-=1
                    print('Finalizando compra')
                    if troco_50>0 or troco_25>0 or troco_10>0 or troco_5>0:
                            print('Liberando troco')
                    if troco_50>0:
                        if troco_50 == 1:
                                print(troco_50, 'moeda de 50 centavos')
                        else:
                                print(troco_50, 'moedas de 50 centavos')
                        carga_moedas(troco,50,-troco_50)
                    if troco_25>0:
                        if troco_25 == 1:
                                print(troco_25, 'moeda de 25 centavos')
                        else:
                                print(troco_25, 'moedas de 25 centavos')
                        carga_moedas(troco,25,-troco_25)
                    if troco_10>0:
                        if troco_10 == 1:
                                print(troco_10, 'moeda de 10 centavos')
                        else:
                                print(troco_10, 'moedas de 10 centavos')
                        carga_moedas(troco,10,-troco_10)
                    if troco_5>0:
                        if troco_5 == 1:
                                print(troco_5, 'moeda de 5 centavos')
                        else:
                                print(troco_5, 'moedas de 5 centavos')
                        carga_moedas(troco,5,-troco_5)
                    print('Retire seu produto')
                    break
            elif x[1] == prod and x[0] == 0:
                print('Produto indisponível, por favor escolha outro produto')
                prod = input('Código do produto desejado:')
                compra(valor_depositado, matriz_cadastro,prod, troco)
                break
            elif x[1] == prod and x[2] > valor_depositado:
                print('Valor depositado insuficiente, por favor depositar mais.')
                valor_depositado = deposito(valor_depositado)
                compra(valor_depositado, matriz_cadastro,prod, troco)
                break
            elif prod == 'x':
                    print('Cancelando a compra e devolvendo o valor de R$',"%.2f"%valor_depositado)
                    break
valor_depositado = 0
matriz_cadastro = [[10,'00',1.5],[5,'01',3],[1,'02',1.85],[0,'03',1]]
troco = [100,1,1,1]
while True:
    print('Digite:')
    print('[A] para cadastro de produto')
    print('[B] para carga de moedas')
    print('[C] para depósito de dinheiro')
    print('[D] para compra')
    print('[X] para cancelar')
    char=input()
    char = char.upper()
    if char == 'A':
        cadastro(matriz_cadastro)
    elif char == 'B':
        moeda = int(input('Qual moeda terá carga?'))
        quant_moeda = int(input('Quantidade de moedas:'))
        carga_moedas(troco, moeda, quant_moeda)
    elif char == 'C':
        valor_depositado = deposito(valor_depositado)
    elif char == 'D':
        prod = input('Código do produto desejado:')
        compra(valor_depositado, matriz_cadastro,prod, troco)
        valor_depositado = 0
    else:
        print("Cancelando compra")
        if valor_depositado > 0:
                print('Devolvendo o valor de R$',"%.2f"%valor_depositado)

But the thing is that I'm supposed to do it in C, and I know very little about. I tried to pass it to C, and got this code:

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

int i;
int j;
int control;
int quant;
char local[50];
float preco;
#define true 1;
#define false 0;
int matriz_cadastro[4][3] = {{10,00,1.5},{5,01,3},{1,02,1.85},{0,03,1}};
int troco[4]= {100,100,100,100};
int moeda;
int quant_moeda;
float valor_depositado;
char char_add;
float troco_venda;
int troco_5;
int troco_10;
int troco_25;
int troco_50;
char prod[50];
char resp;

int carga_moedas(int troco[4], int moeda,int  quant_moeda){
        if (moeda == 5){
            troco[0] = troco[0] + quant_moeda;
        }
        if (moeda == 10){
            troco[1] =troco[1]+ quant_moeda;
        }
        if (moeda == 25){
            troco[2] =troco[2]+ quant_moeda;
        }
        if (moeda == 50){
            troco[3] = troco[3]+ quant_moeda;
        }
        return 0;
}
float deposito(float valor_depositado){
        char_add = getchar();
        while(char_add == '+'){
            valor_depositado += 0.1;
            printf("Valor depositado: R$%.2f\n",valor_depositado);
            char_add = getchar();
            if (char_add == 'x'){
                printf('Cancelando a compra e devolvendo o valor de         R$%.2f\n',valor_depositado);
                valor_depositado = 0;
            }
        }
        return valor_depositado;
}
float moedas_troco(troco_venda){
                troco_50 = troco_venda/0.5;
                if (troco_50 > troco[3]){
                    troco_50 = troco[3]; 
                }
                troco_venda -= troco_50*0.5;
                troco_25 = troco_venda/0.25;
                if (troco_25 > troco[2]){
                    troco_25 = troco[2];
                }
                troco_venda -= troco_25*0.25;
                troco_10 = troco_venda/0.1;
                if (troco_10 > troco[1]){
                    troco_10 = troco[1];
                }
                troco_venda -= troco_10*0.1;
                troco_5 = troco_venda/0.05;
                if (troco_5 > troco[0]){
                    troco_5 = troco[0];
                }
                troco_venda -= troco_5*0.05;
                return troco_5, troco_10, troco_25, troco_50, troco_venda;
}

int compra(valor_depositado, matriz_cadastro,prod, troco){
    for (i=0;i<sizeof(matriz_cadastro); i++){
        if (matriz_cadastro[i][1] == prod && matriz_cadastro[i][2] <=            valor_depositado && matriz_cadastro[i][0]>0){
            troco_venda = valor_depositado-matriz_cadastro[i][2];
            troco_5, troco_10, troco_25, troco_50, troco_venda = moedas_troco(troco_venda);
            if (troco_venda >= 0.05){
                printf('Desculpe, não há troco suficiente\n');
                printf('Cancelando a compra e devolvendo o valor de R$%f\n',valor_depositado);
                break;
            }
            else{
                matriz_cadastro[i][0]--;
                printf('Finalizando compra\n');
                if (troco_50>0 || troco_25>0 || troco_10>0 || troco_5>0){
                    printf('Liberando troco\n');
                }
                if (troco_50>0){
                    if (troco_50 == 1)
                        printf(troco_50, 'moeda de 50 centavos\n');
                    else
                        printf(troco_50, 'moedas de 50 centavos\n');
                    j = carga_moedas(troco,50,-troco_50);
                }
                if(troco_25>0){
                    if (troco_25 == 1)
                        printf(troco_25, 'moeda de 25 centavos\n');
                    else
                        printf(troco_25, 'moedas de 25 centavos\n');
                    j = carga_moedas(troco,25,-troco_25);
                }
                if (troco_10>0){
                    if (troco_10 == 1)
                                printf(troco_10, 'moeda de 10 centavos\n');                    
                    else
                                printf(troco_10, 'moedas de 10 centavos\n');
                    j = carga_moedas(troco,10,-troco_10);
                }
                if (troco_5>0){
                    if (troco_5 == 1)
                                printf(troco_5, 'moeda de 5 centavos\n');
                    else
                                printf(troco_5, 'moedas de 5 centavos\n');
                    j = carga_moedas(troco,5,-troco_5);
                }
                printf('Retire seu produto\n');
                break;
            }
        }
        else if (matriz_cadastro[i][1] == prod && matriz_cadastro[i][0] == 0){
                printf('Produto indisponível, por favor escolha outro produto\n');
                printf('Código do produto desejado:\n');
                gets(prod);
                compra(valor_depositado, matriz_cadastro,prod, troco);
                break;
        }
        else if (matriz_cadastro[i][1] == prod && matriz_cadastro[i][2] > valor_depositado){
                printf('Valor depositado insuficiente, por favor depositar mais.\n');
                valor_depositado = deposito(valor_depositado);
                compra(valor_depositado, matriz_cadastro,prod, troco);
                break;
        }
        else if (prod == 'x'){
                    printf('Cancelando a compra e devolvendo o valor de R$%.2f\n',valor_depositado);
                    break;
        }
    }
}
int main() {
        valor_depositado = 0;
        while(1>0){
        printf("Digite:\n");
        printf("[A] para cadastro de produto\n");
        printf("[B] para carga de moedas\n");
        printf("[C] para depósito de dinheiro\n");
        printf("[D] para compra\n");
        printf("[X] para cancelar\n"); 
        resp = getchar();
        if (resp == 'A')
            printf('Função indisponível no momento');
        else if(resp == 'B'){
            printf('Qual moeda terá carga?\n');
            gets(moeda);
            printf('Quantidade de moedas:\n');
            gets(quant);
            carga_moedas(troco, moeda, quant_moeda);
        }
        else if (resp == 'C')
            valor_depositado = deposito(valor_depositado);
        else if (resp == 'D'){
            printf('Código do produto desejado:\n');
            gets(prod);
            compra(valor_depositado, matriz_cadastro,prod, troco);
            valor_depositado = 0;
        }
        else{
            printf("Cancelando compra\n");
            if (valor_depositado > 0)
                printf('Devolvendo o valor de R$%.2f\n',valor_depositado);
        }
        }
        return 0;
}`

But it keeps getting the same error(subscripted value is neither array nor pointer nor vector), and it doesn't work. Can somebody help me?

Michael Greene
  • 10,343
  • 1
  • 41
  • 43

1 Answers1

1

There are many things wrong with this conversion from Python to C.

  • Use of a single quote ' instead of a double quote " for strings (e.g. the arguments you are passing to printf)
  • Multiple return values from function moedas_troco. You cannot return multiple values in C, and there is no implicit tuple construction like you could have in Python.
  • C expects type definitions for each of the arguments to any function. It will default to int but this is very bad practice, you should be specifying the types.
  • You are shadowing global variable names with argument names. This is also bad practice and will only confuse you as to which variable you are referencing.
  • The gets function is dangerous and should not be used. Consider using fgets instead.
  • printf expects a string to be passed as the first argument. You are passing integers in multiple places.

Your specific error "subscripted value is neither array nor pointer nor vector" you are seeing because you have not defined the type of your argument matriz_cadastro (that is shadowing the global variable of the same name) and so the C compiler assumes that it is an integer as opposed to the global array.

Michael Greene
  • 10,343
  • 1
  • 41
  • 43