May 3, 2014 at 8:07am UTC
how do i expand the 2D array without loosing its previous data..??
my 2d expands but when i display its data the previous data is re-initialized.. why and how..in the output it can be seen tht when i increase rows and colums and book new seats it re-intialize it n forgets the previous data.. :/ i cant get the problem hope there is some1 who can point my mistake thnx
[#include "flights_info.h"
#include<cmath>
#include <ctime>
#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<string.h>
using namespace std;
//int seats[4][5];
int **seats ;
char name[15];
int bookingId = 100;
int curRow = 0, curCol = 0;
int rows;
int cols;
flights_info::flights_info(void)
{
Flight_No = 0;
date[10] = '\0';
time[10] = '\0';
Gate = 0;
}
void flights_info::dynamic()
{
cout << "Enter rows:\n";
cin >> rows;
cout << "Enter columns:\n";
cin >> cols;
seats = new int*[rows];
for (int i = 0; i < rows; i++)
seats[i] = new int[cols];
// initialization
for (int j = 0; j < cols; j++)
for (int i = 0; i < rows; i++)
seats[i][j] = 0;
}
void flights_info::displaySeatOccupancy()
{
cout << "\t SEATS OCCUPIED \n\n";
for (int row = 0; row<rows; row++)
{
cout << row + 1;
for (int col = 0; col<cols; col++){
cout << "\t" << seats[row][col];
}
cout << "\n";
}
}
bool flights_info::isAvaliable(int numOfSeats)
{
int ans = (rows - curRow) * cols;
if (ans > numOfSeats)
return true;
else
return false;
}
void flights_info::showBookingMessage(){
_strdate_s(date);
_strtime_s(time);
Gate = 4;
Flight_No = 1234;
cout << "\n\t Flight Information\n\t\nName ||FlightNo|| Date || Time ||Gate\t\n";
cout << "\n" << name << "||" << Flight_No << " ||" << date << " ||" << time << " ||" << Gate << "\n";
}
void flights_info::booking()
{
fflush(stdin);
cout << "Enter ur Name:\n";
cin.getline(name, 15);
int numOfSeats;
cout << "\tHow many seats you want to reserve?\n";
cin >> numOfSeats;
bool avail = isAvaliable(numOfSeats);
if (avail == true)
{
for (int i = 1; i <= numOfSeats; i++)
{
seats[curRow][curCol] = bookingId++;
curCol++;
if (curCol>rows)
{
curCol = 0;
curRow++;
}
}
bookingId++;
showBookingMessage();
}
else
cout << "\n\tNot enough seats available!”, “The flight is fully booked!\n";
}main.cpp
#include "flights_info.h"
#include<iostream>
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<time.h>
using namespace std;
void main()
{
flights_info f;
//f.initialize();
int option;
do {
cout << "\t****MENU****\n\n";
cout << "\t1. FLIGHT_INFO\n";
cout << "\t2. BOOKING\n";
cout << "\t3. Display Seat Layout & Occupancy\n";
cout << "\t4. Exit\n";
cout << "\t Please enter ur CHOICE(1,2,3,4):\t";
cin >> option;
cout << endl << endl;
switch (option)
{
case 1:
f.dynamic();
cout << endl << endl;
break;
case 2:
f.booking();
cout << endl << endl;
break;
case 3:
f.displaySeatOccupancy();
cout << endl << endl;
break;
case 4:
cout << "\tBye Bye have a good day!\n";
cout << endl << endl;
}
} while (option != 4);
system("pause");
getchar();
}
]
[output::
****MENU****
1. FLIGHT_INFO
2. BOOKING
3. Display Seat Layout & Occupancy
4. Exit
Please enter ur CHOICE(1,2,3,4): 1
Enter rows:
2
Enter columns:
2
****MENU****
1. FLIGHT_INFO
2. BOOKING
3. Display Seat Layout & Occupancy
4. Exit
Please enter ur CHOICE(1,2,3,4): 2
Enter ur Name:
sam
How many seats you want to reserve?
3
Flight Information
Name ||FlightNo|| Date || Time ||Gate
sam||1234 ||05/03/14 ||12:55:36 ||4
****MENU****
1. FLIGHT_INFO
2. BOOKING
3. Display Seat Layout & Occupancy
4. Exit
Please enter ur CHOICE(1,2,3,4): 1
Enter rows:
4
Enter columns:
5
****MENU****
1. FLIGHT_INFO
2. BOOKING
3. Display Seat Layout & Occupancy
4. Exit
Please enter ur CHOICE(1,2,3,4): 2
Enter ur Name:
sami
How many seats you want to reserve?
5
Flight Information
Name ||FlightNo|| Date || Time ||Gate
sami||1234 ||05/03/14 ||12:55:56 ||4
****MENU****
1. FLIGHT_INFO
2. BOOKING
3. Display Seat Layout & Occupancy
4. Exit
Please enter ur CHOICE(1,2,3,4): 3
SEATS OCCUPIED
1 0 0 0 0 0
2 104 105 106 107 108
3 0 0 0 0 0
4 0 0 0 0 0
****MENU****
1. FLIGHT_INFO
2. BOOKING
3. Display Seat Layout & Occupancy
4. Exit
Please enter ur CHOICE(1,2,3,4):
]
May 3, 2014 at 9:21am UTC
Create the new 2D array but hold on to the old 2D array. Then you can loop through the old 2D array and copy the data to the new 2D array. Then when you are finished copying the old data you can get rid of the old 2D array.
Last edited on May 3, 2014 at 9:22am UTC