this program ask the user which movie they like and reserves a number of seats desired by the user ok i have this code working -but i have few more questions guys, -i wanted to put error trapping in the seatRes() function but have no idea how, -i also wanted to make a function that will be able to cancel a reserve seat, and change it to another seat -i also want to put error trapping when choosing a seat that has already been taken i read some topics here in the site but i got lost and dont fully understand the pointers and references they also gave me an idea about the boolean thing, but i cant understand how will i make it work can anyone guide me please? Code: #include <iostream> #include <conio.h> using namespace std; int movieChoice; int seatNum; int seatPlan [5][10]={0}; int rows,cols; char space; void seatDisplay( int a[][10]); void seatNew( int n[][10]); void seatDisplay( int a[][10]){ //this function shows the seat plan for (int i=0;i<5;i++){ for (int j=0;j<10;j++){ cout<<a[i][j]; cout<<" "; } cout<<endl; } } void seatNew( int n[][10]){ // this function shows the reserved seats for (int i=0;i<5;i++){ for (int j=0;j<10;j++){ cout<<n[i][j]; cout<<" "; } cout<<endl; } } void seatRes(int x){ //this function reserves the seats while (x>50 || x<=0){ cout<<"Cannot process Reservation."<<endl; cout<<"Please Enter number of seats to be reserved: "; cin>>x; } cout<<endl; cout<<"Please enter seat row and column you wish to reserve: \n"; cout<<endl; for (int i=1;i<=x;i++){ cout<<"(row,column): "; cin>>rows>>space>>cols; seatPlan[rows][cols]=1; } } int priceComp(int y){ //this function computes and displays the ticket price int price; price=y*150; cout<<endl; cout<<"You have reserved "<<y<<" seat(s)."; cout<<endl; cout<<endl; cout<<"Ticket Price: "<<price<<endl; cout<<endl; return 0; } int main(){ //this is the main function cout<<"Welcome to Movie World"<<endl; cout<<endl; cout<<"1 - Movie 1"<<endl; cout<<"2 - Movie 2"<<endl; cout<<"3 - Movie 3"<<endl; cout<<endl; cout<<endl; cout<<"Enter your choice of Movie: "; cin>>movieChoice; cout<<endl; switch (movieChoice){ case 1: cout<<endl; cout<<"Movie 1"<<endl; cout<<endl; cout<<"Seating Reservation"<<endl; cout<<endl; seatDisplay(seatPlan); cout<<"How many seats would you like to reserve?: "; cin>>seatNum; seatRes(seatNum); cout<<"Seating Reservation"; cout<<endl; cout<<"1 marks your reserved seat \n"; cout<<endl; cout<<"------Movie 1 SEAT PLAN----------- \n"; cout<<endl; seatNew(seatPlan); priceComp(seatNum); break; case 2: cout<<endl; cout<<"Movie 2"<<endl; cout<<endl; cout<<"Seating Reservation"<<endl; cout<<endl; seatDisplay(seatPlan); cout<<"How many seats would you like to reserve?: "; cin>>seatNum; seatRes(seatNum); cout<<"Seating Reservation"; cout<<endl; cout<<"1 marks your reserved seat \n"; cout<<"------Movie 2 SEAT PLAN----------- \n"; cout<<endl; seatNew(seatPlan); priceComp(seatNum); break; case 3: cout<<endl; cout<<"Movie 3"<<endl; cout<<endl; cout<<"Seating Reservation"<<endl; cout<<endl; seatDisplay(seatPlan); cout<<"How many seats would you like to reserve?: "; cin>>seatNum; seatRes(seatNum); cout<<"Seating Reservation"; cout<<endl; cout<<"1 marks your reserved seat \n"; cout<<"------Movie 3 SEAT PLAN----------- \n"; cout<<endl; seatNew(seatPlan); priceComp(seatNum); break; default: cout<<endl; cout<<"Invalid Input"<<endl; } return 0; _getch(); }
i would use a boolean to do error checking. booleans only have 2 values 0 or 1, for an empty seat or an occupied seat. loop through and test if the seats are empty, if they are assign someone there if not move on to the next seat until you either find and empty one or fill all the seats. if you fill all the seats i would add a message saying all seats are full you have to wait to the next showing of the movie or something like that. i have rearranged your function adding test conditions and some pseudocode to get you on the right track to testing for the empty seats. Code: void seatRes(int x){ //this function reserves the seats bool isTaken = 0; while (x>50 || x<=0){ cout<<"Please Enter number of seats to be reserved: "; if (isTaken = 0) { cout<< "seat is available."<< endl; cin>>x; cout<<"Please enter seat row and column you wish to reserve: \n"; cout<<endl; for (int i=1;i<=x;i++){ cout<<"(row,column): "; cin>>rows>>space>>cols; seatPlan[rows][cols]=1; } } else { cout << "Seat is taken. Pick another seat. cout<<"Please enter seat row and column you wish to reserve: \n"; cout<<endl; cin>>x; for (int q = 0; q > x; q++) { /* you want to fill all the seats so you make q greater than x have q increment +1 and if empty assign person in available seat. to do this make x = q. */ // put rest of program logic here //loop through and test all the seats to see if it is empty. if it is assign person to that seat.