|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
#include <stdio.h>
struct student
{
int num;
float score;
struct student *next;
};
void main()
{
struct student *p1, *p2, *head;
struct student student[10];
int n, i;
i = 0;
printf("Please enter the student's number and score: \n"); /*这里运行出来是正确的,编译运行出来的是这样的:
printf("Number:"); Please enter the student's number and score:
scanf("%d", &student[0].num); Number:1
printf("Score:"); Score:98*/
scanf("%.2f", &student[0].score);
p1 = &student[0];
p2 = &student[0];
head = NULL;
n = 0;
while( (*p1).num != 0 )
{
n = n + 1;
if(n == 1)
{
head = p1;
}
else
{
(*p2).next = p1;
}
p2 = p1;
i++;
p1 = &student;
printf("\n\nPlease enter the student's number and score: \n"); /*这段是复制前面那段printf的,然后进行了一些修改,然后编译运行的时候,想像上面printf那样打印出来,可是这里
printf("Number:"); 直接运行跳过了scanf("%d", &student.num);都没给我输入的机会,一直执行到scanf("%.2f", &student.score);
scanf("%d", &student.num); 这里才停下给我输入,最后运行出来是这样的:
printf("Score:"); Please enter the student's number and score:
scanf("%.2f", &student.score); Number:Score:94*/
}
(*p1).next = NULL;
}
有大佬能帮我看看这是怎么回事吗?
我这个现在可以运行,而且要注意输入的数据不能超过数组容量
不知道你的问题,具体出在了哪里。。
- #define _CRT_SECURE_NO_WARNINGS
- #include <stdio.h>
- #include <stdlib.h>
- struct student
- {
- int num;
- float score;
- struct student *next;
- };
- void main()
- {
- struct student *p1, *p2, *head;
- struct student student[10];
- int n, i;
- i = 0;
- printf("Please enter the student's number and score: \n");
- printf("Number:");
- scanf("%d", &student[0].num);
- printf("Score:");
- scanf("%f", &student[0].score);
- p1 = &student[0];
- p2 = &student[0];
- head = NULL;
- n = 0;
- while ((*p1).num != 0)
- {
- n = n + 1;
- if (n == 1)
- {
- head = p1;
- }
- else
- {
- (*p2).next = p1;
- }
- p2 = p1;
- i++;
- p1 = &student[i];
- printf("\n\nPlease enter the student's number and score: \n");
- printf("Number:");
- scanf("%d", &student[i].num);
- int ch;
- while ((ch = getchar()) != EOF && ch != '\n')
- {
- ;
- }
- printf("Score:");
- scanf("%f", &student[i].score);
- }
- (*p1).next = NULL;
- }
复制代码
|
|