问题描述
- C语言结构体循环赋值老是出现随机数。。
-
问题是这样的 本人大一 C语言课程设计 做一个运动员管理系统 部分代码如下
#include
#include
#include
#include
void search();
void enter();
void browse();
void save();
void add();
int load(int y);
int exchange(int x);
void Department();
void sex();
void project();
void rank();
void Score();
void name();
struct on{
int N;}z;
struct department{
char departmentName[11];
char project[11];
int rank;
int score;}department[10];
struct sportsMen{
char sportmenName[11];
char departmentName[11];
char sex[5];
char project[11];
int rank;
int score;}sportMen[50];
struct score{
char departmentName[11];
int rank[10];
int scoreSum[10];}score[10],k;
struct another{
char sportmenName[11];
char departmentName[11];
char sex[5];
char project[11];
int rank[10];
int score[4];}another[50];
int main(void)
{
int d;int k;
enter();
k=z.N;
for(d=0;d<k;d++)
{
printf("姓名:%s 性别:%s 部门:%s 项目:%s 名次:%d 得分:%dn",sportMen[d].sportmenName,sportMen[d].sex,
sportMen[d].departmentName,sportMen[d].project,&sportMen[d].rank,&sportMen[d].score);
}
}
void enter()//输入函数
{
int i,G,j;
system("CLS");
printf("请输入你想要录入的运动员人数(0-50)?");
scanf("%d",&G);z.N=G; for(i=0;i<G;i++)//给sportMen赋值 { printf("姓名:");scanf("%s",sportMen[i].sportmenName); printf("性别:");scanf("%s",sportMen[i].sex); printf("部门:");scanf("%s",sportMen[i].departmentName); printf("项目:");scanf("%s",sportMen[i].project); printf("排名:");scanf("%d",&sportMen[i].rank); system("CLS"); } for(j=0;j<G;j++)//给sportMen中的score赋值 { switch(sportMen[j].rank) { case 1:if(sportMen[j].project=="aa"&&"bb"&&"cc")sportMen[j].score=7; else sportMen[j].score=5; break; case 2:if(sportMen[j].project=="aa"&&"bb"&&"cc")sportMen[j].score=5; else sportMen[j].score=3; break; case 3:if(sportMen[j].project=="aa"&&"bb"&&"cc")sportMen[j].score=3; else sportMen[j].score=2; break; case 4:sportMen[j].score=2; break; case 5:sportMen[j].score=1; break; default:sportMen[j].score=0; break; } }
}
在利用for循环给结构体sportMen赋值时 不能输出想要的结果,排名和得分会出现随机数
请教各位我这段代码哪里有问题?`
解决方案
数字和字符混合输入注意用get()去掉末尾的空格,这个影响你了
时间: 2025-01-21 07:58:35