| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 384 人关注过本帖
标题:结构体中字符串输出出错
只看楼主 加入收藏
其实、不想说
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:122
专家分:156
注 册:2011-3-3
结帖率:92.31%
收藏
已结贴  问题点数:20 回复次数:2 
结构体中字符串输出出错
#include "stdio.h"
#include "malloc.h"
#include "stdlib.h"
#include "string.h"
#include "conio.h"

struct student
{
    long int num;
    char name[20];
    char class_type[5];/*j is junior college,g is graduate,u is undergraduate */
    int tuition;
    int back_charge;
    struct student *next;
};

struct student *creat(void);
struct student *print(struct student *head);

int n;
int j_tuition;
int a;

main()
{
    struct student *head=NULL;
    head=creat();
    print(head);
    getch();
}

struct student *creat(void)
{
    struct student *p1,*p2,*head;

    clrscr();
    n=0;
    head=NULL;
    p1=p2=NULL;
    p1=p2=(struct student *)malloc(sizeof(struct student));
    printf("input the junior college student tuition:\n");
    scanf("%d", &j_tuition);
    printf("input number junior college:0 undergraduate:1 or graduate:2:\n");
    scanf("%d", &a);
    if(a==1)
        j_tuition *=0.8;
    else if(a==2)
            j_tuition *=0.56;
    printf("input the student num:\n");
    scanf("%d", &p1->num);
    while(p1->num != 0)
    {
        if(head==NULL)
            head=p1;
        else
            p2->next=p1;
        p2=p1;
        printf("\ninput the student name:\n");
        scanf("%s", p1->name);
        printf("\ninput the student class type j,u or g:\n");
        scanf("%s", p1->class_type);
        printf("\ninput the student tuition:\n");
        scanf("%d", &p1->tuition);
        p1->back_charge=j_tuition - p1->tuition;

        n++;

        p1=(struct student *)malloc(sizeof(struct student));
        printf("\ninput the student num:\n");
        scanf("%d", &p1->num);
    }
    p2->next=NULL;
    return(head);
}

struct student *print(struct student *head)
{
    struct student *p;
    if(head==NULL)
        printf("\nNo student information!\n");
    else
    {
        printf("the number of studnet is %d\n",n);
        printf("................................................\n");
        printf("|num\t|name\t|class_type\t|tuition\t|back_chrge|\n");
        printf("................................................\n");
        p=head;
        do
        {
            printf("|%d\t|%s\t|%s\t|",p->num,p->name,p->class_type);
            printf("%d\t|%d\t|\n", p->tuition, p->back_charge);
            printf("............................................\n");
            p=p->next;
        }while(p!=NULL);
    }
}
输出出错,第一个字符串name输出为NULL
搜索更多相关主题的帖子: college include junior 结构体 字符串 
2011-09-06 22:41
pauljames
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:千里冰封
威 望:9
帖 子:1555
专家分:10000
注 册:2011-5-8
收藏
得分:20 
我机器上感觉可以正常输出
input the junior college student tuition:
3
input number junior college:0 undergraduate:1 or graduate:2:
1
input the student num:
78
input the student name:
paul
input the student class type j,u or g:
j
input the student tuition:
23
input the student num:
0
the number of studnet is 1
................................................
|num    |name    |class_type    |tuition    |back_chrge|
................................................
|78    |paul    |j    |23    |-21    |
............................................

经常不在线不能及时回复短消息,如有c/单片机/运动控制/数据采集等方面的项目难题可加qq1921826084。
2011-09-07 06:30
其实、不想说
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:122
专家分:156
注 册:2011-3-3
收藏
得分:0 
回复 2楼 pauljames
我用的是win-tc,改了n遍就是输出会出错!!!
2011-09-07 07:33
快速回复:结构体中字符串输出出错
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022321 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved