本帖最后由 superbe 于 2020-8-2 18:48 编辑
把char *name[10]; 改成char name[10],即用一个字符数组保存候选人名字。后面输入循环中相应修改。
下面注释的行是修改前的行,修改在下一行。#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUM 4
int main(void)
{
//char *name[10];
char name[10];
int i = 0, j = 0;
struct person
{
char *name;
int votenum;
}candidate[4] = { { "小甲鱼", 0 },{ "杨幂", 0 },{ "范冰冰", 0 },{ "莫丁丁", 0 } };
printf("欢迎进入良好公民评选投票系统:()\n\n");
printf("候选人有:%s, %s , %s, %s(路人甲)\n\n", candidate[NUM - 4].name, candidate[NUM - 3].name, candidate[NUM - 2].name, candidate[NUM - 1].name);
while (i <= 9)
{
printf("第 %d 位投票, 请写下支持的候选人名字: ", i + 1);
//scanf("%s", &name[i]);
scanf("%s", name);
//if (strcmp(name[i], candidate[NUM - 4].name) == 0)
if (strcmp(name, candidate[NUM - 4].name) == 0)
{
candidate[NUM - 4].votenum++;
}
//else if (strcmp(name[i], candidate[NUM - 3].name) == 0)
else if (strcmp(name, candidate[NUM - 3].name) == 0)
{
candidate[NUM - 3].votenum++;
}
//else if (strcmp(name[i], candidate[NUM - 2].name) == 0)
else if (strcmp(name, candidate[NUM - 2].name) == 0)
{
candidate[NUM - 2].votenum++;
}
//else if (strcmp(name[i], candidate[NUM - 1].name) == 0)
else if (strcmp(name, candidate[NUM - 1].name) == 0)
{
candidate[NUM - 1].votenum++;
}
else
{
continue;
}
i++;
}
printf("\n");
for (; j < NUM; j++)
{
printf("%s 同学得票数为: %d\n", candidate[j].name, candidate[j].votenum);
}
i = 0;
if (candidate[i].votenum > candidate[i + 1].votenum && candidate[i].votenum > candidate[i + 2].votenum && candidate[i].votenum > candidate[i + 3].votenum)
{
printf("\n本次投票活动的胜利者是: %s\n\n", candidate[i].name);
}
else if (candidate[i + 1].votenum > candidate[i + 2].votenum && candidate[i + 1].votenum > candidate[i + 3].votenum)
{
printf("\n本次投票活动的胜利者是: %s\n\n", candidate[i + 1].name);
}
else if (candidate[i + 2].votenum > candidate[i + 3].votenum)
{
printf("\n本次投票活动的胜利者是: %s\n\n", candidate[i + 2].name);
}
else
{
printf("\n本次投票活动的胜利者是: %s\n\n", candidate[i + 3].name);
}
system("pause");
return 0;
}
提交上去才发现楼上已回答过了。 |