Home > database >  How do I iterate over a struct array to check if string input is equal to a struct array value
How do I iterate over a struct array to check if string input is equal to a struct array value

Time:06-09

I am making a database program for playlists. A user can enter the name of a playlist they want to add, but if that name already exists in the struct array, it should show a prompt saying it already exists. The problem with the code I have written is that, if for instance, there are already 3 values in the struct array, and I enter a string input that matches the second or third name values in the struct array, it would still add a new playlist because since it did not match with the first value, it would go over to the else statement already and add the string input as a playlist name.

What I want it to do is to iterate over ALL existing struct array values until it finds a matching value, otherwise the user's string input should be added in the struct array. Below is the function I have written:

void addPlaylist(struct playlist *playlist, int (*index)){
    char temp[50];
    printf("What do you want your playlist to be called?: ");
    scanf("Is", temp);
    if((*index)!=0){
        for(int i=0; i<(*index);i  ){
            if((strcmp(temp, playlist[(i)].name))!=0){
                (*index)  ;
                strcpy(playlist[(*index)].name, temp);
                printf("Playlist successfully added!\n");
                break;
            }else{
                printf("%s already exists!\n", playlist[(i)].name);
            }
            
        }
    } else{
        strcpy(playlist[(*index)].name, temp);
        printf("Playlist successfully added!\n");
        (*index)  ;
    }
}

CodePudding user response:

Scan through the full aray to see if it already exists, set a variable if it does and set the returned index if you want it. If not then insert. You don't need to special case index == 0 because then the loop over the array is empty. Something like:

void addPlaylist(struct playlist *playlist, int (*index)){
    char temp[50];
    printf("What do you want your playlist to be called?: ");
    scanf("Is", temp);
    bool found=false;
    for(int i=0; i<(*index);i  ){
        if((strcmp(temp, playlist[(i)].name))==0){/*It matches*/
            found=true;
            *index=i;
            break;
        }
    }
    if(!found){
        strcpy(playlist[(*index)].name, temp);
    }
}

CodePudding user response:

What I want it to do is to iterate over ALL existing struct array values until it finds a matching value

Simply return; after printf("%s already exists!\n", playlist[(i)].name);


Consider returning a value to indicate why function ended.

// void addPlaylist(struct playlist *playlist, int (*index)){
int addPlaylist(struct playlist *playlist, int *index){
  assert(playlist && index && *index >= 0); // Handle pathologic cases.

  char temp[50];
  printf("What do you want your playlist to be called?: ");
  // Test return value 
  if (scanf("Is", temp) != 1) {
    return EOF;  // No valid input
  }

  // if ((*index)!=0){ // not needed
    
  for (int i = 0; i < *index; i  ) {
    // Simplify
    if (strcmp(temp, playlist[i].name) == 0) { 
      printf("%s already exists!\n", playlist[i].name);
      return 0;  // Nothing added
    }
  }

  strcpy(playlist[*index].name, temp);
  printf("Playlist successfully added!\n");
  return 1; // Success!
}
  • Related