Yes I know what the code is supposed to do, my diagrams have been
drawn out and everything its just a bit confusing to me how I can
match the seat number and row of the bool double array to the linked
list information a long with my errors. The full code is really needed
to show what the program actually does and is supposed to do. In the
"clearPlane" calling in main() it is saying that my calling of the
struct is changing its reference from SEAT *& to SEAT .
This is the code:
#include<iostream>
#include<string>
#include<cctype>
using namespace std;
struct SEAT {
// bool occupied; // initially, false
string first,last; // passenger name
int numBags; // max of 4
char mealType; // (r)egular, (v)egetaria, (o)ther
SEAT *next;
};
const int ROWS= 30;
const int COLS= 6;
int menuchoice();
void clearPlane(SEAT *&p , bool o[][COLS]);
void showPlane(bool o[][COLS]);
bool getSeat(SEAT *&p, int &row, int &col, bool o[][COLS]);
void getSeatFromUser(int &row, int &col, bool o[][COLS]);
void getInfo(SEAT *&s, int, int);
void showInfo(SEAT *&s, int, int);
void makeReservation(SEAT p, bool o[][COLS]);
void cancelReservation(SEAT p, bool o[][COLS]);
void viewReservations(SEAT p, bool o[][COLS]);
void orderedInsert(SEAT *&p, int &row, int &col);
int main()
{
int choice; // user's menu choice
SEAT plane; // reservation info for entire plane
bool occupied[ROWS][COLS];
clearPlane( plane, occupied);
showPlane( occupied);
do
{
cin.clear();
choice= menuchoice();
cin.ignore();
switch (choice) {
case 1: makeReservation(plane, occupied); break;
case 2: cancelReservation(plane, occupied); break;
case 3: viewReservations(plane, occupied); break;
case 4: break;
}
}while(choice !=4);
return 0;
}
/* menuchoice
**-----------------
** displays menu and get's user's choice
**--------------------------------------------------------------------
*/
int menuchoice()
{
int choice;
cout << endl << endl;
cout << "+------------------------+\n";
cout << "| Main Menu |\n";
cout << "+------------------------+\n";
cout << "| [1] Reserve a Seat |\n";
cout << "| [2] Cancel Reservation |\n";
cout << "| [3] List Reservations |\n";
cout << "| [4] Exit Program |\n";
cout << "+------------------------+\n";
do {
cout << "Choice: ";
cin >> choice;
if(!cin)
cin.clear();
} while (choice < 1 || choice > 4);
return choice;
}
/* showPlane
**-----------------
** displays grid showing open/taken seats
**--------------------------------------------------------------------
*/
void showPlane(bool o[][COLS] )
{
int i,j;
cout << " 1 2
3\n";
cout << " 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9
0\n";
for (j=0; j<COLS; j++) {
cout << (char) (j+65);
for (i=0; i<ROWS; i++)
if (o[j]==true)
cout << " x";
else
cout << " ";
cout << endl;
}
}
/* clearPlane
**-----------------
** empties all reservations in airplane
**--------------------------------------------------------------------
*/
void clearPlane(SEAT *&p, bool o[][COLS])
{
int i,j;
for (i=0; i<ROWS; i++)
for (j=0; j<COLS; j++)
o[j]= false;
}
/* getSeat
**-----------------
** obtains desired row and column number for a seat; the seat can be
** selected by the user or can be selected automatically by the
** computer; the function return true if an open seat was found;
** false otherwise.
**--------------------------------------------------------------------
*/
bool getSeat(SEAT *&p, int &row, int &col, bool o[][COLS])
{
bool allTaken; // true if all seats on plane are occupied
char method; // (a)utomatic or (m)anual seat selection
allTaken= true; // make sure at least
one
for (row=0; row<ROWS && allTaken; row++) // seat is available
for (col=0; col<COLS && allTaken; col++)
if (!o[row][col])
allTaken= false;
if (allTaken) return false; // if not return false
do {
cout << "Would you like to select a seat (a)utomatically or
(m)anually: ";
cin >> method;
} while (tolower(method)!='a' && tolower(method)!='m');
switch (tolower(method)) {
case 'a': for (row=0; row<ROWS; row++) // (a)utomatic seat
selection
for (col=0; col<COLS; col++)
if (!o[row][col])
return true;
break; // this break is redundant, we hope
case 'm': getSeatFromUser(row,col,o); // (m)anual seat
selection
return !o[row][col];
}
return false; // we should never get here
}
/* getSeatFromUser
**-----------------
** obtains desired row and column number for a seat; this function
** will be called from getSeat when requesting a seat for making
** reservations and will be called directly when selecting a
** seat for cancellation.
**--------------------------------------------------------------------
*/
void getSeatFromUser(int &row, int &col, bool o[][COLS])
{
char let;
showPlane (o);
do {
cout << "Enter row (1.." << ROWS << "): ";
cin >> row;
} while (row < 1 || row > ROWS);
do {
cout << "Enter col (A.." << (char) (COLS+64) << "): ";
cin >> let;
let= toupper(let);
} while (let < 'A' || let > (COLS+64));
col= let-65;
row--;
}
/* getInfo
* --------------------------------------------------------
* Accepts the SEAT struct then the user enters in their
* information.
* ------------------------------------------------------*/
void getInfo(SEAT *&s, int row, int col)
{
cin.ignore(100,'\n');
string answer;
do{
cout<<"\nEnter in this information:\n\n";
cout<<"Enter your first and last name: ";
cin>>s->first;
cin>>s->last;
cout<<"\n";
do{
cin.clear();
cout<<"How many bags will you be bringing?(limit of 4) ";
cin>>s->numBags;
cout<<"\n";
}while(s->numBags<0||s->numBags>4);
do{
cin.clear();
cout<<"What type of meal would you like?((r)egular, (v)egatarian,
or (o)ther) ";
cin>>s->mealType;
s->mealType=tolower(s->mealType);
cout<<"\n";
}while(s->mealType!='r' && s->mealType!='v' && s->mealType!= 'o');
showInfo(s, row, col);
getline(cin,answer);
do{
cout<<"Is this information correct?(yes,no)";
getline(cin,answer);
cout<<"\n";
}while(answer!="yes" && answer!="no");
cin.clear();
}while(answer=="no");
}
/*--------------------------------------------------------
* Displays the information they just entered.
* ------------------------------------------------------*/
void showInfo(SEAT *&s, int row, int col)
{
string regular="Regular";
string vegetarian="Vegetarian";
string other="Other";
cout<<"\nSeat: "<<row+1<<", "<<(char)(col+65)<<"\n";
cout<<"Name: "<<s->first<<" "<<s->last<<"\n";
cout<<"Number of bags: " <<s->numBags<<"\n";
if(s->mealType=='r')
cout<<"Meal Type: "<<regular<<"\n\n";
if(s->mealType=='v')
cout<<"Meal Type: "<<vegetarian<<"\n\n";
if(s->mealType=='o')
cout<<"Meal Type: "<<other<<"\n\n";
}
/*--------------------------------------------------------
* Calls showPlane and then will ask what seat they want
* and will call getInfo
* ------------------------------------------------------*/
void makeReservation(SEAT *&p, bool o[][COLS])
{
int row;
int col;
do{
cout<<"\n";
getSeat(p,row, col,o);
}while(o[row][col]==true);
o[row][col]=true;
orderedInsert(p,row,col);
//getInfo(p,row,col,o);
}
/*--------------------------------------------------------
* User enters in a seat row and column and if it is empty
* it will display that. If it is occupied then it will
* make the array empty.
* ------------------------------------------------------*/
void cancelReservation(SEAT *&p, bool o[][COLS])
{
int row;
int col;
getSeatFromUser(row, col, o);
if(o[row][col]==false)
cout<<"\nThe reservation is empty.";
else
o[row][col]=false;
}
/*--------------------------------------------------------
* For loop that will go through each seat and if it is
* occupied it will display the SEAT struct for that one
* then move on.
* ------------------------------------------------------*/
void viewReservations(SEAT *&p, bool o[][COLS])
{
int i;
int n;
for(i=0; i<30; i++)
for(n=0; n<6; n++)
if(o[n]==true)
{
showInfo(p, i, n);
}
}
void orderedInsert(SEAT *&p, int &row, int &col)
{
SEAT *nextSEAT= new SEAT;
getInfo(nextSEAT, row, col);
}