hello friends,my program first worked correctly but didn't worked when i run it for second time.when i debuged it by dev c++ this message appeared:"an access violation (segmentation fault) raised in your program".what can i do?
#include<iostream>
usingnamespace std;
int main()
{
/*************************declaring database structure having special members*/
struct database{
char pilot_name[50];
char plane_name[50];
char offset[50];
char destination[50];
char cus_sure[100][50];
char cus_last[100][50];
int capacity;
int fly_number;
int reserved;
int year;
char month[15];
int day;
int houre;
char meridian[2];
int minute;
};
/********************************************///declaring fly structure in user_chice numbers
cout<<"how many fly do you want to record?\n";
int rec_num;//declars number of flies user want to record
cin>>rec_num;
database fly[rec_num];
/*******************************************/
/*************recieving fly information and save them in fly array database structure**/
for(int i=0;i<rec_num;i++) {
cout<<"enter this fly number:\n";
cin>>fly[i].fly_number;
cout<<"enter this fly's pilot_name:\n";
cin>>fly[i].pilot_name;
cout<<"enter this fly's plane_name:\n";
cin>>fly[i].plane_name;
cout<<"where is fly's offset?:\n";
cin>>fly[i].offset;
cout<<"where is fly's destination?:\n";
cin>>fly[i].destination;
cout<<"how many sits are availabe in this fly?:\n";
cin>>fly[i].capacity;
cout<<"enter fly time in order year,month(string),day,houre,minute:\n";
cin>>fly[i].year;
cin>>fly[i].month;
cin>>fly[i].day;
cin>>fly[i].houre;
cout<<"pm or am?\n";
cin>>fly[i].meridian;
cin>>fly[i].minute;
cout<<"DO YOU WANT TO RECORD ANOTHER FLY? Y/N?\n";
char ans;
cin>>ans;
if(ans == 'y')
continue;
if(ans == 'n')
break;}
/****************************************************************************************/
//REPORTING RECORDED FLIES PHASE:
for(int i=0;i<rec_num;i++) {
cout<<"FLY NUMBER : "<<fly[i].fly_number<<endl;
cout<<"PILOT : "<<fly[i].pilot_name<<endl;
cout<<"PLANE : "<<fly[i].plane_name<<endl;
cout<<"OFFSET : "<<fly[i].offset<<endl;
cout<<"DESTINATION : "<<fly[i].destination<<endl;
cout<<"CAPACITY : "<<fly[i].capacity<<endl;
cout<<"FLY TIME : "<<fly[i].year<<" "<<fly[i].month
<<" "<<fly[i].day<<" "<<fly[i].houre<<":"<<fly[i].minute<<fly[i].meridian<<endl<<endl<<endl;
}
/****************************************************************************************/
//RESERVATION PHASE:
cout<<"DO YOU WANT TO RESERVE ANY TICKETS? Y/N?\n";
char ans1;//customer choice to reserve ticket or not!
cin>>ans1;
while(ans1 == 'y') {
cout<<"WHICH FLY DO YOU WANT TO RESERVE?\n";
int cus_choice;//fly number that customer chooses
cin>>cus_choice;
if(fly[cus_choice-1].capacity > 0) {
cout<<"YOUR SURE NAME?\n";
cin>>fly[cus_choice-1].cus_sure[fly[cus_choice-1].reserved];
cout<<"YOUR LAST NAME?\n";
cin>>fly[cus_choice-1].cus_last[fly[cus_choice-1].reserved];
cout<<"YOUR TICKET HAS BEEN RESERVED\n";
cout<<"you must go to ticket_office to receipt your ticket!\n";
fly[cus_choice-1].capacity -= 1;//decreasing fly capacity for 1 after reserving this fly
fly[cus_choice-1].reserved += 1;//increasing fly reserved number for 1 after reserving this fly
cout<<"DO YOU WANT TO RESERVE ANOTHER FLY? Y/N\n";
cin>>ans1;
if(ans1 == 'y')
continue;
if(ans1 == 'n')
break;
}
if(fly[cus_choice-1].capacity == 0) {
cout<<"SORRY!! THIS FLY IS FULL!\n";
cout<<"DO YOU WANT TO RESERVE ANOTHER FLY? Y/N\n";
cin>>ans1;
if(ans1 == 'y')
continue;
if(ans1 == 'n')
break;
}
}
/************************************************************************************************/
/*FINAL FLY REPORTING*/
for(int i=0;i<rec_num;i++) {
cout<<"FLY NUMBER : "<<fly[i].fly_number<<endl;
cout<<"PILOT : "<<fly[i].pilot_name<<endl;
cout<<"PLANE : "<<fly[i].plane_name<<endl;
cout<<"OFFSET : "<<fly[i].offset<<endl;
cout<<"DESTINATION : "<<fly[i].destination<<endl;
cout<<"REMAINED CAP : "<<fly[i].capacity<<endl;
cout<<"reserved num : "<<fly[i].reserved<<endl;
cout<<"FLY TIME : "<<fly[i].year<<" "<<fly[i].month
<<" "<<fly[i].day<<" "<<fly[i].houre<<":"<<fly[i].minute<<fly[i].meridian<<endl<<endl<<endl;
}
/*************************************************************************************************/
//WHO RESERVED WHICH FLY REPORTING PHASE!
cout<<"people who reseved ticket :\n";
for(int i=0;i<rec_num;i++) {
cout<<"fly number : "<<fly[i].fly_number<<endl<<endl;
for(int j=0;j<fly[i].reserved;j++) {
cout<<" "<<fly[i].cus_sure[j]<<" "<<fly[i].cus_last[j]<<endl<<endl;
}
}
/*************************************************************************************************/
return 0;}
int rec_num;//declars number of flies user want to record
cin>>rec_num;
database fly[rec_num];
This shouldn't compile. DevC++ is a terrible compiler that you should consider replacing with something that uses the actual C++ standard. In a proper compiler this would give an error because you can't define an array with a non-constant variable. You need to dynamically allocate the memory to make the array variable in length.
Replace those three lines with:
1 2 3
int rec_num;//declars number of flies user want to record
cin>>rec_num;
database* fly = new database[rec_num];
GCC has an extension enabled by default that allows VLAs (variable length arrays) in C++ code, however this is not actually legal in C++, so it is recommended to turn off that extension if you want your code to be portable. Consider using a std::vector instead.
If you're using Bloodshed DevC++, it is an outdated IDE which comes with an outdated version of a compiler when you download the two in a package. I would recommend upgrading. If you're using another fork of the DevC++ IDE which is updated regularly, you may disregard the recommendation.