great! i removed ANY \n and works... tnx a lot
great! i removed ANY \n and works... tnx a lot
when i try to use the program, i first chose option 3 to insert a recipe, then i press 2 to view all recipes, but it says that it has no recipe why???
Code:#include <stdio.h> #include <stdlib.h> void menu(); // struct containing recipe typedef struct { char recipeName[40]; char recipeHowTo[1000]; int howManyPerson; int level; int timeToEat; } recipe; // struct for list struct element { recipe data; struct element *elemPtr; }; // prototypes void menu(int choice, struct element *listPtr); struct element *insertRecipe(struct element *listPtr); void listRecipes(struct element *listPtr); int main() { int choice = 2 ; struct element *list = NULL; int status = 0; while( status != 1 ){ menu(choice,list); } system("PAUSE"); return 0; } void menu(int choice, struct element *listPtr){ printf("----Recipes for fun\n"); printf(".1 Search Recipe\n"); printf(".2 View Recipes\n"); printf(".3 Insert Recipe\n"); printf(".4 Edit Recipe\n"); printf(".5 Delete Recipe\n"); printf(".8 Set measure system\n"); printf(".9 Email Recipes\n"); printf("Insert the number corrispondant to what you need and press ENTER! "); scanf("%d",&choice); if ( choice == 1 ) { } else if ( choice == 2 ) { listRecipes(listPtr); } else if ( choice == 3 ) { listPtr = insertRecipe(listPtr); } else if ( choice == 4 ) { } else if ( choice == 5 ) { } else if ( choice == 8 ) { } else if ( choice == 9 ) { } else { } } // INSERT RECIPE struct element *insertRecipe(struct element *listPtr){ //declare funct that needs a element Pointer as argument //var declairation char recipeName[40] = "default"; char recipeHowTo[1000] = "default"; int howManyPerson = 0; int level = 0; int timeToEat = 0; recipe x; struct element *xPtr; //filling info printf("Insert the Recipe name:"); scanf("%s",recipeName); strcpy(x.recipeName,recipeName); printf("Insert the how to:"); scanf("%s",recipeHowTo); strcpy(x.recipeHowTo,recipeHowTo); printf("Insert for how many person is the recipe "); scanf("%d",&howManyPerson); x.howManyPerson = howManyPerson; printf("Insert the level of the recipe from 1 to 5 "); scanf("%d",&level); x.level = level; printf("Insert the time needed to prepare the recipe "); scanf("%d",&timeToEat); x.timeToEat = timeToEat; if (listPtr != NULL) { // if list is not empty xPtr = (struct element *)malloc(sizeof(struct element)); //allocate mem xPtr->data = x; //put x in data of the pointer xPtr->elemPtr = listPtr; //put xptr on top list } else { listPtr = (struct element *)malloc(sizeof(struct element)); listPtr->data = x; listPtr->elemPtr = NULL; //listPtr points to end list xPtr = listPtr; } return(listPtr); } //LIST RECIPES void listRecipes(struct element *listPtr){ if ( listPtr == NULL ) { printf("No recipes in the database\n"); } else { while ( listPtr != NULL ) { printf("%s",listPtr->data.recipeName); listPtr = listPtr->elemPtr; } } }
Several issues with your code:
1) Your assignment of string literals "default" to your char arrays is an incorrect way to do what you are trying to do. Use strcpy().
2) This :
should be a switch statement for obvious reasons.Code:if ( choice == 1 ) { } else if ( choice == 2 ) { listRecipes(listPtr); } else if ( choice == 3 ) { listPtr = insertRecipe(listPtr); } else if ( choice == 4 ) { } else if ( choice == 5 ) { } else if ( choice == 8 ) { } else if ( choice == 9 ) { } else { } }
3) On a first glance it looks like you are not actually modifying the list when you are inserting elements. If you wish to do so you may want to pass a **struct instead of a *struct (i.e. a pointer containing the address of the address of the head of the list).
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.
i dont get the 3rd point, i'm following an online example can u explain be better plz?
this is the example i'm following Guida C
heeeelp plz
The person writing the code you are looking at is obviously also a beginner, so I suggest to stop using that as a reference for anything.
Inside your insert function it looks to me like you are modifying the pointer passed as parameter so in order to do so successfully you need to pass an address to that pointer (i.e. struct element **head)
Then you change what *head points to by assigning *head = my_new_ptr, or whatever the new element pointer you allocate memory for is called ( I think xptr).
What is happening right now in your code is the same as this:
The same thing is happening to your pointer. You are passing a pointer variable, changing it inside the function, and once you exit the function the pointer still has the old value it had (unallocated garbage).Code:void my_func(int x){ x = 3; } int main(void){ int y = 5; my_func(y); /*THE VALUE OF y is STILL 5 */ return 0; }
You need to brush up on pointers a bit. A pointer is essentialy a variable containing an address. In order to change the contents of a variable within a function you need to provide the address of that variable. So in this case, you need to provide the address of a pointer which is of type **. (basically the address of the memory location where the address of the element in the list is located)
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.
He doesn't necessarily need to use a pointer-to-a-pointer. I would suggest doing the same thing in menu that is done in insertRecipe.
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
@imalc can u better explain what u mean plz?
@claudiu can u show me plz how to put that * ? i tried but always compile error... can u correct the code directly? so i can better understand
ty
If you understand what the first three specific lines (cut out from you code) below do to update 'listPtr' in 'menu', and recognise that the last lines (also cut out from you code) indicate you are not doing the same thing to update 'list' in 'main', then you'll have worked out what you need to do to .Code:else if ( choice == 3 ) { listPtr = insertRecipe(listPtr); } struct element *insertRecipe(struct element *listPtr){ //declare funct that needs a element Pointer as argument return(listPtr);If that hint doesn't help then I can only conclude that you did not write the code to begin with.Code:menu(choice,list); void menu(int choice, struct element *listPtr){
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
i got ur hint, but it doesnt run
Code:#include <stdio.h> #include <stdlib.h> void menu(); // struct containing recipe typedef struct { char recipeName[40]; char recipeHowTo[1000]; int howManyPerson; int level; int timeToEat; } recipe; // struct for list struct element { recipe data; struct element *elemPtr; }; // prototypes struct element *menu(int choice, struct element *list); struct element *insertRecipe(struct element *listPtr); void listRecipes(struct element *listPtr); void saveRecipes(struct element *listPtr); int main() { int choice = 2 ; struct element *list = NULL; int status = 0; while( status != 1 ){ list = menu(choice,list); } system("PAUSE"); return 0; } struct element *menu(int choice, struct element *list){ printf("----Recipes for fun\n"); printf(".1 Search Recipe\n"); printf(".2 View Recipes\n"); printf(".3 Insert Recipe\n"); printf(".4 Edit Recipe\n"); printf(".5 Delete Recipe\n"); printf(".8 Set measure system\n"); printf(".9 Email Recipes\n"); printf("Insert the number corrispondant to what you need and press ENTER! "); scanf("%d",&choice); if ( choice == 1 ) { } else if ( choice == 2 ) { listRecipes(list); } else if ( choice == 3 ) { list = insertRecipe(list); } else if ( choice == 4 ) { } else if ( choice == 5 ) { } else if ( choice == 8 ) { } else if ( choice == 9 ) { saveRecipes(list); } else { } return(list); } // INSERT RECIPE struct element *insertRecipe(struct element *listPtr){ //declare funct that needs a element Pointer as argument //var declairation char recipeName[40]; char recipeHowTo[1000]; int howManyPerson = 0; int level = 0; int timeToEat = 0; recipe x; struct element *xPtr; //filling info printf("Insert the Recipe name:"); scanf("%s",recipeName); strcpy(x.recipeName,recipeName); printf("Insert the how to:"); scanf("%s",recipeHowTo); strcpy(x.recipeHowTo,recipeHowTo); printf("Insert for how many person is the recipe "); scanf("%d",&howManyPerson); x.howManyPerson = howManyPerson; printf("Insert the level of the recipe from 1 to 5 "); scanf("%d",&level); x.level = level; printf("Insert the time needed to prepare the recipe "); scanf("%d",&timeToEat); x.timeToEat = timeToEat; if (listPtr != NULL) { // if list is not empty xPtr = (struct element *)malloc(sizeof(struct element)); //allocate mem xPtr->data = x; //put x in data of the pointer xPtr->elemPtr = listPtr; //put xptr on top list } else { listPtr = (struct element *) malloc(sizeof(struct element)); listPtr->data = x; listPtr->elemPtr = NULL; //listPtr points to end list // xPtr = listPtr; } return(listPtr); } //LIST RECIPES void listRecipes(struct element *listPtr){ if ( listPtr == NULL ) { printf("No recipes in the database\n"); } else { while ( listPtr != NULL ) { printf("%s",listPtr->data.recipeName); listPtr = listPtr->elemPtr; } } } //SAVE TO TXT void saveRecipes(struct element *listPtr){ FILE *stream = fopen("recipes.txt","w"); while(listPtr != NULL) { fprintf(stream,"%s\n%s\n%d%d%d\n\n",listPtr->data.recipeName,listPtr->data.recipeHowTo,listPtr->data.howManyPerson,listPtr->data.level,listPtr->data.timeToEat); listPtr = listPtr->elemPtr; } fflush(stream); fclose(stream); }
I see two prototypes for menu. One of them that should be deleted is:"it doesnt run" doesn't tell us much. Always be specific. Is it because it does not compile (you'd show the compile errors if that were the case), or does it produce the wrong output when executed (you'd show the incorrect vs the expected output).Code:void menu();
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"
ty
i'm using the program, when i insert recipes all is ok, but when i go to see what it is inserted, i saw only the first recipe, no other inserted, why?
Code:#include <stdio.h> #include <stdlib.h> // struct containing recipe typedef struct { char recipeName[40]; char recipeHowTo[1000]; int howManyPerson; int level; int timeToEat; } recipe; // struct for list struct element { recipe data; struct element *elemPtr; }; // prototypes struct element *menu(int choice, struct element *list); struct element *insertRecipe(struct element *listPtr); struct element *editRecipe(struct element *listPtr); void listRecipes(struct element *listPtr); void saveRecipes(struct element *listPtr); struct element *loadRecipes(struct element *listPtr); int main() { int choice = 2 ; struct element *list = NULL; int status = 0; while( status != 1 ){ list = loadRecipes(list); list = menu(choice,list); } system("PAUSE"); return 0; } struct element *menu(int choice, struct element *list){ printf("----Recipes for fun\n"); printf(".1 Search Recipe\n"); printf(".2 View Recipes\n"); printf(".3 Insert Recipe\n"); printf(".4 Edit Recipe\n"); printf(".5 Delete Recipe\n"); printf(".8 Set measure system\n"); printf(".9 Email Recipes\n"); printf("Insert the number corrispondant to what you need and press ENTER! "); scanf("%d",&choice); if ( choice == 1 ) { } else if ( choice == 2 ) { listRecipes(list); } else if ( choice == 3 ) { list = insertRecipe(list); } else if ( choice == 4 ) { list = editRecipe(list); } else if ( choice == 5 ) { } else if ( choice == 8 ) { } else if ( choice == 9 ) { saveRecipes(list); } else { } return(list); } // INSERT RECIPE struct element *insertRecipe(struct element *listPtr){ //declare funct that needs a element Pointer as argument //var declairation char recipeName[40]; char recipeHowTo[1000]; int howManyPerson = 0; int level = 0; int timeToEat = 0; recipe x; struct element *xPtr; //filling info printf("Insert the Recipe name:"); scanf("%s",recipeName); strcpy(x.recipeName,recipeName); printf("Insert the how to:"); scanf("%s",recipeHowTo); strcpy(x.recipeHowTo,recipeHowTo); printf("Insert for how many person is the recipe "); scanf("%d",&howManyPerson); x.howManyPerson = howManyPerson; printf("Insert the level of the recipe from 1 to 5 "); scanf("%d",&level); x.level = level; printf("Insert the time needed to prepare the recipe "); scanf("%d",&timeToEat); x.timeToEat = timeToEat; if (listPtr != NULL) { // if list is not empty xPtr = (struct element *)malloc(sizeof(struct element)); //allocate mem xPtr->data = x; //put x in data of the pointer xPtr->elemPtr = listPtr; //put xptr on top list } else { listPtr = (struct element *) malloc(sizeof(struct element)); listPtr->data = x; listPtr->elemPtr = NULL; //listPtr points to end list xPtr = listPtr; } return(listPtr); } //EDIT RECIPE struct element *editRecipe(struct element *listPtr){ struct element *originalValue = listPtr; int i = 1; int cont = 1; int editValue = 0; if ( listPtr == NULL ) { printf("No recipes in the database\n"); } else { int i = 1; while ( listPtr != NULL ) { printf("%d %s\n",i,listPtr->data.recipeName); i++; listPtr = listPtr->elemPtr; } } listPtr = originalValue; printf("Insert the number of the value to edit: "); scanf("%d",&editValue); for(cont=1;cont<i;cont++){ if (editValue == cont) { printf("Change the Recipe name:"); scanf("%s",listPtr->data.recipeName); printf("Change the how to:"); scanf("%s",listPtr->data.recipeHowTo); printf("Change for how many person is the recipe "); scanf("%d",listPtr->data.howManyPerson); printf("Change the level of the recipe from 1 to 5 "); scanf("%d",listPtr->data.level); printf("Change the time needed to prepare the recipe "); scanf("%d",listPtr->data.timeToEat); } listPtr = listPtr->elemPtr; } return(originalValue); } //LOAD RECIPES struct element *loadRecipes(struct element *listPtr){ FILE *filePtr; recipe x; struct element *xPtr; if(( filePtr = fopen("recipes.txt","rb"))== NULL){ printf("Error in file opening"); } while( fread(&x,sizeof(recipe),1,filePtr) != NULL ){ if(listPtr == NULL){ listPtr = (struct element *) malloc(sizeof (struct element)); strcpy(listPtr->data.recipeName,x.recipeName); strcpy(listPtr->data.recipeHowTo,x.recipeHowTo); //strcpy(listPtr->data.howManyPerson,x.howManyPerson); //strcpy(listPtr->data.level,x.level); //strcpy(listPtr->data.timeToEat,x.t); listPtr->elemPtr = NULL; }else{ xPtr = (struct element *) malloc(sizeof (struct element)); strcpy(xPtr->data.recipeName,x.recipeName); strcpy(xPtr->data.recipeHowTo,x.recipeHowTo); //strcpy(xPtr->data.howManyPerson,x.howManyPerson); //strcpy(xPtr->data.level,x.level); //strcpy(xPtr->data.timeToEat,x.t); xPtr->elemPtr = xPtr; listPtr = xPtr; } }fclose(filePtr); return(listPtr); } //LIST RECIPES void listRecipes(struct element *listPtr){ if ( listPtr == NULL ) { printf("No recipes in the database\n"); } else { while ( listPtr != NULL ) { printf("%s\n",listPtr->data.recipeName); listPtr = listPtr->elemPtr; } } } //SAVE TO TXT void saveRecipes(struct element *listPtr){ FILE *stream = fopen("recipes.txt","w"); while(listPtr != NULL) { fprintf(stream,"%s\n%s\n%d%d%d\n\n",listPtr->data.recipeName,listPtr->data.recipeHowTo,listPtr->data.howManyPerson,listPtr->data.level,listPtr->data.timeToEat); listPtr = listPtr->elemPtr; } fflush(stream); fclose(stream); }
Last edited by xphoenix; 07-05-2010 at 07:44 AM.
it doesnt charge the list at the start