2017-03-03 161 views
-4

我使用「開發CPP」我就是在C爲什麼我的C程序崩潰

寫一些代碼,而我運行下面的代碼,在功能1,exe文件輸入所有數據後僅崩潰。

有沒有錯誤上映之前,我完成輸入數據的廣告按回車

這是怎麼回事?

// hotel system *work in progress*// 
#include<stdio.h> 
#include<stdlib.h> 

struct book 
{ 
    int bookno[20]; 
    char travellername[20]; 
    char destination[20]; 
    char hotelname[20]; 
    char checkin[20]; 
    char checkout[20]; 
    int guestno[20]; 
    char type[20]; 
    float fee; 

}b; 


void add();//Add new booking 
void all(); //view all booking 
//void mod(); modify booking 
//void search(); search booking 
//void del(); delete booking 

void main() 
{ 

int choose; 
do{ 
printf("\n  *** Welcome to Hong Kong Hotek booking Record and Management  System 2017 ***\n"); 
printf("\n  *** This system is developed by CCIT4020 Class No.NL-?? Group No.?? ***"); 
printf("\n\n\n--<Basic functions>-- \n"); 

printf("\n1. Add New Hotel Booking Record(s): \n"); 
printf("\n2. Display All Hotel Booking Records: \n"); 
printf("\n3. Modify Hotel Booking Record(s): \n"); 
printf("\n4. Search Hotel Booking Record(s): \n"); 
printf("\n5. Delete Hotel Booking Record(s): \n"); 
printf("\n0. Quit: \n"); 
printf("\nWhat is your option (0-5)? "); 
scanf("%d",&choose);  

switch (choose) 
{ 
    case 1 : 
     add(); 
    break; 
    case 2: 
     all(); 
    break; 

    //case 3: 
    // mod(); 
    //break; 

    //case 4: 
    // search(); 
    //break; 

    //case 5: 
    // del(); 
    //break; 

    case 0: 
     exit(0); 
    break; 


    default: 
     printf("Invalid choice! Please enter again!"); 
    break; 
} 
}while(choose!=0); 

} 

void add() 
{ 

FILE *fp; 
struct book b; 
printf("Hotel Booking number: "); 
scanf("%s",b.bookno); 

printf("Name of Traveller: "); 
scanf("%s",b.travellername); 

printf("Destination: ");  
scanf("%s",b.destination); 

printf("Name of Hotel: "); 
scanf("%s",b.hotelname); 

printf("Check-in Schedule: "); 
scanf("%s",b.checkin); 

printf("Check-out Schedule: "); 
scanf("%s",b.checkout); 

printf("Number of Guests: "); 
scanf("%s",b.guestno); 

printf("Room Type: "); 
scanf("%s",b.type); 

printf("Total Fee: "); 
scanf("%s",b.fee); 

fp=fopen("data.txt","a"); 

if(fp == NULL) 
{ 
    printf("There are no data file! please create one!"); 
} 
else 
{ 
    fprintf(fp,"%s \n %s \n %s \n %s \n %s \n %s \n %s \n %s \n %s",b.bookno,b.travellername,b.destination,b.hotelname,b.checkin,b.checkout,b.guestno,b.type,b.fee); 
    printf("One Record Added!"); 
} 
    printf("\n"); 
fclose(fp); 
} 

void all() 
{ 
char choose; 
FILE *fp; 

fp = fopen("data.txt","r"); 
if(fp == NULL) 
{ 
    printf("There are no data file!"); 
    exit(1); 

} 
else 
{ 
    system("clear"); 
    while((choose = fgetc(fp)) != EOF) 
     printf("%c",choose); 

} 
fclose(fp); 
} 
+2

....因爲它有一個錯誤。找到它。 –

+1

歡迎來到Stack Overflow!您介意創建[___MCVE___](http://stackoverflow.com/help/mcve)? –

+1

您的輸入是什麼? –

回答

0

閱讀你的編譯器消息:

的問題是在這裏:

scanf("%s",b.fee); 

格式說明是%s,但b.fee是一個浮動。

你需要這樣的:

scanf("%f", &b.fee); 

有可能更多的問題,這樣的一個。自己檢查一下。每個scanf格式說明符都必須與變量匹配。

+1

'scanf(「%s」,b.bookno);'是另一個。事實上,所有以字符串形式掃描的無字符串數據都是一個問題。甚至沒有提到這是一個整數...... –

+1

說,'費'變量是唯一沒有提供地址,所以崩潰必須來自那裏。該修復後代碼仍然是超級錯誤。 –