/结构体数组: 有 3 个候选人, 每个选民只能投票选一人, 要求编一个统计选票
的程序, 先后输入被选人的名字, 最后输出各人得票结果 (第 9 章课程例题)/
#include<stdio.h>
#include<string.h>//字符串函数记得声明库
struct person
{
char name[20];
int voted;
}candidate[3]{ {"a",0 }, {"b", 0},{"c",0} };
int main()
{
int i;
char temp=(char)'a';
char vote_to[20] = { 0 };
//scanf_s("%s", temp, 5);//先有输入才能判断
while(temp != "over")
{
printf("which candidate would you choose:\n");
scanf_s("%s", vote_to,5);
temp = vote_to;//防止比较函数结果成为循环条件
for(i=0;i<3;i++)
if (strcmp(vote_to, candidate[i].name) == 0)//==比较不是赋值
candidate[i].voted++;
//else
//continue;
}
for (i = 0; i < 3; i++)
{
printf("%s is:\n", candidate[i].name);
printf("%5d\n", candidate[i].voted);
}
return 0;
}
那么晚打扰大家了,怎么程序在循环结构结束后不会跳出呢
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
3条回答 默认 最新
fuill 2022-01-09 00:34关注while(temp != "over")
改成
while(strcmp(temp,"over")==0)
字符串比较用strcmp()函数,相等返回0,不相等返回-1或1本回答被题主选为最佳回答 , 对您是否有帮助呢?评论 打赏 举报解决 1无用