结构体数组投票问题
#include <stdio.h>#include <stdlib.h>
#define NUM 4
int main(void)
{
char *name;
int i = 0, j;
struct person
{
char *name;
int votenum;
}candidate = {{"小甲鱼", 0}, {"杨幂", 0}, {"范冰冰", 0}, {"莫丁丁", 0}};
printf("欢迎进入良好公民评选投票系统:()\n\n");
printf("候选人有:%4s,%4s,%4s,%4s(路人甲)\n\n", candidate.name, candidate.name, candidate.name, candidate.name);
while (i <= 9)
{
printf("第 %d 位投票, 请写下支持的候选人名字: ", i+1);
scanf("%s", name+i);
if (name == candidate.name)
{
candidate.votenum++;
}
else if(name == candidate.name)
{
candidate.votenum++;
}
else if(name == candidate.name)
{
candidate.votenum++;
}
else
{
candidate.votenum++;
}
i++;
}
printf("\n");
for(j = 0; j < NUM; j++)
{
printf("%3s 同学得票数为: %d\n", candidate.name, candidate.votenum);
}
printf("\n本次投票活动的胜利者是: %s\n\n", candidate.name);
system("pause");
return 0;
}
该代码运行时,为什么最后统计的数只有最后一个名额叠加了,这个while循环问题在哪? 字符串比较用strcmp吧 字符串用strcmp比较,if 里一直为假,else 的时候就直接运行了,
最后也没有比较得票 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUM 4
int main(void)
{
char *name;
int i =0,j = 0;
struct person
{
char *name;
int votenum;
}candidate = {{"小甲鱼", 0}, {"杨幂", 0}, {"范冰冰", 0}, {"莫丁丁", 0}};
printf("欢迎进入良好公民评选投票系统:()\n\n");
printf("候选人有:%4s,%4s,%4s,%4s(路人甲)\n\n", candidate.name, candidate.name, candidate.name, candidate.name);
while (i <= 9)
{
printf("第 %d 位投票, 请写下支持的候选人名字: ", i+1);
scanf("%s", &name);
if (strcmp(name,candidate.name) == 0)
{
candidate.votenum++;
}
else if(strcmp(name,candidate.name) == 0)
{
candidate.votenum++;
}
else if(strcmp(name,candidate.name) == 0)
{
candidate.votenum++;
}
else if(strcmp(name,candidate.name) == 0)
{
candidate.votenum++;
}
else
{
continue;
}
i++;
}
printf("\n");
for(; j < NUM; j++)
{
printf("%s同学得票数为: %d\n", candidate.name, candidate.votenum);
}
i = 0;
if (candidate.votenum > candidate.votenum && candidate.votenum > candidate.votenum && candidate.votenum > candidate.votenum )
{
printf("\n本次投票活动的胜利者是: %s\n\n", candidate.name);
}
else if (candidate.votenum > candidate.votenum && candidate.votenum > candidate.votenum)
{
printf("\n本次投票活动的胜利者是: %s\n\n", candidate.name);
}
else if (candidate.votenum > candidate.votenum)
{
printf("\n本次投票活动的胜利者是: %s\n\n", candidate.name);
}
else
{
printf("\n本次投票活动的胜利者是: %s\n\n", candidate.name);
}
system("pause");
return 0;
}
现在是循环不了了是什么情况??? 兔比南博万 发表于 2020-8-2 17:43
#include
#include
#include
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NUM 4
int main(void)
{
char name;
int i = 0, j = 0;
struct person
{
const char* name;
int votenum;
}candidate = { {"小甲鱼", 0}, {"杨幂", 0}, {"范冰冰", 0}, {"莫丁丁", 0} };
printf("欢迎进入良好公民评选投票系统:()\n\n");
printf("候选人有:%4s,%4s,%4s,%4s(路人甲)\n\n", candidate.name, candidate.name, candidate.name, candidate.name);
for (; i <= 9; i++)
{
printf("第 %d 位投票, 请写下支持的候选人名字: ", i + 1);
scanf("%s", name);
if (strcmp(name, candidate.name) == 0)
{
candidate.votenum++;
}
else if (strcmp(name, candidate.name) == 0)
{
candidate.votenum++;
}
else if (strcmp(name, candidate.name) == 0)
{
candidate.votenum++;
}
else if (strcmp(name, candidate.name) == 0)
{
candidate.votenum++;
}
else
{
continue;
}
// i++;
}
printf("\n");
for (; j < NUM; j++)
{
printf("%s同学得票数为: %d\n", candidate.name, candidate.votenum);
}
i = 0;
if (candidate.votenum > candidate.votenum && candidate.votenum > candidate.votenum && candidate.votenum > candidate.votenum)
{
printf("\n本次投票活动的胜利者是: %s\n\n", candidate.name);
}
else if (candidate.votenum > candidate.votenum && candidate.votenum > candidate.votenum)
{
printf("\n本次投票活动的胜利者是: %s\n\n", candidate.name);
}
else if (candidate.votenum > candidate.votenum)
{
printf("\n本次投票活动的胜利者是: %s\n\n", candidate.name);
}
else
{
printf("\n本次投票活动的胜利者是: %s\n\n", candidate.name);
}
system("pause");
return 0;
}
改为这样即可
兔比南博万 发表于 2020-8-2 17:43
#include
#include
#include
好像没我啥事了{:10_284:}
页:
[1]