How to check if the string is already in an array of structures? The array is inside a structure and I don't know how to compare arrays inside the structure in C.
#include <stdio.h>
#include <string.h>
struct song {
char title[30];
char artist[30];
char album[30];
};
typedef struct playlist {
char sname[30];
char asongs[10];
int scount;
struct song music;
} songlist;
void addPlaylist(struct playlist *menu, int i) {
printf("Enter playlist name: ");
scanf("%s", menu[i].sname);
for (int j = 0; j < i; j++) {
if (strcmp(menu[i].sname, menu[j].sname) == 0) {
printf("already exists!\n");
} else {
printf("Successfully added playlist!\n");
}
}
}
int main() {
//menu. if choice == 1, go to addPlaylist func
}
I am asking the user for the name of playlist, and when a string is entered, it should be on the array sname
.
When the user wants to add another playlist and enters a playlist name that is already in the array sname
, the program should print already exists!
; otherwise, it should add on the array.
The array is inside a structure and I'm trying to compare the arrays to check if the user's input (which is a string) already exists inside the array. I tried using for loop but I am not sure about my conditions and the components inside the strcmp
function.
EDIT For example, if the user already entered multiple names like:
favorites
vibes
mood
and then, the user decided to add more playlist names then accidentally entered the name vibes
which already exists in the array.
If vibes
already exists, I want the program to print already exists!
I want to know how to properly check if the entered string already exists (especially if the string is inside a structure).
EDIT
Here's the output:
As you can see there's also problem in printing the successfully...
and already...