c语言循环输入的时候,第一组数据和第二组数据一起输入了,但是最终显示出的数据顺序是对的,求大神指教!!!

yangyumizha 2017-09-22 02:34:11
输出结果如下:

代码如下:
#include<stdio.h>
#include<string.h>
struct students
{
char name[10];
char number[10];
int score;
struct students *next;
};
int main()
{
int i,j,k;
struct students stu[5],*head,*p;
char n[10],no[10];
for(i=0;i<5;i++)
{
printf("input the information of %dth student:\n",i+1);
scanf("%s%s%d\n",n,no,&stu[i].score);
strcpy(stu[i].name,n);
strcpy(stu[i].number,no);
}
head=&stu[0];
stu[4].next=NULL;
for(i=0;i<4;i++)
{
stu[i].next=&stu[i+1];
}
p=head;
printf("students list:\n");
while(p!=NULL)
{
printf("%s\t%s\t%d\n",p->name,p->number,p->score);
p=p->next;
}
}
...全文
177 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
自信男孩 2017-09-22
  • 打赏
  • 举报
回复
#include<stdio.h>
#include<string.h>

struct students
{
    char name[10];
    char number[10];
    int score;
    struct students *next;
};

int main()
{
    int i,j,k;
    struct students stu[5],*head,*p;
    char n[10],no[10];

    for(i=0;i<5;i++)
    {
        printf("input the information of %dth student:\n",i+1);
        scanf("%s%s%d",n,no,&stu[i].score);    /* delete \n*/
        strcpy(stu[i].name,n);
        strcpy(stu[i].number,no);
    }
    head=&stu[0];
    stu[4].next = NULL;
    for(i=0;i<4;i++)
    {
        stu[i].next=&stu[i+1];
    }
    p=head;
    printf("students list:\n");
    while(p!=NULL)
    {
        printf("%s\t%s\t%d\n",p->name,p->number,p->score);
        p=p->next;
    }
}
去掉scanf中的回车符;
wang0635 2017-09-22
  • 打赏
  • 举报
回复
输入的东西,只要没有被scanf取走(扫描后符合%所对应的格式),也没有被清空(flush),还是要被下一次的scanf扫描的

69,369

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