0 c
我试图用来strcmp比较存储在名为index的结构数组中的字符串.该索引包含一个名为webpage的字符串和一个名为rank的int.我们的想法是比较两个索引并创建第三个索引,其中包含前两个索引之间相同的网页以及它们各自排名的总和.
问题是,当我使用for循环查找索引之间类似页面的数量时,它总是返回7.它应该只返回4.我是否错误地使用了这个函数?
int thesame = 0;
for(i = 0; i < 8; i++) {
for(j = 0; j < 7; j++) {
if(strcmp(hotels[i].webpage,seattle[i].webpage) == 0) {
thesame++;
}
}
}
printf("%d\n", thesame);
return 0;
Run Code Online (Sandbox Code Playgroud)
我也明白,我有很多长度的硬编码值,这不是很好的编码实践.我计划在更改之前让程序正常工作.
if (strcmp (hotels[i].webpage, seattle[i].webpage) == 0)
Run Code Online (Sandbox Code Playgroud)
您正在使用索引i两次.我想你想要的是:
if (strcmp (hotels[i].webpage, seattle[j].webpage) == 0)
---------------------------------------^----------------
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1762 次 |
| 最近记录: |