| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1749 人关注过本帖
标题:请大神看看我这个子函数是不是有问题?
只看楼主 加入收藏
magabbc
Rank: 1
等 级:新手上路
帖 子:12
专家分:5
注 册:2015-4-2
结帖率:100%
收藏
已结贴  问题点数:20 回复次数:12 
请大神看看我这个子函数是不是有问题?
为什么我的主函数执行这个函数时当它执行完子函数后就自动退出
ps:我在主函数加了while(1) 一般来说是不会退出来的 只会一直循环执行这些子函数
下楼给代码
2015-04-02 21:21
magabbc
Rank: 1
等 级:新手上路
帖 子:12
专家分:5
注 册:2015-4-2
收藏
得分:0 
struct data *Out_all_list( struct data *head)
{
    struct data *lp;
    if(head!=NULL)
    {
        lp=head;
        while(lp!=NULL)
        {
            printf("\n num:\t%d\n name:\t%s\n tel:\t%s\n",lp->num,lp->name,lp->tel);
            lp=lp->next;
        }
        printf("\n\n\n");
    }
    else
    {
        printf("\n\n\n\n\t\tThis is empty list!\n\n\n\n");
    }
}
2015-04-02 21:22
magabbc
Rank: 1
等 级:新手上路
帖 子:12
专家分:5
注 册:2015-4-2
收藏
得分:0 
可以帮忙看看吗谢谢了
2015-04-02 21:30
longwu9t
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:6
帖 子:732
专家分:2468
注 册:2014-10-9
收藏
得分:10 
struct data *Out_all_list( struct data *head)

这个函数执行时是有返回值的 struct data *
但是从你所给的代码 没有看到返回值……

这样的程序不出错就怪了

Only the Code Tells the Truth             K.I.S.S
2015-04-02 21:33
magabbc
Rank: 1
等 级:新手上路
帖 子:12
专家分:5
注 册:2015-4-2
收藏
得分:0 
回复 4楼 longwu9t
请问 返还值return()里面应该填什么呢  我其他的子函数都没出现过这个问题的
2015-04-02 21:37
longwu9t
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:6
帖 子:732
专家分:2468
注 册:2014-10-9
收藏
得分:0 
回复 5楼 magabbc
我不神仙啊
不知道你程序的意图 没看到你的全码
叫我怎么给你意见
我只能把看到的眼前的bug告知你

Only the Code Tells the Truth             K.I.S.S
2015-04-02 21:40
TonyDeng
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:贵宾
威 望:304
帖 子:25859
专家分:48889
注 册:2011-6-22
收藏
得分:10 
以下是引用magabbc在2015-4-2 21:37:55的发言:

请问 返还值return()里面应该填什么呢  我其他的子函数都没出现过这个问题的


struct data *Out_all_list( struct data *head)
你敲上面被我描红的代码时没想过要返回什么的?没想好,你写上去干什么?

授人以渔,不授人以鱼。
2015-04-02 21:40
magabbc
Rank: 1
等 级:新手上路
帖 子:12
专家分:5
注 册:2015-4-2
收藏
得分:0 
回复 6楼 longwu9t
#include<stdio.h>
#include<stdlib.h>
#define NULL 0
struct data
{
    int num;
    char name[20];
    int tel[20];
    struct data *next;
};
struct data *put_onelist(struct data *head)
{

