[英]How do I iterate over a struct array to check if string input is equal to a struct array value
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.
我编写的代码的问题是,例如,如果结构数组中已经有 3 个值,并且我输入了一个与结构数组中的第二个或第三个名称值匹配的字符串输入,它仍然会添加一个新的playlist 因为它与第一个值不匹配,所以它会 go 已经转到 else 语句并将输入的字符串添加为播放列表名称。
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:
下面是我写的function:
void addPlaylist(struct playlist *playlist, int (*index)){
char temp[50];
printf("What do you want your playlist to be called?: ");
scanf("%49s", 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)++;
}
}
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.
您不需要特殊情况 index == 0 因为这样数组上的循环是空的。 Something like:
就像是:
void addPlaylist(struct playlist *playlist, int (*index)){
char temp[50];
printf("What do you want your playlist to be called?: ");
scanf("%49s", 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);
}
}
What I want it to do is to iterate over ALL existing struct array values until it finds a matching value
我想要它做的是迭代所有现有的结构数组值,直到找到匹配值
Simply return;
只需
return;
after printf("%s already exists,\n". playlist[(i)];name);
printf("%s already exists,\n". playlist[(i)];name);
Consider returning a value to indicate why function ended.考虑返回一个值来指示 function 结束的原因。
// 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("%49s", 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!
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.