Hi;
I have a linked list working but now the problem is to calculate the total of products i have using recursion.
So my problem is here.
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct loja{
char marca[100];
char car[10];
int stock;
float preco;
}INFO;
typedef struct elem
{
INFO nodo;
struct elem *Prox;
}ELEM;
int insElectrodomestico(INFO info, ELEM **initlista)
{
ELEM *novo, *aux;
novo=(ELEM*) malloc(sizeof(ELEM));
if(novo==NULL)
{
printf("Erro na lista");
return -1;
}
novo->nodo=info;//O que se criar novo pertence a estrutura info
novo->Prox=NULL;//O proximo elemente fica NULL
if(*initlista==NULL)
{
*initlista=novo;//Se for NULL inicia a lista
return 0;
}
aux=*initlista; //Inicia a lista atraves do aux
while(aux->Prox!=NULL) //Enquanto que aux->prox seja diferente de NULL
{
aux=aux->Prox; //aux vai receber aux->prox
}
aux->Prox=novo; //Deixa preparado novo, para receber valor de aux->novo
return 0;
}
//My main problem is here
//I read the list, then if the stock is <1, quit
//else, i read the stock, and use recursion
int totEletrodomesticos(INFO info,ELEM *initlista)
{
ELEM *aux;
aux->nodo=info;
aux->Prox=NULL;
if(aux==NULL)
{
printf("Lista vazia...");
exit(1);
}
if(info.stock<1)
{
return -1;
}
else
{
return(info.stock+totEletrodomesticos(info.stock-1));
}
}
int main()
{
ELEM *initlista=NULL; // ---Declarar um aponta para o primeiro elemento da lista
INFO info; //Chama a lista
int op;
do
{
printf("Quer registar: 1- SIM, 0-NAO \n");
scanf("%d", &op);
getchar();
printf("Brand: \n");
gets(info.marca);
printf("Class: \n");
gets(info.car);
printf("Stock: \n");
scanf("%d", &info.stock);
getchar();
printf("Price: \n");
scanf("%f", &info.preco);
getchar();
insElectrodomestico(info,&initlista);
totEletrodomesticos(info, &initlista);
}
while(op!=0);
return 0;
}
I have all this errors caused by recursion function
error: incompatible type for argument 1 of 'totEletrodomesticos'|
note: expected 'INFO' but argument is of type 'int'|
error: too few arguments to function 'totEletrodomesticos'|
D:\2012\c_exe\Pergunta 3 b\main.c|42|note: declared here|
In function 'main':|
warning: passing argument 2 of 'totEletrodomesticos' from incompatible pointer type [enabled by default]|
D:\2012\c_exe\Pergunta 3 b\main.c|42|note: expected 'struct ELEM *' but argument is of type 'struct ELEM **'|
In function 'totEletrodomesticos':|
||=== Build finished: 5 errors, 2 warnings ===|
Honestly...i'm lost