C语言问题,为什么我的第一个for循环只能执行一次,然后直接跳到return了?
#include<stdio.h>#include<malloc.h>structstudent{intage;doublescore;charname[30];};in...
# include <stdio.h>
# include <malloc.h>
struct student
{
int age;
double score;
char name[30];
};
int main(void)
{
struct student * p, t;
int len, i, j;
printf("输入学生个数:");
printf("len=");
scanf_s("%d", &len);
p = (struct student *)malloc(len * sizeof(struct student));
for (i = 1; i <= len; ++i)
{
printf("输入第%d学生的信息:\n", i);
printf("输入学生年龄:");
scanf_s("%d", &p[i]);
printf("输入学生成绩:");
scanf_s("%lf", &p[i]);
printf("输入学生姓名:");
scanf_s("%s", (p + i)->name);
}
printf("\n\n");
for (i = 0; i < len - 1; ++i)
{
for (j = 0; j < len - 1 - i;++j)
if (p[j].score > p[j + 1].score)
{
t = p[j];
p[j] = p[j + 1];
p[j + 1] = t;
}
}
for (i = 1; i <= len; ++i)
{
printf("第%d个学生的信息是:\n\n", i);
printf("年纪是%d\n\n", p[i].age);
printf("分数是%lf\n\n", p[i].score);
printf("姓名是%s\n\n\n\n", p[i].name);
}
return 0;
}
printf("输入学生年龄:");
scanf_s("%d", &p[i].age);
printf("输入学生成绩:");
scanf_s("%lf", &p[i].score);
忘记打了, 加上了 还是只能执行一次然后直接结束本程序 展开
# include <malloc.h>
struct student
{
int age;
double score;
char name[30];
};
int main(void)
{
struct student * p, t;
int len, i, j;
printf("输入学生个数:");
printf("len=");
scanf_s("%d", &len);
p = (struct student *)malloc(len * sizeof(struct student));
for (i = 1; i <= len; ++i)
{
printf("输入第%d学生的信息:\n", i);
printf("输入学生年龄:");
scanf_s("%d", &p[i]);
printf("输入学生成绩:");
scanf_s("%lf", &p[i]);
printf("输入学生姓名:");
scanf_s("%s", (p + i)->name);
}
printf("\n\n");
for (i = 0; i < len - 1; ++i)
{
for (j = 0; j < len - 1 - i;++j)
if (p[j].score > p[j + 1].score)
{
t = p[j];
p[j] = p[j + 1];
p[j + 1] = t;
}
}
for (i = 1; i <= len; ++i)
{
printf("第%d个学生的信息是:\n\n", i);
printf("年纪是%d\n\n", p[i].age);
printf("分数是%lf\n\n", p[i].score);
printf("姓名是%s\n\n\n\n", p[i].name);
}
return 0;
}
printf("输入学生年龄:");
scanf_s("%d", &p[i].age);
printf("输入学生成绩:");
scanf_s("%lf", &p[i].score);
忘记打了, 加上了 还是只能执行一次然后直接结束本程序 展开
2个回答
展开全部
# include <stdio.h>
# include <malloc.h>
struct student
{
int age;
double score;
char name[30];
};
int main(void)
{
struct student * p, t;
int len, i, j;
printf("输入学生个数:");
printf("len=");
scanf_s("%d", &len);
p = (struct student *)malloc(len * sizeof(struct student));
for (i = 0; i < len; ++i)
{
printf("输入第%d学生的信息:\n", i+1);
printf("输入学生年龄:");
scanf_s("%d", &p[i].age);
printf("输入学生成绩:");
scanf_s("%lf", &p[i].score);
printf("输入学生姓名:");
scanf_s("%s", (p + i)->name);
}
printf("\n\n");
for (i = 0; i < len - 1; ++i)
{
for (j = 0; j < len - 1 - i;++j)
if (p[j].score > p[j + 1].score)
{
t = p[j];
p[j] = p[j + 1];
p[j + 1] = t;
}
}
for (i = 0; i < len; ++i)
{
printf("第%d个学生的信息是:\n\n", i+1);
printf("年纪是%d\n\n", p[i].age);
printf("分数是%lf\n\n", p[i].score);
printf("姓名是%s\n\n\n\n", p[i].name);
}
return 0;
}
# include <malloc.h>
struct student
{
int age;
double score;
char name[30];
};
int main(void)
{
struct student * p, t;
int len, i, j;
printf("输入学生个数:");
printf("len=");
scanf_s("%d", &len);
p = (struct student *)malloc(len * sizeof(struct student));
for (i = 0; i < len; ++i)
{
printf("输入第%d学生的信息:\n", i+1);
printf("输入学生年龄:");
scanf_s("%d", &p[i].age);
printf("输入学生成绩:");
scanf_s("%lf", &p[i].score);
printf("输入学生姓名:");
scanf_s("%s", (p + i)->name);
}
printf("\n\n");
for (i = 0; i < len - 1; ++i)
{
for (j = 0; j < len - 1 - i;++j)
if (p[j].score > p[j + 1].score)
{
t = p[j];
p[j] = p[j + 1];
p[j + 1] = t;
}
}
for (i = 0; i < len; ++i)
{
printf("第%d个学生的信息是:\n\n", i+1);
printf("年纪是%d\n\n", p[i].age);
printf("分数是%lf\n\n", p[i].score);
printf("姓名是%s\n\n\n\n", p[i].name);
}
return 0;
}
更多追问追答
追问
改了 还是不行, 我觉得问题出在第一个for循环中,但是我找不出来。
追答
scanf_s("%lf", &p[i].score);改成scanf_s("%f", &p[i].score);试试。
2017-09-02
展开全部
可能是你输入的len值有误导致循环直接跳出
追问
不会吧?len的值我都试过了30不行,5也不行,3也一样 直接跳出程序。剩下的代码全不执行,我用的是VS2013
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询