2017-06-01 35 views
0

對於長碼iam仍然是新的c ..它只是發生在我顯示回數據的活動數據總是有1個數據插入,這使得我的程序不響應。顯示時出現鬼影輸出;嵌套鏈表[C]

#include <stdio.h> 
#include <stdlib.h> 

struct activity{ 
    int reps, calBurn; 
    char aName[30]; 

    struct activity *ptrnexto; 
}; 

struct activity *newptro, *curptro, *prevptro; 

struct plan { 
    char pName[30], focus; 
    int recWieght; 

    struct plan *ptrnext; 
    struct activity *act; 
}; 

struct plan *headptr, *newptr, *curptr, *prevptr; 

int menu(int num); 
int insertPlan(); 
int displayPlans(); 

int main() 
{ 
    menu(1); 
} 

//菜單FUNC

int menu(int no) 
{ 
    int choice; 
    if (no==0){ 
     return 0; 
    } 
    while(no==1) 
    { 
     system("cls"); 
     printf("\n"); 
     printf("\t\t\t\t Welcome!\n"); 
     printf("\t\t\t Input a new fitness plan\t [1]\n" 
       "\t\t\t View ALL fitness plan\t [5]\n"); 
     printf("\n\t\t\t   -----------\n "); 
     printf("\t\t\t Choice: "); 
     scanf("%d",&choice); 
     fflush(stdin); 
     system("cls"); 
     switch(choice) 
     { 
      case 1: 
      { 
       insertPlan(); 
       break; 
      } 
      case 5: 
      { 
       displayPlans(); 
       return 0; 
       break; 
      } 
      default: 
      { 
       printf("Invalid key entry."); 
       break; 
      } 

     }//end switch 
     printf("\n\tDo you wish to return to menu?\n" 
       "\tYes[1]\t\tNo[0]\n\t"); 
     printf("--> "); 
     scanf("%d",&no); 
     return menu(no); 
    } 
    return 0; 
} 

//插入FUNC

int insertPlan() 
{ 

    int pos=0,ch; 
    newptr = (struct plan*)malloc(sizeof(struct plan)); 

    printf("\n Recomended Wieght : "); 
    scanf(" %d",&newptr->recWieght); 
    printf("\n Plan Name : "); 
    scanf(" %s",&newptr->pName); 
    printf("\nUpperBody(U)--LowerBody(L)--Abdomen(A)--Stamina(S) \nPlan Focus : "); 
    scanf(" %c",&newptr->focus); 
    printf("\n Put in the plans : "); 
    while(ch!=0) 
    { 
     newptro= (struct activity*)malloc(sizeof(struct activity)); 
     pos = pos +1; 
     printf("\nInsert in actvity %d",pos); 
     printf("\n Activity Name : "); 
     scanf("%s",&newptro->aName); 
     printf("\n how many reps : "); 
     scanf("%d",&newptro->reps); 
     printf("\n estimated calory burn : "); 
     scanf("%d",&newptro->calBurn); 

     if (newptr->act==NULL) 
     { 
      newptr->act=newptro; 
      newptro->ptrnexto=NULL; 
     } 
     else 
     { 
      newptro->ptrnexto=newptr->act; 
      newptr->act=newptro; 
     } 
     printf("\n\tIs there anymore plans?\n" 
       "\tYes[1]\t\tNo[0]\n\t"); 
     scanf("%d",&ch); 
    } 


    if (headptr==NULL) 
    { 
     headptr=newptr; 
     newptr->ptrnext=NULL; 
    } 
    else 
    { 
     newptr->ptrnext=headptr; 
     headptr=newptr; 
    } 
    return 
menu(1); 
} 

//顯示

int displayPlans() 
{ 
    int pos=0; 
    if (headptr==NULL) 
     printf("EMPTY LIST"); 
    else 
    { 
     curptr=headptr; 
     while(curptr!=NULL) 
     { 
      pos=pos +1; 
      printf("\n%d -%s \n",pos, curptr->pName); 
      curptro=curptr->act; 

      while(curptro!=NULL) 
      { 
       printf(" #%s --> %d", curptro->aName, curptro->reps); 
       curptro=curptro->ptrnexto; 
      } 
      curptr=curptr->ptrnext; 
     } 
    } 
} 

fitnessCookie.c fitnessCpCookie 創建者Asyraf扶余於23/04/2017。 版權fitnessCookie©2017。版權所有。

這裏是顯示輸出後的輸出從菜單中運行 display output

聲明:我沒有這最後用紅色下劃線的數據輸入。

如何做一個輸入一個笑臉反正..

+0

通過使用調試器來定位*發生崩潰的位置。然後檢查未初始化的變量或結構成員。但最重要的是,不要一次性編寫整個程序。迭代,寫小部分,逐步測試它在每個步驟之間的作用。我也建議你閱讀[如何調試小程序](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)。 –

+0

Iam使用的代碼塊內置了一些調試插件。它已經重寫了代碼並改變了其結構的一週,我認爲它是一個回合時間,我試圖從專業人士那裏尋求幫助。 theres更多的代碼,但我確定崩潰發生在顯示功能,當我嘗試顯示我的活動結構,我懷疑我的插入或顯示可能是錯誤的,但我把主菜單功能的情況下被忽視。 –

+0

使用uninitialize變量。 'ch' at'while(ch!= 0)','newptr-> act' at'if(newptr-> act == NULL)' – BLUEPIXY

回答

0

答案被BLUEPIXY在評論給出:
設置NULLnewptr->act

+0

「我不知道如何標記我的問題爲回答提醒我,因爲我無法在2天內回答我自己的問題」,這並不重要。 – Stargateur

+0

問題在他們有答案時會被視爲「已回答」,您現在擁有答案。沒有直接做這件事的機制。 – Yunnosch