Hi, there, I have written a program here which shows plane to book a seat, and enter flight info(this part works fine)... but having some small problem here I am using gotoxy and it gives me an error, it says that gotoxy undeclared. Could anyone help me with this please, correct the code or tell me how to do it. I am using Dev C++. Thanks in advance.
heres the code for it:
#include <iostream>
#include <cstring>
#include <string>
#include <iomanip>
#include <stdlib.h>
#include <conio.h>
//#include <stdio.h>
//#include "cursor.h"
#include <windows.h>
using namespace std;
struct FlightDetails
{
string ID;
string departure;
string destination;
string airlineName;
//double adultPrice = 200;
//double childPrice = 50;
//double totalPrice = 0;
};
typedef struct FlightDetails F;
struct Flight
{ F flightRecord;
struct Flight *link;
};
typedef struct Flight node;
//SHOW PLANE
const int MAX_PAX = 120;
const int ROWS = 30;
const int START_X = 9;
const int START_Y = 4;
char menu();
void showPlane();
void checkMouseAtSeat(char letters[], int rows[], int serials[], int& pax);
int getRefNumber(int serials[], int pax);
void populatePlane(char letters[],int rows[], int pax);
bool seatBooked (char letter, int ROW, char letters[], int rows[], int pax);
bool isUniqueSerial(int serial, int serials[], int pax);
void cancelBooking(int serials[], char letters[], int rows[], int& pax);
int cancelInput (char input[]);
void drawLine1();
void drawLine2();
void drawLine3();
void drawLine4();
//void gotoxy(int x, int y);
void printList (char letters[], int rows[], int serials[], int pax);
//SHOW PLANE END
node * createList(node * head, F flightRecord, node ** last);
node * createFlightNode(node * newNode, F flightRecord);
void displayList(node * head);
int main()
{
//plane
system ("CLS");
//system ("COLOR F1");
char letters[MAX_PAX];
int rows[MAX_PAX] = {0};
int serials[MAX_PAX] = {0};
int serial = 0;
int pax = 0;
char choice;
//plane
//char choice;
node *head=NULL;
node *last=head; //initial value//&last is a double pointer
F flightRec;
do
{
//main menu
//system("CLS");
cout << "\n\n\n\n\n\n";
cout << setw(40) << " Menu \n\n";
cout << setw(40) << " 1:" << " Enter Flight Details:\n " << endl;
cout << setw(40) << " 2:" << " Display Flight Details:\n " << endl;
cout << setw(40) << " 3:" << " Book a Seat:\n " << endl;
cout << setw(40) << " 4:" << " Exit\n " << endl;
cin >> choice;
cin.get();
//start of switch statement
switch(choice)
{
case '1' : system("CLS"); //ENTER FLIGHT DETAILS
{
///// FLIGHT /////
//FlightNode * createFlightList(FlightNode * head);
node * createFlightNode(node * newNode, F flightRecord);
{
cout << "Enter Flight ID: " << " ";
cin >> flightRec.ID;
cout << endl;
while( flightRec.ID != "!")
{
//cout << "Enter Flight ID:";
//cin >> flightRec.ID;
//cout << endl;
cout << "Enter Departure Airport: " << " ";
cin >> flightRec.departure;
cout << endl;
cout << "Enter Destination Airport: " << " ";
cin >> flightRec.destination;
cout << endl;
cout << "Enter Airline Title: " << " ";
cin >> flightRec.airlineName;
cout << endl;
system("CLS");
cout << "Enter - ! - to exit:";
cout << endl;
cout << " or " << endl;
cout << endl;
head=createList(head,flightRec,&last);
//this passes back the double pointer &last for the next position
cout << "Enter Flight ID: " << " ";
cin >> flightRec.ID;
system("CLS");
}//end while
}//end node
}//case A end
break;
case '2' : system("CLS");
{
displayList(head);
cout << endl;
system("pause");
system("CLS");
}//case B end
break;
case '3' : system("CLS");
{
char choice;
do
{
system ("CLS");
cout << "\n\n\n";
cout << setw(35) <<"" << "MENU:" << endl;
cout << setw(37) << right << "B : "; cout << "Book a seat\n";
cout << setw(37) << right << "C : "; cout << "Cancel booking\n";
cout << setw(37) << right << "E : "; cout << "Exit\n";
cout << setw(37) << right << "Enter choice : ";
cin >> choice;
choice = toupper(choice);
}
while ((choice != 'B') && (choice != 'C') && (choice != 'E'));
return choice;
do
{
choice = menu();
if (choice == 'B')
{
system ("CLS");
showPlane();
populatePlane(letters, rows, pax);
checkMouseAtSeat(letters, rows, serials, pax);
cout << endl << setw(15) << "";
system("pause");
}
else if (choice == 'C')
{
system ("CLS");
showPlane();
populatePlane(letters, rows, pax);
cancelBooking(serials, letters, rows, pax);
system("pause");
}
else (choice == 'E');
{
return 0;
}
}//do end
while (choice != 'E');
}//case 3 end
}//end switch
}//do end
while (choice != '4');
}//int end
//function to create one node with details
node * createFlightNode(node * newNode, F flightRecord)
{
newNode = new node;
newNode-> flightRecord = flightRecord;
newNode->link = NULL;
return newNode;
}
//adds one record at a time to the list, double pointer last returned with new last value
node *createList(node * head, F flightRec, node ** last)
{
if( head == NULL)
{
head = createFlightNode(head, flightRec);
//the contents of the double pointer same as original last
//pointer
*last = head;
}
else
{
//(*last) refers to old last pointer
(*last)-> link = createFlightNode( (*last)->link, flightRec);
//*last refers to old last single pointer
*last = (*last)->link;
}
return head;
}
void displayList(node * head)
{
node * current;
//current now contains the address held of the 1-st node similar to head
current = head;
cout << "\n\n" <<endl;
if(current == NULL)
{
cout << "Empty List\n\n" << endl;
}
else
{
while(current!= NULL)
{
cout << "Flight ID :" << " "<< current->flightRecord.ID << endl;
cout << "Departure Airport :" << " "<< current->flightRecord.departure << endl;
cout << "Destination Airport :" << " "<< current->flightRecord.destination << endl;
cout << "Airline Name :" << " "<< current->flightRecord.airlineName << endl;
current = current->link;
cout << endl << endl;
}//end while
}//end else
}//end void
//plane
void drawLine1()
{
for(int i = 0; i < ROWS - 1; i++)
{
cout << (char)205 << (char) 203;
}
}
void drawLine2()
{
cout << (char)186;
for(int i = 0; i < ROWS; i++)
{
cout << ' ' << (char)186;
}
}
void drawLine3()
{
cout << (char)204;
for(int i = 0; i < ROWS - 1; i++)
{
cout << (char)205 << (char) 206;
}
cout << (char)205 << (char)185;
}
void drawLine4()
{
for(int i = 0; i < ROWS - 1; i++)
{
cout << (char)205 << (char)202;
}
}
void showPlane()
{
int start_x = START_X + 1;
int start_y = START_Y + 1;
gotoxy(start_x, start_y -1);
for(int i = 0; i < ROWS; i++)
{
cout << setw(2) << i + 1;
}
gotoxy(start_x, start_y);
cout << (char)201;
drawLine1();
cout << (char)205 << (char)187;
gotoxy(start_x - 1, start_y + 1);
cout << 'A';
drawLine2();
gotoxy(start_x, start_y + 2);
drawLine3();
gotoxy(start_x - 1, start_y + 3);
cout << 'B';
drawLine2();
gotoxy(start_x, start_y + 4);
cout << (char)204;
drawLine4();
cout << (char)205 << (char)185;
gotoxy(start_x, start_y + 5);
cout << (char)186;
for(int i = 0; i < ROWS-1; i++)
{
cout << " ";
}
cout << ' ' << (char)186;
gotoxy(start_x, start_y + 6);
cout << (char)204;
drawLine1();
cout << (char)205 << (char)185;
gotoxy(start_x - 1, start_y + 7);
cout << 'C';
drawLine2();
gotoxy(start_x, start_y + 8);
drawLine3();
gotoxy(start_x - 1, start_y + 9);
cout << 'D';
drawLine2();
gotoxy(start_x, start_y + 10);
cout << (char)200;
drawLine4();
cout << (char)205 << (char)188;
cout << endl<<endl;
}
void checkMouseAtSeat(char letters[],int rows[], int serials[], int& pax)
{
bool mouseAtSeat;
char letter;
int mouseX, mouseY, ROW;
do
{
mouseAtSeat = true;
//where_mouse (mouseX, mouseY);
if (mouseX < START_X + 2 || mouseX > START_X + ROWS * 2) mouseAtSeat = false;
if (mouseY < START_Y + 2 || mouseY == START_Y + 6) mouseAtSeat = false;
if (mouseY > 15) mouseAtSeat = false;
if ((mouseY - START_Y)%2 != 0 || mouseY == START_Y + 6) mouseAtSeat = false;
ROW = (mouseX - START_X);
if (ROW % 2 != 0) mouseAtSeat = false;
if (mouseAtSeat)
{
ROW = ROW/2;
const int a = START_Y + 2;
const int b = START_Y + 4;
const int c = START_Y + 8;
const int d = START_Y + 10;
switch (mouseY)
{
case a: letter = 'A'; break;
case b: letter = 'B'; break;
case c: letter = 'C'; break;
case d: letter = 'D'; break;
} // close switch
mouseAtSeat = seatBooked (letter, ROW, letters, rows, pax);
}
} while (!mouseAtSeat);
gotoxy(mouseX, mouseY);
cout <<(char)219;
letters[pax] = letter;
rows[pax] = ROW;
gotoxy(0, 18);
cout << setw(25)<< "Seat: " << letters[pax] << rows[pax] << endl;
serials[pax] = getRefNumber(serials, pax);
pax++;
}
int getRefNumber(int serials[], int pax)
{
int serial = 0;
bool isUniqueRefNumber;
do
{
srand((int)time(0));
int span = 10000;
int start = 10000;
serial = rand() % span + start;
isUniqueRefNumber = isUniqueSerial(serial, serials, pax);
} while (!isUniqueRefNumber);
cout << setw(25)<< "Ref. Number: " << serial << endl << endl;
return serial;
}
void populatePlane(char letters[],int rows[], int pax)
{
int x = 0, y = 0;
for(int i=0; i<pax; i++)
{
int x = 0, y = 0;
x = START_X + rows[i]*2;
if (letters[i] == 'A') y = START_Y + 2;
if (letters[i] == 'B') y = START_Y + 4;
if (letters[i] == 'C') y = START_Y + 8;
if (letters[i] == 'D') y = START_Y + 10;
if (pax != 0)
{
gotoxy(x,y);
cout << (char)219;
}
}
}
bool seatBooked (char letter, int ROW, char letters[], int rows[], int pax)
{
bool mouseAtSeat = true;
for (int i=0; i<pax; i++)
{
if ((letters[i] == letter) && (rows[i] == ROW)) mouseAtSeat = false;
}
return mouseAtSeat;
}
bool isUniqueSerial(int serial, int serials[], int pax)
{
bool isUniqueRefNumber = true;
for (int i=0; i<pax; i++)
{
if (serials[i] == serial) isUniqueRefNumber = false;
}
return isUniqueRefNumber;
}
void cancelBooking(int serials[], char letters[], int rows[], int& pax)
{
char input[5];
int refNumber = 0;
bool itemFound = false;
int i = 0;
int z = 0;
gotoxy(0, 18);
cout << setw(15) << ""<< "Canceling. To exit enter 0 below.";
cout << endl;
cout << setw(15)<< "" << "Please provide your 5 digit Ref. Number: ";
do
{
refNumber = cancelInput(input);
if (refNumber)
{
for (i=0; i<pax; i++)
{
if (refNumber == serials[i])
{
itemFound = true;
cout << endl << setw(15) << "" << "Booking for seat " <<
letters[i] << rows[i] << " canceled.\n\n";
for (z=i; z<pax-1; z++)
{
serials[z] = serials[z+1];
letters[z] = letters[z+1];
rows[z] = rows[z+1];
}
pax--;
showPlane();
populatePlane(letters, rows, pax);
gotoxy(0,21);
cout << endl << setw(15) << ""; system("pause");
input[0] = '0';
}
}
}
if (!itemFound && refNumber)
{
cout << endl << setw(15) << "" << "Number " <<
refNumber << " not found!\n\n";
cout << endl << setw(15) << ""; system("pause");
break;
}
} while (input[0] != '0');
}
int cancelInput(char input[])
{
int refNumber = 0;
bool itemOk = false;
do
{
cin >> input;
refNumber = atoi (input);
if (atoi (input)) itemOk = true;
else itemOk = false;
if (input[0] == '0') itemOk = true;
} while (!itemOk);
return refNumber;
}
void printList (char letters[], int rows[], int serials[], int pax)
{
cout << "Seat" << " " << "Ref." << endl;
for (int i=0; i<pax; i++)
{
cout << letters[i] << rows[i] << " " << serials[i] << endl;
}
cout << pax << " passengers\n\n";
}
/*void gotoxy(int x, int y)
{
HANDLE hConsoleOutput;
COORD dwCursorPosition;
cout.flush();
dwCursorPosition.X = x;
dwCursorPosition.Y = y;
hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hConsoleOutput,dwCursorPosition);
} */