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

C语言投票计数函数错误排查:ASCII字符读取与大小写匹配问题

投票统计代码问题求助

我的任务是模拟投票流程。

我是编程初学者,我的代码里的vote_count()函数存在错误,尝试了各种方法仍未找到问题所在。我的思路是转换输入投票字符的大小写,将其对应的ASCII整数值与候选人姓名的首字母匹配,若匹配则增加对应候选人的票数。

我在将单个字符读取为ASCII值时遇到了问题,在vote_count()的循环中打印这些值时出现异常:输入的每个字母都被解析为相同的整数值。

请为我提供修复这段问题代码的建议,谢谢!

#include <stdio.h>
// Library to easily change an input character to its lower-case variant
#include <ctype.h>
// Library to handle comparison of strings
#include <string.h>

#define NAME_BUFFER 20
#define NUMBER_OF_CANDIDATES 3

// Define with type identifiers
void vote_count(int number_of_voters, char * candidate_1, char * candidate_2, char * candidate_3);

int main(void)
{
    int number_of_voters;
    printf("Number of Voters: ");
    scanf("%d", &number_of_voters);

    char candidate_1[NAME_BUFFER], candidate_2[NAME_BUFFER], candidate_3[NAME_BUFFER];
    
    printf("Name of 1st Candidate: ");
    scanf("%s", candidate_1);
    
    printf("Name of 2nd Candidate: ");
    scanf("%s", candidate_2);
    
    printf("Name of 3rd Candidate: ");
    scanf("%s", candidate_3);

    // Pass without type identifiers
    vote_count(number_of_voters, candidate_1, candidate_2, candidate_3);

    return 0;
}

// Re-define with type identifiers
void vote_count(int number_of_voters, char * candidate_1, char * candidate_2, char * candidate_3)
{
    int candidate_1_prefix_letter = candidate_1[0];
    int candidate_2_prefix_letter = candidate_2[0];
    int candidate_3_prefix_letter = candidate_3[0];

    int candidate_1_votes = 0, candidate_2_votes = 0, candidate_3_votes = 0;
    
    // Declare a variable for the vote choice
    int vote_choice;

    for (int counter = 0; counter < number_of_voters; counter++)
    {   

        scanf("%d", &vote_choice);        
        // Use this when colleting only one character at a time
        getchar();
        

        // Check who was voted for
        if (toupper(vote_choice) == candidate_1_prefix_letter)
        {
            candidate_1_votes++;
        }
        else if (toupper(vote_choice) == candidate_2_prefix_letter)
        {
            candidate_2_votes++;
        }
        else if (toupper(vote_choice) == candidate_3_prefix_letter)
        {
            candidate_3_votes++;
        }
        
    }

    // Print the votes
    printf("%s: %d\n" , candidate_1, candidate_1_votes);
    printf("%s: %d\n" , candidate_2, candidate_2_votes);
    printf("%s: %d\n" , candidate_3, candidate_3_votes);


    // Check who won
    if (((candidate_1_votes == candidate_2_votes) && (candidate_1_votes != 0)) || ((candidate_1_votes == candidate_3_votes) && (candidate_1_votes != 0)) || ((candidate_2_votes == candidate_3_votes) && (candidate_3_votes != 0)))
    {
        printf("Tie!\n");
    }
    else if ((candidate_1_votes > candidate_2_votes) && (candidate_1_votes > candidate_3_votes))
    {
        printf("%s has won the vote! Praise %s!\n", candidate_1, candidate_1);
    }
    else if ((candidate_2_votes > candidate_1_votes) && (candidate_2_votes > candidate_3_votes))
    {
        printf("%s has won the vote! Praise %s!\n", candidate_2, candidate_2);
    }
    else if ((candidate_3_votes > candidate_1_votes) && (candidate_3_votes > candidate_2_votes))
    {
        printf("%s has won the vote! Praise %s!\n", candidate_3, candidate_3);
    }

}

当我尝试在vote_count函数的循环中打印单个字符值时,输入的每个字符都被解析为相同的整数值。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.13 17:55:25