函数返回值在数组(C++、Splashkit)中的位置



所以我设计了一个数组,用户在其中输入五个名称,终端将打印这些名称并识别最长的名称。我现在尝试引入一个函数,用户在其中输入一个名称,如果该名称在数组中,则打印该名称的位置(如果该名称不在,则打印-1(。我使用的是Splashkit库,它有点不同。我试过几种不同的变体,但到目前为止运气不好。这就是我所拥有的。。。

int index_of(string value, string names[], int size)
{
value = read_string("Enter name to find index: ");

for(int i = 0; i < size; i++)
{
if ((names[i]) == string(value))
{
return i;
write_line("The index is: ");
write(i);
}
}
return -1;
}

它编译得很好,但就是不起作用,即使我在main中调用它。有人有什么想法吗?谢谢:(

int main()
{
#define SIZE 5
string names[SIZE];
int i;
i = 0;
while( i < SIZE )
{
names[i] = read_string("Enter a name:");
i++;
}
for(i = 0; i < SIZE; i++)
{
write_line(names[i]);
}
int total;
total = total_length(names, SIZE);
write("Total length: ");
write_line(total);
bool has_connor;
has_connor = contains(names, SIZE, "connor");
if ( has_connor ) write_line("contains Connor");

write("The longest name is: ");
write_line(longest_name(names, SIZE));
int index_of(int value, string names[], int size);
return 0;
}

我认为这些行write_line("The index is: "); write(i);是无用的,因为返回在它们之前。尝试

write_line("The index is: ");
write(i);
return i;

最新更新