0

I'm looking to sort a recipe list. The recipe list is an array that contains recipe names as well as their ingredients, recipe names begin with 0 and ingredients begin with 1. I want to print out the available recipes in a list, but I'm getting a segmentation fault, is this a correct way to sort the list? Additionally, is it possible to print the item in the list without the 0 in front of it? Here is the code for the printing:

#include <stdio.h>
#include "rawRecipes.h"
#include <string.h> 

void listRecipes(void){
int n = sizeof(rawRecipes); 
int i,j;
//temp place for putting string
char temp[30];   
  //iterate through recipe list array
  for (i=0; i<sizeof(rawRecipes); i++){
    //copies each item in recipe list array to temp
    strcpy(rawRecipes[i], temp);
    for (j=0; j<30; j++){
      if (temp[j] == 0){
        break; 
        printf("%s", rawRecipes[i]); 
      }
    } 
  }
}

int main(void) {
  listRecipes(); 
  return 0;
}

and here is the code for the list used in the header file:

char *rawRecipes[]={"0Broccoli Coleslaw","1olive oil","1white vinegar","1broccoli","0Creamy Broccoli Salad","1broccoli","1white sugar","1red onion","1white wine vinegar","0Minnesota Broccoli Salad","1eggs","1broccoli","1red onion",""};

Expected output would look something like: Broccoli Coleslaw \n Creamy Broccoli Salad \n Minnesota Broccoli Salad

edit I've changed the code and it seems to be printing out the correct items, but multiple times, how do I alter the code so it only prints once?

#include <stdio.h>
#include "rawRecipes.h"
#include <string.h> 

void listRecipes(void){
int n = sizeof(rawRecipes); 
int i,j;
//temp place for putting string
char temp[30];   
  //iterate through recipe list array
  for (i=0; i<14; i++){
    //copies each item in recipe list array to temp
    strcpy(temp, rawRecipes[i]);
    for (j=0; j<30; j++){
      if (temp[j] == '1'){
        break; 
      }
      printf("%s\n", rawRecipes[i]); 
    } 
    //printf("%s", rawRecipes[i]); 
  }
}

int main(void) {
  listRecipes(); 
  return 0;
}
10
  • what's rawRecipes? The arguments in strcpy are backwards, the destination comes first, source is second. Commented Apr 7, 2020 at 18:41
  • 1
    sizeof(rawRecipes) returns the size of the array in bytes, not the number of elements. Commented Apr 7, 2020 at 18:41
  • rawRecipes is the header file and recipe list Commented Apr 7, 2020 at 18:43
  • 1
    Why do you think you need to copy into temp in the first place? Just use rawRecipes[i][0] Commented Apr 7, 2020 at 18:44
  • 1
    You can print without the first character with printf("%s", &rawRecipes[i][1]) Commented Apr 7, 2020 at 18:47

1 Answer 1

1
  1. You have the arguments to strcpy() in the wrong order. But there's no need to make a copy, you can just use rawRecipes[i] directly.
  2. sizeof(rawRecipes) is the number of bytes in the array, not the number of elements. You need to divide by the size of each element to get the number of elements.
  3. There's no need for the inner loop. If you want to check the first character, just use [0].
  4. You need to compare with a the character '0', not the number 0'.
  5. You can skip the 0 by printing starting at the address of rawRecipes[i][1].
void listRecipes(void){
    int n = sizeof(rawRecipes)/sizeof(rawRecipes[0]); 
    int recipe_num = 0;
    for (int i= 0; i<n; i++){
        if (rawRecipes[i][0] == '0') {
            printf("Recipe #%d: %s\n", ++recipe_num, &rawRecipes[i][1]); 
        } 
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Sorry I just had one more question, if I wanted to print "Recipe #%d" in front of the recipe each time, how would I implement a loop for this? I've tried using a for loop (int j=0; j<=3; j++) but I can't seem to find the correct placement for it. So it should print Recipe #1 Broccoli Coleslaw \n Recipe #2 Creamy Broccoli Salad and so on
You don't need a loop for it. Just increment a variable every time you print the recipe name.
I've added it to the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.