Hello Everyone,
Firstly, this site as been invaluable to me in recent weeks, so thankyou very much. My problem is that I have a question asked inside a For Loop, one input means breaking out of the loop (break) which works fine, the Alternative input means jumping to a different location within the loop.
Its a booking program for a small airline, with 1 plane, in which the first 5 seats are first class and 6-10 are economy. the question is after one section if full weather the user would like to try the other section, any help to take a positive input to that question and move the user to the appropriate part of the code would be much appreciated.
#include <iostream>
#include<iomanip>
usingnamespace std;
constint seats =9;//const int for array number
bool SeatingPlan[seats]; // declating boolean array
int choice;// varialbe for menu selection
int choice2;// variable for first class over flow
int choice3;// varialbe for Economy overflow
int count =0;// limits the number of recursions the program will go through
int f =1;//seat counter for First Class
int e =6;//seat counter for second class
int main()
{
for (int i =0; i<seats; i++)// simple for loop initialising all array elements to false
SeatingPlan[i] = false;
do
{ // do loop makes a recuring menu
count ++ ;
cout<< " Reservations" << endl;
cout <<"--------------"<<endl;
cout<<"Please enter 1 for First Class bookings"<< endl;
cout<<"Please enter 2 for Economy bookings"<< endl;
cin>> choice;
//First Class Section
if (choice ==1)
{
cout<<"Welcome to First Class booking"<<endl;
cout<<"------------------------------"<<endl;
if ( f > 5)// prevents seat 6 from being booked as first class
{
cout<< " Unfortunately there arent enough seats"
<< "left in First Class, press 1 try Economy"
<<" or any other key for more information"<< endl;
cin>> choice2 ;
if (choice2 != 1)
{ cout<<"The next flight leaves in 3 hours"<< endl;
break;
}
if (choice2 ==1)
{
// fill me with code!!
}
}
elseif (f <= 5)
{
SeatingPlan[f] = true;
cout<<" You Are booked in Seat "<< f<< " in First Class"<< endl;
f++;
}
}
//Economy Section
if (choice ==2)
{
cout<<"Welcome to Economy booking"<<endl;
cout<<"------------------------------"<<endl;
if ( e > 10)// shows that Economy section runs from seat 6 to 10
{
cout<< " Unfortunately there arent enough seats"
<< "left in Economy, Press 1 to try First Class"
<<"or any other key for more information"<< endl;
cin>> choice3 ;
if (choice3 != 1)
{ cout<<"The next flight leaves in 3 hours"<< endl;
break;
}
if (choice3 ==1)
{
// fill me with code!!
}
}
elseif (e <= 10)
{
SeatingPlan[e] = true;
cout<<" You Are booked in Seat "<< e<< " in Economy"<< endl;
e++;
}
}
}
while (count <= 10);
return 0;
}
You would just add to the seat count. So if f is first class and e is economy.. If they tried for economy and it was filled and they said to go for first class you would then add them into first class.
The ability to jump around code without using methods that most people try to avoid will require to start harnessing the true potential of C++ which is OOP.
#include <iostream>
#include<iomanip>
usingnamespace std;
constint seats =9;//const int for array number
bool SeatingPlan[seats]; // declating boolean array
int choice;// varialbe for menu selection
int choice2;// variable for first class over flow
int choice3;// varialbe for Economy overflow
int count =0;// limits the number of recursions the program will go through
int f =1;//seat counter for First Class
int e =6;//seat counter for second class
void function1()
{
cout<<"Welcome to First Class booking"<<endl;
cout<<"------------------------------"<<endl;
if ( f > 5)// prevents seat 6 from being booked as first class
{
cout<< " Unfortunately there arent enough seats"
<< "left in First Class, press 1 try Economy"
<<" or any other key for more information"<< endl;
cin>> choice2 ;
if (choice2 != 1)
{ cout<<"The next flight leaves in 3 hours"<< endl;
break;
}
if (choice2 ==1)
function2();
}
elseif (f <= 5)
{
SeatingPlan[f] = true;
cout<<" You Are booked in Seat "<< f<< " in First Class"<< endl;
f++;
}
}
void function2()
{
cout<<"Welcome to Economy booking"<<endl;
cout<<"------------------------------"<<endl;
if ( e > 10)// shows that Economy section runs from seat 6 to 10
{
cout<< " Unfortunately there arent enough seats"
<< "left in Economy, Press 1 to try First Class"
<<"or any other key for more information"<< endl;
cin>> choice3 ;
if (choice3 != 1)
{ cout<<"The next flight leaves in 3 hours"<< endl;
break;
}
if (choice3 ==1)
function1();
}
elseif (e <= 10)
{
SeatingPlan[e] = true;
cout<<" You Are booked in Seat "<< e<< " in Economy"<< endl;
e++;
}
}
}
int main()
{
for (int i =0; i<seats; i++)// simple for loop initialising all array elements to false
SeatingPlan[i] = false;
do
{ // do loop makes a recuring menu
count ++ ;
cout<< " Reservations" << endl;
cout <<"--------------"<<endl;
cout<<"Please enter 1 for First Class bookings"<< endl;
cout<<"Please enter 2 for Economy bookings"<< endl;
cin>> choice;
//First Class Section
if (choice ==1)
function1();
if(choice==2)
function2();
}
while (count <= 10);
return 0;
}
If you happen to continue reading, JewelCPP's code is more object oriented than your approach, and is definitely more flexible in it's options. If you like the sound of that, people take a look at some tutorials like http://www.cplusplus.com/doc/tutorial/functions/
Both of these Ideas worked perfectly, I'm going to go with the functions as i think its a better approach, Thank you very much you lovely people. Merry Christmas to you both