我正在尝试获取将用户输入的字符串与用户的命令行字符串进行比较的代码。我尝试了其他方法,考虑到其编码几乎相同,我认为这样做会更容易,只是检查输入的字符串是否为先前的字符串之一,如果不是,则应该返回false但继续循环直到检查完所有用户的命令行字符串。
bool vote(string name)
{
for (int i = 0; i < candidate_count; i++) //loop through all the candidates
{
if (strcmp(name, candidates[i].name) == 0) //this part works however alone it considers all user strings to be true that's why I added the other code to have it check if the users string does not exist and if so it would return false
{
candidates[i].votes++;
return true;
}
else (strcmp(name, candidates[i].name) != 0); //expression result unused?
{
return false;
}
}
return false;
}
当我告诉它在测试完该字符串后返回循环时,我不明白为什么将表达式结果置于未使用状态。我应该做的是一次检查每个用户的字符串,然后说出此人是否退出或不存在并跳过投票。
答案 0 :(得分:0)
该行的结尾处有一个错字。
else (strcmp(name, candidates[i].name) != 0); //expression result unused?
^^^
答案 1 :(得分:0)
else (strcmp(name, candidates[i].name) != 0); //expression result unused?
“表达式结果未使用”,因为您不需要编写它-上面的if
条件已经隐含了它:
if (strcmp(name, candidates[i].name) == 0)
{
candidates[i].votes++;
return true;
}
else //<-- this else without any expression having the same result
{
return false;
}
实际上,您甚至根本不需要写else
。这将具有相同的效果:
for (int i = 0; i < candidate_count; i++) //loop through all the candidates
{
if (strcmp(name, candidates[i].name) == 0)
{
candidates[i].votes++;
return true;
}
}
return false; //<-- this return is enough
答案 2 :(得分:0)
else语句可能不使用函数中的条件
else (strcmp(name, candidates[i].name) != 0); //expression result unused?
此外,条件后还有分号。
看来你的意思
else if (strcmp(name, candidates[i].name) != 0) //expression result unused?
{
return false;
}
但是语法写就足够了
else
{
return false;
}
但是从逻辑上讲这是不正确的。因为如果第一个候选名称与指定名称不同,则将不考虑其他候选名称。
该函数可以通过以下方式编写
bool vote(string name)
{
int i = 0;
while ( i < candidate_count && strcmp(name, candidates[i].name) != 0 ) i++;
if ( i != candidate_count ) candidates[i].votes++;
return i != candidate_count;
}
答案 3 :(得分:0)
您的代码可以重新格式化为以下格式:
bool vote(string name)
{
for (int i = 0; i < candidate_count; i++) //loop through all the candidates
{
if (strcmp(name, candidates[i].name) == 0) //this part works however alone it considers all user strings to be true that's why I added the other code to have it check if the users string does not exist and if so it would return false
{
candidates[i].votes++;
return true;
}
else
{
strcmp(name, candidates[i].name) != 0; //expression result unused
}
return false;
}
return false;
}
现在,我认为这个问题非常容易发现。
您在这里还有另一个问题。如果第一个strcmp
不返回0,则for循环将在return false
处停止(在第一次迭代中)
答案 4 :(得分:0)
C期望在else
之后声明,而不是if
中的条件。在此代码中:
else (strcmp(name, candidates[i].name) != 0);
编译器将(strcmp(name, candidates[i].name) != 0);
解释为表达式语句。通常,表达式语句用于分配值(如x = 3;
)或调用函数(如printf("Hello, world.\n");
)。任何表达式都可以用作语句,因此(strcmp(name, candidates[i].name) != 0);
可以用作else
之后的语句。
但是,编译器发现未使用该表达式的结果。它不分配或修改值,也不调用具有副作用的函数,并且不具有任何其他副作用。因此,编译器会警告您未使用其结果。
else
语句不接受要求值的条件;如果if
条件失败,则仅执行其语句。只要有两种选择之间的选择,就使用if (condition) statement1 else statement2
,如果statement2
为假,则总是执行condition
。仅当存在三个或更多替代方案时,您才使用其他条件,并且这些条件将通过与if
一样的if (condition1) statement1 else if (condition2) statement2 else statement3
语句复合而包含在内。您的代码只有两种选择,因此将使用更简单的形式。
那么我们将拥有:
if (strcmp(name, candidates[i].name) == 0)
{
candidates[i].votes++;
return true;
}
else
{
return false;
}
这修复了“未使用表达式结果”的消息,但是随后出现了另一个问题。这不是您想要的代码。此代码使例程在看到数组中与false
参数不匹配的候选对象后立即返回name
。那不是你想做的。当数组中的名称与name
参数不匹配时,您要继续到数组中的下一个元素,继续循环。您不想返回,因此删除return
语句。根本不需要else
子句,代码可以是:
if (strcmp(name, candidates[i].name) == 0)
{
candidates[i].votes++;
return true;
}
然后循环将继续。如果在以后的迭代中找到匹配的名称,该候选人的票数将增加,并且例程将返回。如果循环结束但找不到匹配项,则代码将继续到循环外的return false;
,这就是您想要的。