2016-06-07 40 views
1

我想顯示鏈接列表中的席位的可用性,但我不知道chekcavailability()中的邏輯不起作用。如何檢查C代碼鏈接列表中的可用性席位

第一次輸入沒問題,第二次輸入時我輸入了相同的值,表示座位已經被拿走並返回到特定的功能。

但是當我再次輸入可用的不同值時,它會給我一個錯誤。

請幫助謝謝。

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

#define A 20 

struct passenger 
{ 
char name[20]; 
char booking_id[20]; 
char passno[20]; 
char seatno[20]; 
struct passenger *next; 
}*start,*curr; 



void datainput(), savefile(), loadfile(), checkavailability(), validationseat(); 

int main() 
{ 
void reserve_seat(), cancel_seat(), modify_seat(), display_layout(), exit(); 
int choice; 
start=curr=NULL; 


do 
{ 

    system("cls"); 
    printf("\n\n*************************************************"); 
    printf("\n\n********AIRLINE RESERVATION MENU*****************"); 
    printf("\n\n*************************************************"); 
    printf("\n\n\t\t* 1. Reserve seat  *"); 
    printf("\n\n\t\t* 2. Modify seat  *"); 
    printf("\n\n\t\t* 3. Cancel seat  *"); 
    printf("\n\n\t\t* 4. Display seat layout*"); 
    printf("\n\n\t\t* 5. Exit    *"); 
    printf("\n\n*************************************************"); 
    printf("\n\n\n\n\t\t Enter your choice: "); 
    scanf("%d",&choice);fflush(stdin); 
    switch (choice) 
    { 
    case 1: 
     reserve_seat(); 
     break; 

    case 2: 
     modify_seat(); 
     break; 

    case 3: 
     cancel_seat(); 
     break; 

    case 4: 
     display_layout(); 
     break; 
    case 5: 
     { 
     exit(); 
     break; 
     } 
    default: 
     printf("invalid choice!!, please try again"); 
    } 
    getch(); 

}while (choice != 5); 

} 

void datainput() 
{ 
printf("\n\t\t enter your booking ID: "); 
gets(curr->booking_id); fflush(stdin); 
printf("\n\t\t enter your seat number: "); 
gets(curr->seatno); fflush(stdin); 
printf("\n\t\t Enter Name: "); 
gets(curr->name); fflush(stdin); 
printf("\n\t\t Enter Passport Number: "); 
gets(curr->passno); fflush(stdin); 

} 

void reserve_seat() 
{ 
curr=start; 
checkavailability(); 
if(start==NULL) 
    { 
     start=curr=(struct passenger *)malloc(sizeof(struct passenger)); 
     datainput(); 
     curr->next=NULL; 
     printf("\n\t data has been recorded"); 
     return; 
    } 
    while(curr->next=NULL) 
     curr=curr->next; 
    curr->next=(struct passenger *)malloc(sizeof(struct passenger)); 
    curr=curr->next; 
    datainput(); 
    curr->next=NULL; 
    printf("\n\t data has been recorded"); 

void checkavailability() 
{ 
int i; 
char cmp3[20]; 
printf("select your seat(1-20)"); 
gets(cmp3);fflush(stdin); 
while(curr) 
{ 
    if (strcmp(curr->seatno, cmp3)==0) 
    { 
     printf("Seat has been taken\n"); 
     checkavailability(); 
    } 
    else 
    { 
     break; 
    } 
} 
printf("seat available"); 
return; 
} 
+0

花時間編輯您的帖子,然後發佈。好 。糾正Tom指出的所有錯誤。然後發帖 – 2016-06-07 09:21:31

回答

1

有許多錯誤與您的代碼,而最糟糕的是,他不能編譯(除非conio聲明沒有參數退出功能)。

首先,您需要激活一個體面的編譯器選項。至少-Wall -Wextra(取決於編譯器)。

主要功能如下:exit(); 退出函數需要一個參數。修復。

modify_seat(); 
cancel_seat(); 
display_layout(); 

這些功能沒有實現,所以你不應該打電話給他們。解決這個問題。

不要使用獲取!這是危險的和不安全的。 不要fflush(stdin)!這是未定義的行爲。

「A」的定義是無用的,並沒有真正的解釋。它應該是什麼? 「陣列」?

爲了您的安全,請儘量不要使用全局變量。 我建議定義一個結構並在main中聲明一個變量。

在 「reserve_seat」: 而(curr->未來= NULL)

你在這裏犯了一個錯誤:這是一段時間(!curr->未來= NULL) 分配NULL給curr->接下來,也許這就是爲什麼你的鏈表被打破的原因。

嗯,這對我來說是一個骯髒的代碼,因爲它嚴重缺乏嚴格的編碼。 例如,檢查你的函數調用是否失敗(如malloc)。 我建議你重寫代碼。

+0

抱歉我的不良行爲,實際上我並沒有發送完整的代碼,因爲它太長了,我只是把具體的問題發送到問題我反擊,基本上我只需要檢查可用性的邏輯,謝謝非常多的回答和建議 –

+0

是的,但是爲了我們,給我們一個MVC(最小,完整和可驗證)的例子,特別是因爲你使用全局變量!坦率地說,你的代碼就像用半自動手槍玩俄羅斯輪盤賭一樣。 –

+0

我應該用完整的代碼問同一個問題嗎?我應該在哪裏放置它? –