标题:结构体中字符串输出出错
取消只看楼主
其实、不想说
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:122
专家分:156
注 册:2011-3-3
结帖率:92.31%
已结贴  问题点数:20 回复次数:1 
结构体中字符串输出出错
#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
其实、不想说
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:122
专家分:156
注 册:2011-3-3
得分:0 
回复 2楼 pauljames
我用的是win-tc,改了n遍就是输出会出错!!!
2011-09-07 07:33



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-349332-1-1.html




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

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