    struct data *p1;
    int i;
    if(head==NULL)
        printf(" \n\n\n\n\t\tThis is empty list!\n\n\n\n");
    else
    {
        printf("The serial number of input to output data node :   ");
        scanf("%d",&i);
        for(p1=head;p1->num!=i;p1=p1->next);
        printf(" Name: %s\n Tel: %s\n",p1->name,p1->tel);
    }
}
struct data *add_data(struct data *head)
{
    struct data *q1,*q2;
    int a;
    if(head==NULL)
    {
         printf("\n\n\n\n\t\tThis is empty list!\n\n\n\n");
    }
    else
    {
        printf("Input you need add data's num: ");
        scanf("%d",&a);
        for(q1=head;q1->num!=a-1;q1=q1->next);
        q2=(struct data*)malloc(sizeof(struct data));
        q2->num=a;
        printf("Please Input name : ");
        scanf("%s",q2->name);
        printf("       Input tel : ");
        scanf("%s",q2->tel);
        q2->next=q1->next;
        q1->next=q2;
    }
}
struct data *Out_all_list( struct data *head)
{
    struct data *lp;
    if(head!=NULL)
    {
        lp=head;
        while(lp!=NULL)
        {
            printf("\n num:\t%d\n name:\t%s\n tel:\t%s\n",lp->num,lp->name,lp->tel);
            lp=lp->next;
        }
        printf("\n\n\n");
    }
    else
    {
        printf("\n\n\n\n\t\tThis is empty list!\n\n\n\n");
    }
}
struct data *build_list(void)
{
    struct data *head,*p,*q;
    int n=1;
    p=q=head=(struct data*)malloc(sizeof(struct data));
    printf("Please input %d num:\t",n);
    scanf("%d",&(p->num));
    if(p->num==0)
    {
        printf("Please input num agine!\n");
    }
    printf("       Input %d name:\t",n);
    scanf("%s",&p->name);
    printf("       Input %d tel:\t",n);
    scanf("%s",&(p->tel));
    n++;
    while(p->num!=0)
    {
        p=(struct data*)malloc(sizeof(struct data));
        printf("please input %d num:\t",n);
        scanf("%d",&(p->num));
        if(p->num==0)
            p->next=NULL;
        else
        {
            printf("       Input %d name:\t",n);
            scanf("%s",&p->name);
            printf("       Input %ld tel:\t",n);
            scanf("%s",&(p->tel));
            q->next=p;
            q=p;
        }
        n++;
    }
    return (head);
}
void main()
{
    struct data *head=NULL;
    char ch;
    while(1)
    {
        fflush(stdin);
    printf("\n\n\t--------- welcome to use the data management system ---------\n\n");
    printf("\t\t(a) The input data \n\n\t\t(b) Output a data node\n\n\t\t(c) add a data \n\n\t\t(d) Output all data \n\n\t\t(e) exit \n\n");
    printf("\t-------------------------------------------------------------\n");
    printf("\n\tPlease enter the serial number and press enter to confirm: ");
    scanf("%c",&ch);
    switch(ch)
    {
    case 'a':
        {
            system("cls");
            head=build_list();
            break;
        }
    case 'b':
        {
            system("cls");
            put_onelist(head);
            break;
        }
    case 'c':
        {
            system("cls");
            add_data(head);
            break;
        }
    case 'd':
        {
            system("cls");
            Out_all_list(head);
            break;
        }
    case 'e':
        {
            return 0;
        }
    default:
        printf("\tError!please input the serial number.\n");
    }
    getch();
    system("cls");
    }
}
2015-04-02 21:41
magabbc
Rank: 1
等 级:新手上路
帖 子:12
专家分:5
注 册:2015-4-2
收藏
得分:0 
回复 7楼 TonyDeng
我把全部代码发出来了 朋友帮我看看可以不
2015-04-02 21:42
longwu9t
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:6
帖 子:732
专家分:2468
注 册:2014-10-9
收藏
得分:0 
回复 8楼 magabbc
不多说了,你先把编译器的报警去掉再说

C:\Windows\system32\cmd.exe /c (clang-cl -D_CRT_SECURE_NO_DEPRECATE -D_HAS_EXCEP
TIONS=0 -Wno-invalid-source-encoding main.c -o main.exe)
main.c(3,9) :  warning: 'NULL' macro redefined [-Wmacro-redefined]
#define NULL 0
        ^
C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\INCLUDE\stdio.h(120,9) :
 note:
      previous definition is here
#define NULL    ((void *)0)
        ^
main.c(23,49) :  warning: format specifies type 'char *' but the argument has
      type 'int *' [-Wformat]
        printf(" Name: %s\n Tel: %s\n",p1->name,p1->tel);
                                 ~~             ^~~~~~~
main.c(25,1) :  warning: control reaches end of non-void function
      [-Wreturn-type]
}
^
main.c(44,20) :  warning: format specifies type 'char *' but the argument has
      type 'int *' [-Wformat]
        scanf("%s",q2->tel);
               ~~  ^~~~~~~
               %d
main.c(48,1) :  warning: control reaches end of non-void function
      [-Wreturn-type]
}
^
main.c(57,76) :  warning: format specifies type 'char *' but the argument has
      type 'int *' [-Wformat]
  ...printf("\n num:\t%d\n name:\t%s\n tel:\t%s\n",lp->num,lp->name,lp->tel);
                                             ~~                     ^~~~~~~
main.c(66,1) :  warning: control reaches end of non-void function
      [-Wreturn-type]
}
^
main.c(79,16) :  warning: format specifies type 'char *' but the argument has
      type 'char (*)[20]' [-Wformat]
    scanf("%s",&p->name);
           ~~  ^~~~~~~~
main.c(81,16) :  warning: format specifies type 'char *' but the argument has
      type 'int (*)[20]' [-Wformat]
    scanf("%s",&(p->tel));
           ~~  ^~~~~~~~~
main.c(93,24) :  warning: format specifies type 'char *' but the argument has
      type 'char (*)[20]' [-Wformat]
            scanf("%s",&p->name);
                   ~~  ^~~~~~~~
main.c(94,46) :  warning: format specifies type 'long' but the argument has type

      'int' [-Wformat]
            printf("       Input %ld tel:\t",n);
                                 ~~~         ^
                                 %d
main.c(95,24) :  warning: format specifies type 'char *' but the argument has
      type 'int (*)[20]' [-Wformat]
            scanf("%s",&(p->tel));
                   ~~  ^~~~~~~~~
main.c(103,1) :  warning: return type of 'main' is not 'int'
      [-Wmain-return-type]
void main()
^
main.c(103,1) :  note: change return type to 'int'
void main()
^~~~
int
main.c(143,13) :  error: void function 'main' should not return a value
      [-Wreturn-type]
            return 0;
            ^      ~
main.c(148,5) :  warning: implicit declaration of function 'getch' is invalid in

      C99 [-Wimplicit-function-declaration]
    getch();
    ^
14 warnings and 1 error generated.
shell returned 1
Hit any key to close this window...

[ 本帖最后由 longwu9t 于 2015-4-2 21:44 编辑 ]

Only the Code Tells the Truth             K.I.S.S
2015-04-02 21:42
快速回复:请大神看看我这个子函数是不是有问题?
数据加载中...
 
   



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

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