首页 > 解决方案 > strcmp 中未使用的表达式结果

问题描述

我正在尝试获取代码以将用户输入的字符串与用户的命令行字符串进行比较。我尝试了其他方法,我认为考虑到它的编码几乎相同,这会更容易,并且只是检查输入的字符串是否是以前的字符串之一,如果不是,它应该返回 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;
}

当我告诉它在测试该字符串后返回循环时,我不明白为什么它会不使用表达式结果。我所拥有的应该是一次检查每个用户字符串,然后说出该人是否退出或者他们是否不存在并跳过该投票。

标签: cbooleancs50

解决方案


你在行尾有一个错字。

 else (strcmp(name, candidates[i].name) != 0); //expression result unused?
                                            ^^^

推荐阅读