| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 552 人关注过本帖
标题:本人新手,有程序请教一下各位高人
只看楼主 加入收藏
lastrobber
Rank: 2
来 自:廊坊
等 级:论坛游民
帖 子:26
专家分:52
注 册:2010-12-30
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:7 
本人新手,有程序请教一下各位高人
程序是这样的,做的是一个学生成绩管理系统,不知道为什么,总是在插入的时候不能读入学生的名字,请高人指点一下,下面是代码
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
    void read_func(void);
        void insert_func(void);
        void write_func(void);
        void sort_func(void);
        void delete_func(void);
        void display_func(void);
        void modify_func(void);
        void anykey_func(void);
        struct student
        {
            char name[20];
            int score;
            struct student *next;
        };
        struct student *ptr,*head,*current,*prev;
        int main(void)
        {
            char option1;
            system("cls");
            read_func();
            while(1)
            {
                printf("********************\n");
                printf("            1.insert\n");
                printf("            2.delete\n");
                printf("            3.display\n");
                printf("            4.modify\n");
                printf("            5.quit\n");
                printf("*********************\n");
                printf(" Please enter your choice(1-5)...");
                option1=getche();
                printf("\n");
        switch(option1)
        {
        case'1':
            insert_func();
            break;
        case'2':
            delete_func();
            break;
        case'3':
            display_func();
            break;
        case'4':
            modify_func();
            break;
        case'5':
            write_func();
            exit(0);
        }
        }
        }
        void read_func(void)
        {
            FILE *fptr;
            head=(struct student *)malloc(sizeof(struct student));
            head->next=NULL;
            if((fptr=fopen("xscj.txt","r"))==NULL)
            {
                printf("Data file not exist\n");
                printf("Press any to edit first record...\n");
                getch();
                insert_func();
            }
            else
            {
                ptr=(struct student *)malloc(sizeof(struct student));
                while(fscanf(fptr,"%s%d",ptr->name,&ptr->score)!=EOF)
                {
                    sort_func();
                ptr=(struct student *)malloc(sizeof(struct student));
                }
                fclose(fptr);
        }
        }
        void write_func(void)
        {
            FILE *fptr;
            fptr=fopen("xscj.txt","w");
            current=head->next;
            while(current!=NULL)
            {
                fprintf(fptr,"%s%d\n",current->name,current->score);
                    current=current->next;
            }
            fclose(fptr);
        }
        void insert_func(void)
        {
            char s_temp[4];
            ptr=(struct student *)malloc(sizeof(struct student));
            printf("Student name:");
            getche();
            gets(ptr->name);
            printf("Student score:");
            scanf("%d",&ptr->score);
            sort_func();
        }
        void sort_func(void)
        {
            prev=head;
            current=head->next;
            while((current!=NULL)&&(current->score>ptr->score))
            {
                prev=current;
                current=current->next;
            }
            ptr->next=current;
            prev->next=ptr;
        }
        void delete_func(void)
        {
            char del_name[20];
            printf("Delete student name:");
            gets(del_name);
            prev=head;
            current=head->next;
            while((current!=NULL)&&(strcmp(current->name,del_name)!=0))
            {
                prev=current;
                current=current->next;
            }
            if(current!=NULL)
            {
                prev->next=current->next;
                free(current);
                printf("%s student record deleted\n",del_name);
            }
            else
                printf("Student %s not found\n",del_name);
        anykey_func();
        }
        void modify_func(void)
        {
            char n_temp[20],s_temp[4];
            printf("Modify student name:");
            gets(n_temp);
            current=head->next;
            while((current!=NULL)&&(strcmp(current->name,n_temp)!=0))
            {
                prev=current;
                current=current->next;
            }
            if(current!=NULL)
            {
                printf("*******************\n");
                printf("Student name:%s\n",current->name);
                printf("Student score:%d\n",current->score);
                printf("*******************\n");
                printf("Please enter new score:");
                scanf("%d",&current->score);
                printf("%s student record modify\n",n_temp);
            }
            else
                printf("Student %s not found\n",n_temp);
            anykey_func();
        }
        void display_func(void)
        {
            int count=0;
            system("cls");
            if(head->next==NULL)
            {
                printf("No student record\n");
            }
            else
            {
                printf("NAME            SCORE\n");
                printf("-----------------------\n");
                current=head->next;
                while(current!=NULL)
                {
                    printf("%-20s %3d\n",current->name,current->score);
                    count++;
                    current=current->next;
                    if(count%20==0) getch();
                }
                printf("----------------------------\n");
                printf("Total %d record(s) found\n",count);
            }
            anykey_func();
        }
        void anykey_func(void)
        {
            printf("Press any key to continue...");
            getch();
            printf("\n");
        }
