You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

C语言代码中argv字符串与用户输入不匹配问题求助

字符串匹配失败问题的解决方案

执行命令./plurality Alice Bob时,候选人名称已正确传入candidates数组,但后续用户输入的投票名称始终无法匹配,导致所有投票都被判定为无效。

原代码:

#include <cs50.h>
#include <stdio.h>
#include <string.h>

// Max number of candidates
#define MAX 9

// Candidates have name and vote count
typedef struct
{
    string name;
    int votes;
}
candidate;

// Array of candidates
candidate candidates[MAX];

// Number of candidates
int candidate_count;

// Function prototypes
bool vote(string name);
void print_winner(void);

int main(int argc, string argv[])
{
    for (int i = 0; i < candidate_count; i++)
    {
        candidates[i].name = argv[i + 1];
        printf("%s ", candidates[i].name);
        candidates[i].votes = 0;
    }
    printf("\n");

    int voter_count = get_int("Number of voters: ");

    // Loop over all voters
    for (int i = 0; i < voter_count; i++)
    {
        string name = get_string("Vote: ");

        // Check for invalid vote
        if (!vote(name))
        {
            printf("Invalid vote.\n");
        }
    }
}

// Update vote totals given a new vote
bool vote(string name)
{
    for (int i = 0; i < candidate_count; i++)
    {
        if (candidates[i].name == name)
        {
            candidates[i].votes++;
            return true;
        }
    }
    return false;
}

问题根源及修复

  1. 字符串比较方式错误
    C语言中,string(CS50定义的char*别名)是指针类型,==比较的是指针的内存地址,而非字符串内容。argv中的字符串是程序启动时加载的常量字符串,get_string获取的是堆上分配的新字符串,二者地址必然不同,导致匹配失败。
    修复:使用string.h中的strcmp函数比较字符串内容,当strcmp(a, b)返回0时,表示两个字符串内容完全一致。

  2. candidate_count未初始化
    main函数中直接使用未赋值的candidate_count,其值为随机垃圾值,会导致候选人数组初始化异常。
    修复:将candidate_count设置为argc - 1(argv[0]是程序名,后续参数为候选人)。

修复后的代码

#include <cs50.h>
#include <stdio.h>
#include <string.h>

// Max number of candidates
#define MAX 9

// Candidates have name and vote count
typedef struct
{
    string name;
    int votes;
}
candidate;

// Array of candidates
candidate candidates[MAX];

// Number of candidates
int candidate_count;

// Function prototypes
bool vote(string name);
void print_winner(void);

int main(int argc, string argv[])
{
    // 初始化候选人数量
    candidate_count = argc - 1;
    
    for (int i = 0; i < candidate_count; i++)
    {
        candidates[i].name = argv[i + 1];
        printf("%s ", candidates[i].name);
        candidates[i].votes = 0;
    }
    printf("\n");

    int voter_count = get_int("Number of voters: ");

    // Loop over all voters
    for (int i = 0; i < voter_count; i++)
    {
        string name = get_string("Vote: ");

        // Check for invalid vote
        if (!vote(name))
        {
            printf("Invalid vote.\n");
        }
    }
}

// Update vote totals given a new vote
bool vote(string name)
{
    for (int i = 0; i < candidate_count; i++)
    {
        // 使用strcmp比较字符串内容
        if (strcmp(candidates[i].name, name) == 0)
        {
            candidates[i].votes++;
            return true;
        }
    }
    return false;
}

内容的提问来源于stack exchange,提问作者Jack

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 07:50:23