为什么我的C程序崩溃

时间:2017-03-03 14:35:57

标签: c

我正在使用" dev cpp"我在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);
}

1 个答案:

答案 0 :(得分:0)

阅读编译器消息:

问题在于:

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

格式说明符为%s,但b.fee为浮点数。

你需要这个:

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

可能会出现更多问题。自己检查一下。每个scanf格式说明符必须与变量匹配。