嗨,这里的初学者。我的代码的函数count_vote()
是错误的,我已经用尽了自己试图找出我做错了什么。我的攻击计划是改变我输入的“vote”字符的大小写,然后将与它的ASCII字符相关联的整数值与候选人的第一个字母相等。如果有匹配,我将递增与该候选人相关联的选票计数变量。
我在将单个字符作为ASCII值阅读时遇到了麻烦,并且当我试图在count_vote()
for循环中打印这些值时出现了奇怪的行为;输入的每个字母被解释为具有相同的整数值。
请提供一些建议,我可以如何修复我的janky代码。谢谢!
#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);
}
}
当试图从我的count_vote
的for循环中打印单个字符值时,每个字符输入都被解释为具有相同的整数值。
1条答案
按热度按时间fykwrbwg1#
scanf("%d", &vote_choice);
不会将字符读取为ASCII。它会在输入中寻找十进制数字,例如347
或-92
,将其转换为int
,并将其储存在vote_choice
中。要读取单个字符,请使用
char vote_choice;
和scanf(" %c", &vote_choice);
。%c
前面的空格告诉scanf
读取并丢弃流中的空白字符,直到找到非空白字符。