搜索更多相关主题的帖子: 名字 学生 
2010-12-30 09:50
lastrobber
Rank: 2
来 自:廊坊
等 级:论坛游民
帖 子:26
专家分:52
注 册:2010-12-30
收藏
得分:0 
没人回复呀,各位高人帮帮忙啦,小弟在此谢过了哈
2010-12-30 10:28
逐渐学习
Rank: 6Rank: 6
等 级:侠之大者
帖 子:113
专家分:454
注 册:2010-9-26
收藏
得分:20 
程序代码:
void insert_func(void)
        {
            //char s_temp[4];
            ptr=(struct student *)malloc(sizeof(struct student));
            printf("Student name:");
            //getche();
            //gets(ptr->name);
            scanf("%s",ptr->name);
            printf("Student score:");
            scanf("%d",&ptr->score);
            sort_func();
        }
在insert_func函数那里,修改对name的输入。使用scanf();
收到的鲜花
  • lastrobber2010-12-30 11:12 送鲜花  3朵   附言:解决了困扰我两天的问题,赞一个

帮人《---》帮己
2010-12-30 11:01
lastrobber
Rank: 2
来 自:廊坊
等 级:论坛游民
帖 子:26
专家分:52
注 册:2010-12-30
收藏
得分:0 
谢谢高手了啊,我开始的时候用的是scanf("%s",s_temp);但是输出的时候总是乱码,那弱弱的问一句,为什么用get就不行呢,为什么用scanf就可以呢?不是在while(1)
            {
                printf("********************\n");
                printf("            1.insert\n");
                printf("            2.delete\n");
                printf("            3.display\n");
                printf("            4.modify\n");
                printf("            5.quit\n");
                printf("*********************\n");
                printf(" Please enter your choice(1-5)...");
                option1=getche();
那个insert的回车影响的吗?
2010-12-30 11:10
逐渐学习
Rank: 6Rank: 6
等 级:侠之大者
帖 子:113
专家分:454
注 册:2010-9-26
收藏
得分:0 
gets()函数用来从标准输入设备(键盘)读取字符串直到换行符结束,但换行符会被丢弃,然后在末尾添加'\0'字符.

刚才在跟踪调试发现,第一次输入没什么问题,但是第二次输入时,直接就跳到Student score,Student name显示存储的是'\0'.这跟上次回车有关系的。

不过scanf("\s"))函数输入字符串时存在一个问题,就是如果输入了空格会认为字符串结束,空格后的字符将作为下一个输入项处理。

帮人《---》帮己
2010-12-30 11:31
lastrobber
Rank: 2
来 自:廊坊
等 级:论坛游民
帖 子:26
专家分:52
注 册:2010-12-30
收藏
得分:0 
恩恩,受教了!
2010-12-31 10:22
laoyang103
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:内蒙古包头
等 级:贵宾
威 望:19
帖 子:3082
专家分:11056
注 册:2010-5-22
收藏
得分:0 
scanf("%s",s_temp);   需要去地址的 scanf("%s",&s_temp);

                                         
===========深入<----------------->浅出============
2010-12-31 10:53
麦田打望者
Rank: 2
等 级:论坛游民
帖 子:62
专家分:34
注 册:2010-5-31
收藏
得分:0 
学习 学习
2011-05-04 19:04
快速回复:本人新手,有程序请教一下各位高人
数据加载中...
 
   



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

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