Good afternoon, I'm kinda new with C, it's one of my subjects in the course I'm enrolled right now, but I've hit a brick wall when it comes to linked lists. We need to code a vending machine, where each product has a code, a name, an amount, a normal price and a discounted price for students. My problem is mainly when adding products. Now, this is probably a very stupid mistake but I can't figure it out... the problem in the code below is that it tells me dimension, name, amount, normalprice and studentprice are not declared in the read_list function. I'm using a code from an article from another website as a basis, and when I compare both code, I can't seem to find out why my code has compiling errors, while the other doesn't. I'm also not sure if doing it like this is gonna add the values to the list, which could also be a problem.
Code:
#include<stdio.h>#include<stdlib.h>
#include<stdbool.h>
struct test_struct
{
int code;
int dimension;
char name[20];
int amount;
float normalprice;
float studentprice;
struct test_struct *next;
};
struct test_struct *head = NULL;
struct test_struct *curr = NULL;
struct test_struct* read_list(void)
{
int i;
printf("Insert the number of characters in the product's name.\n");
scanf("%d",&dimension);
printf("Insert the product's name.\n");
for (i=0;i<dimension;++i)
{scanf(" %c",&name[i]);}
printf("Insert the amount of the product in stock.\n");
scanf("%d",&amount);
printf("Insert the normal price of the product.\n");
scanf("%d",&amount);
printf("Insert the normal price of the product.\n");
scanf("%f",&normalprice);
printf("Insert the students' discount price of the product.\n");
scanf("%f",&studentprice);
}
struct test_struct* create_list(int code)
{
int i;
printf("\n creating list with headnode as [%d]\n",code);
read_list();
struct test_struct *ptr = (struct test_struct*)malloc(sizeof(struct test_struct));
if(NULL == ptr)
{printf("\n Node creation failed \n");
return NULL;}
ptr->next = NULL;
head = curr = ptr;
return ptr;
}
struct test_struct* add_to_list(int code,bool add_to_end)
{
int i;
if(NULL == head)
{return (create_list(code));}
if(add_to_end)
printf("\n Adding product to end of list with value [%d]\n",code);
else
printf("\n Adding node to beginning of list with value [%d]\n",code);
struct test_struct *ptr = (struct test_struct*)malloc(sizeof(struct test_struct));
if(NULL == ptr)
{
printf("\n Node creation failed \n");
return NULL;
}
ptr->code = code;
ptr->next = NULL;
if(add_to_end)
{curr->next = ptr;
curr = ptr;}
else
{ptr->next = head;
head = ptr;}
return ptr;
}
void print_list(void)
{
int i;
struct test_struct *ptr = head;
printf("\n -------Printing list Start------- \n");
while(ptr != NULL)
{printf("| Product code - %d | Product - ",ptr->code);
for (i=0;i< ptr->dimension;++i)
{printf("%c",ptr->name[i]);}
printf(" | Normal price - %.2f | Student price - %.2f | Instances of the product - %d |\n",ptr->normalprice,ptr->studentprice,ptr->amount);
ptr = ptr->next;}
printf("\n -------Printing list End------- \n");
return;
}
int main(void)
{
int i=0,ret=0;
struct test_struct *ptr = NULL;
print_list();
for(i=1;i<3;i++)
add_to_list(i,true);
print_list();
return 0;
}
Thanks in advance.
EDIT: Changed the language of the printfs in print_list, forgot to change it.