D
deppy_3
Hi.I am started learning Programm language C before some time.I am
trying to make a programm about a very simple "sell shop".This programm
hasn't got any compile problem but when i run it i face some other
ploblems which i can not correct.I would appreciated if someone take a
look at my programm so as to help me.I tried many times to find out
where my mistakes are but i didn't manage something.
I have made some comments due to the programm so as to make it more
easy to as.
Any recommendations about my writing style in the programm are
acceptable.
Please help me!!!
Programm
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/* Functions*/
int menu(void);
struct sells *insert(void);
struct sells *search(void);
int show(struct sells *list);
int exit(void);
/*Struct*/
struct sells{
char *tittle;
char *singer;
int price;
int count;
struct sells *next;
};
struct sells *list;
struct sells *alloc(void)
{
return(struct sells*)malloc(sizeof(struct sells));
}
/*Function main*/
int main(void)
{
menu();
return 0;
}
/*Function menu.In this function the user has the opportunity to choose
what he/she want to do*/
int menu(void)
{
int i;
printf("---------MENU---------\n");
printf("Press \"1\" to insert data\n");
printf("Press \"2\" to search data\n");
printf("Press \"3\" to show data\n");
printf("Press \"4\" to exit from programm\n");
putchar('\n\n');
printf("Make your choice\t");
scanf("%d",&i);
while((i<1)&&(i>4))
{
printf("You made wrong choice.\nPlease make a new
choice\n");
scanf("%d",&i);
}
switch(i)
{
case 1:
{
insert();
break;
}
case 2:
{
search();
break;
}
case 3:
{
show(list);
break;
}
case 4:
{
exit();
break;
}
}
return i;
}
/*In this function the user insert the data.*/
struct sells *insert(void)
{
char titlos[15];
char tragoudisths[15];
int timi;
struct sells *n;
float m_out;
int i;
m_out=0;
printf("%f\n",&m_out);
n=alloc();
printf("Give the title of the CD\n");
scanf("%s",&titlos);
getchar();
printf("Give the name of the singer\n");
scanf("%s",&tragoudisths);
getchar();
printf("Give the price of the CD\n");
scanf("%d",&timi);
getchar();
while((timi<0)||(timi>100))
{
printf("You have insert a wrong price.\nGive a new
price.\n");
scanf("%d",&timi);
getchar();
}
getchar();
n->tittle=strdup(titlos);
n->singer=strdup(tragoudisths);
n->price=timi;
if(list==NULL)
{
list=alloc();
list->tittle=strdup(n->tittle);
list->singer=strdup(n->singer);
list->price=n->price;
list->next=NULL;
printf("%f",&m_out);
}
else if((m_out=(strcmp(titlos,n->tittle)))==0)
{
list->count++;
printf("%f",&m_out);
}
else if((m_out=(strcmp(titlos,n->tittle)))<0)
{
n=alloc();
n->tittle=strdup(titlos);
n->singer=strdup(tragoudisths);
n->price=timi;
n->count=1;
n->next=list;
list=n;
printf("%s\t\t%s\t\t%d\t\t\n",list->tittle,list->singer,list->price);
}
else
{
list->next=insert();
return list;
}
printf("Press \"1\" to return to menu or \"2\" to exit\n");
printf("Make your choice");
scanf("%d",&i);
switch(i)
{
case 1:
{
menu();
break;
}
case 2:
{
exit();
break;
}
}
return n;
}
/*In this function the user search for an element.Also he can modify
it.
If someone can i would like to tell me what i have to do so as to
delete an element.*/
struct sells *search(void)
{
char titlos[15];
int i,k,out;
char choice;
printf("Give the tittle of the CD that you want to find\n");
gets(titlos);
getchar();
if(list==NULL||(out=strcmp(list->tittle,titlos))>0)
{
printf("The tittle that you want doesn't exist.\n");
printf("Press \"1\" to return to menu or \"2\" to exit\n");
printf("Make your choice");
scanf("%d",&i);
switch(i)
{
case 1:
{
menu();
break;
}
case 2:
{
exit();
break;
}
}
return list;
}
else if(out==0)
{
printf("Tittle: \t%s\n",list->tittle);
printf("Singer: \t%s\n",list->singer);
printf("Price: \t%d\n",list->price);
printf("Do you want to make any change;\n");
printf("If Yes press \"1\" else \"0\" to return to
menu\n");
printf("Make your choice");
scanf("%d",&k);
switch(k)
{
case 1:
{
printf("Do you want ot change the tittle;\nPress Y(YES)
or N(NO)\t");
scanf("%s",&choice);
getchar();
if(choice=='Y')
{
printf("Give new tittle:\n");
gets(list->tittle);
getchar();
}
printf("Do you want ot change the name of the
singer;\nPress Y(YES) or N(NO)\t");
scanf("%s",&choice);
getchar();
if(choice=='Y')
{
printf("Give new name:\n");
gets(list->singer);
getchar();
}
printf("Do you want ot change the price of the
CD;\nPress Y(YES) or N(NO)\t");
scanf("%s",&choice);
getchar();
if(choice=='Y')
{
printf("Give new price:\n");
scanf("%d",&list->price);
while((list->price<0)&&(list->price>100))
{
printf("You have insert a wrong price.\nGive a new
price.\n");
scanf("%d",&list->price);
}
putchar('\n');
}
break;
}
case 0:
{
menu();
break;
}
}
}
return list;
}
/*Function which shows to the user all the element that has already
insert.
In this functio there is a problem.When i insert more the 3 elements,
these elements doesn't appear to me.I think that the problem is in my
insert
but i am not quite sure.*/
int show(struct sells *list)
{
int i,k;
k=1;
printf("\t\t-------LIST--------\n");
printf("Num\t\tTittle\t\tSinger\t\tPrice\n");
while(list!=NULL)
{
printf("%d\t%s\t\t%s\t\t%d\t\t\n",k,list->tittle,list->singer,list->price);
k++;
list=list->next;
}
printf("What do you want to do\n");
printf("Press \"1\" to retutn to menu or \"2\" to exit\n");
printf("Make your choice");
scanf("%d",&i);
switch(i)
{
case 1:
{
menu();
break;
}
case 2:
{
exit();
break;
}
}
return 0;
}
/*Exit from the programm*/
int exit(void)
{
int i;
printf("Do you really want to exit from the programm;\n");
printf("If Yes press \"1\" else \"0\"\n");
printf("Make your choice");
scanf("%d",&i);
switch(i)
{
case 1:
{
menu();
break;
}
case 2:
{
break;
}
return 0;
}
}
trying to make a programm about a very simple "sell shop".This programm
hasn't got any compile problem but when i run it i face some other
ploblems which i can not correct.I would appreciated if someone take a
look at my programm so as to help me.I tried many times to find out
where my mistakes are but i didn't manage something.
I have made some comments due to the programm so as to make it more
easy to as.
Any recommendations about my writing style in the programm are
acceptable.
Please help me!!!
Programm
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
/* Functions*/
int menu(void);
struct sells *insert(void);
struct sells *search(void);
int show(struct sells *list);
int exit(void);
/*Struct*/
struct sells{
char *tittle;
char *singer;
int price;
int count;
struct sells *next;
};
struct sells *list;
struct sells *alloc(void)
{
return(struct sells*)malloc(sizeof(struct sells));
}
/*Function main*/
int main(void)
{
menu();
return 0;
}
/*Function menu.In this function the user has the opportunity to choose
what he/she want to do*/
int menu(void)
{
int i;
printf("---------MENU---------\n");
printf("Press \"1\" to insert data\n");
printf("Press \"2\" to search data\n");
printf("Press \"3\" to show data\n");
printf("Press \"4\" to exit from programm\n");
putchar('\n\n');
printf("Make your choice\t");
scanf("%d",&i);
while((i<1)&&(i>4))
{
printf("You made wrong choice.\nPlease make a new
choice\n");
scanf("%d",&i);
}
switch(i)
{
case 1:
{
insert();
break;
}
case 2:
{
search();
break;
}
case 3:
{
show(list);
break;
}
case 4:
{
exit();
break;
}
}
return i;
}
/*In this function the user insert the data.*/
struct sells *insert(void)
{
char titlos[15];
char tragoudisths[15];
int timi;
struct sells *n;
float m_out;
int i;
m_out=0;
printf("%f\n",&m_out);
n=alloc();
printf("Give the title of the CD\n");
scanf("%s",&titlos);
getchar();
printf("Give the name of the singer\n");
scanf("%s",&tragoudisths);
getchar();
printf("Give the price of the CD\n");
scanf("%d",&timi);
getchar();
while((timi<0)||(timi>100))
{
printf("You have insert a wrong price.\nGive a new
price.\n");
scanf("%d",&timi);
getchar();
}
getchar();
n->tittle=strdup(titlos);
n->singer=strdup(tragoudisths);
n->price=timi;
if(list==NULL)
{
list=alloc();
list->tittle=strdup(n->tittle);
list->singer=strdup(n->singer);
list->price=n->price;
list->next=NULL;
printf("%f",&m_out);
}
else if((m_out=(strcmp(titlos,n->tittle)))==0)
{
list->count++;
printf("%f",&m_out);
}
else if((m_out=(strcmp(titlos,n->tittle)))<0)
{
n=alloc();
n->tittle=strdup(titlos);
n->singer=strdup(tragoudisths);
n->price=timi;
n->count=1;
n->next=list;
list=n;
printf("%s\t\t%s\t\t%d\t\t\n",list->tittle,list->singer,list->price);
}
else
{
list->next=insert();
return list;
}
printf("Press \"1\" to return to menu or \"2\" to exit\n");
printf("Make your choice");
scanf("%d",&i);
switch(i)
{
case 1:
{
menu();
break;
}
case 2:
{
exit();
break;
}
}
return n;
}
/*In this function the user search for an element.Also he can modify
it.
If someone can i would like to tell me what i have to do so as to
delete an element.*/
struct sells *search(void)
{
char titlos[15];
int i,k,out;
char choice;
printf("Give the tittle of the CD that you want to find\n");
gets(titlos);
getchar();
if(list==NULL||(out=strcmp(list->tittle,titlos))>0)
{
printf("The tittle that you want doesn't exist.\n");
printf("Press \"1\" to return to menu or \"2\" to exit\n");
printf("Make your choice");
scanf("%d",&i);
switch(i)
{
case 1:
{
menu();
break;
}
case 2:
{
exit();
break;
}
}
return list;
}
else if(out==0)
{
printf("Tittle: \t%s\n",list->tittle);
printf("Singer: \t%s\n",list->singer);
printf("Price: \t%d\n",list->price);
printf("Do you want to make any change;\n");
printf("If Yes press \"1\" else \"0\" to return to
menu\n");
printf("Make your choice");
scanf("%d",&k);
switch(k)
{
case 1:
{
printf("Do you want ot change the tittle;\nPress Y(YES)
or N(NO)\t");
scanf("%s",&choice);
getchar();
if(choice=='Y')
{
printf("Give new tittle:\n");
gets(list->tittle);
getchar();
}
printf("Do you want ot change the name of the
singer;\nPress Y(YES) or N(NO)\t");
scanf("%s",&choice);
getchar();
if(choice=='Y')
{
printf("Give new name:\n");
gets(list->singer);
getchar();
}
printf("Do you want ot change the price of the
CD;\nPress Y(YES) or N(NO)\t");
scanf("%s",&choice);
getchar();
if(choice=='Y')
{
printf("Give new price:\n");
scanf("%d",&list->price);
while((list->price<0)&&(list->price>100))
{
printf("You have insert a wrong price.\nGive a new
price.\n");
scanf("%d",&list->price);
}
putchar('\n');
}
break;
}
case 0:
{
menu();
break;
}
}
}
return list;
}
/*Function which shows to the user all the element that has already
insert.
In this functio there is a problem.When i insert more the 3 elements,
these elements doesn't appear to me.I think that the problem is in my
insert
but i am not quite sure.*/
int show(struct sells *list)
{
int i,k;
k=1;
printf("\t\t-------LIST--------\n");
printf("Num\t\tTittle\t\tSinger\t\tPrice\n");
while(list!=NULL)
{
printf("%d\t%s\t\t%s\t\t%d\t\t\n",k,list->tittle,list->singer,list->price);
k++;
list=list->next;
}
printf("What do you want to do\n");
printf("Press \"1\" to retutn to menu or \"2\" to exit\n");
printf("Make your choice");
scanf("%d",&i);
switch(i)
{
case 1:
{
menu();
break;
}
case 2:
{
exit();
break;
}
}
return 0;
}
/*Exit from the programm*/
int exit(void)
{
int i;
printf("Do you really want to exit from the programm;\n");
printf("If Yes press \"1\" else \"0\"\n");
printf("Make your choice");
scanf("%d",&i);
switch(i)
{
case 1:
{
menu();
break;
}
case 2:
{
break;
}
return 0;
}
}