473,662 Members | 2,390 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with structure and dynamic array

how to make provision of a dynamic array say of size 5 with each slot
holding a structure of type passenger?

/*
* Labsheet Queues
Question 1
Implement a program that shows a queue of people lining at a bus-stop.
The first one in the queue is the first one to get on the bus.
Your program should display the following data :
Id
First Name
Last Name
DestinationAddr ess
The id is to be incremented automatically. By default, it can be set
to 0.
*
* IMPLEMENTATION USING AN ARRAY
*/
#include <iostream>
using namespace std;

struct passenger{
int id;
char first_name[15];
char last_name[30];
char dest_add[20];
};

typedef passenger QueueElementTyp e;

class Queue{
private:
QueueElementTyp e * elements;
int MaxQueue;
int Front;
int Rear;
int fullqueue();
int emptyqueue();
public:
Queue(int MS);
void Enq(QueueElemen tType apassenger);
QueueElementTyp e Deq();
void printQueue();
~Queue();
};

Queue::Queue(in t MS){
elements = new QueueElementTyp e;
MaxQueue = MS;
Front = MaxQueue -1;
Rear = MaxQueue -1;
}

Queue::~Queue() {
delete [] elements;
MaxQueue = 0;
Front = MaxQueue -1;
Rear = MaxQueue -1;
}

int Queue::fullqueu e(){
return(Front = (Rear + 1) % MaxQueue);
}

int Queue::emptyque ue(){
return (Front == Rear);}

void Queue::Enq(Queu eElementType apassenger){
if (fullqueue())
cout<<"Queue is Full";
else{
Rear = (Rear +1) % MaxQueue;
cout<<"Enter passenger first name:";
cin>apassenger. first_name;
cout<<endl<<"En ter passenger last name:";
cin>>apassenger .last_name;
cout<<endl<<"En ter destination address:";
cin>>apassenger .dest_add;
apassenger.id = 1;
elements[Rear] = apassenger;
}
}

QueueElementTyp e Queue::Deq(){
QueueElementTyp e mypass;
if (emptyqueue())
cout<<"Queue is empty";
else{
Front = (Front + 1) % MaxQueue;
mypass = elements[Front];
}
return(mypass);
}

void Queue::printQue ue(){
for(int i = Front ; i<= Rear; i++){
cout<<elements[i].id;
cout<<elements[i].first_name;
cout<<elements[i].last_name;
cout<<elements[i].dest_add;
}
}
int main(int argc, char** argv)
{
Queue myqueue(5);
QueueElementTyp e mypass;
myqueue.Enq(myp ass);
myqueue.printQu eue();

myqueue.Enq(myp ass);
myqueue.printQu eue();
return 0;
}
Jun 27 '08 #1
1 1787
On Wed, 07 May 2008 02:57:31 -0700, curiousEngine wrote:
how to make provision of a dynamic array say of size 5 with each slot
holding a structure of type passenger?

/*
* Labsheet Queues
Question 1
Implement a program that shows a queue of people lining at a bus-stop.
The first one in the queue is the first one to get on the bus. Your
program should display the following data : Id
First Name
Last Name
DestinationAddr ess
The id is to be incremented automatically. By default, it can be set to
0.
Does your homework assignment specifically state that you aren't allowed
to use std::queue? Otherwise, you're re-inventing a (*very* wonky) wheel.

[...]
Queue::Queue(in t MS){
elements = new QueueElementTyp e;
Here you allocate *one* element to 'elements'. I think you probably meant:

elements = new QueueElementTyp e[MS];
Queue::~Queue() {
delete [] elements;
Here you delete an *array* of elements. This is not going to work.
void Queue::Enq(Queu eElementType apassenger){
if (fullqueue())
cout<<"Queue is Full";
else{
Rear = (Rear +1) % MaxQueue;
cout<<"Enter passenger first name:";
cin>apassenger. first_name;
cout<<endl<<"En ter passenger last name:";
cin>>apassenger .last_name;
cout<<endl<<"En ter destination address:";
cin>>apassenger .dest_add;
apassenger.id = 1;
elements[Rear] = apassenger; <------
Since you don't allocate an array of elements this is going to access an
unallocated element. Ditto many other places in your code.

This is a really bad attempt at implementing a queue. Either use
std::queue (*way* easier) or, if you have to implement it yourself, read
a good book on data structures (and C++) first - it's tricky.

--
Lionel B
Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
6644
by: simon | last post by:
From my previous post... If I have a structure, struct sFileData { char*sSomeString1; char*sSomeString2; int iSomeNum1; int iSomeNum2;
11
2405
by: Mannequin* | last post by:
Hi all, I'm working on a quick program to bring the Bible into memory from a text file. Anyway, I have three questions to ask. First, is my implementation of malloc () correct in the program to follow? Second, have I correctly passed the structure's pointer to the functions in this program?
4
8006
by: David Scemama | last post by:
Hi, I'm trying to read a database file written from a turbo Pascal program. I've set a structure to map the records in the file, but I have problem reading the file when I use VBFixedArray in my structure instead of VBFixedString. Here is the original code that works: Structure CCat
2
4443
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c */ #include <time.h>
22
1903
by: sam_cit | last post by:
Hi Everyone, I have the following structure in my program struct sample { char *string; int string_len; };
6
3011
by: noone | last post by:
What is the syntax to access members of a structure without explicitly naming the structure in every access? struct mytype { int a; char* b; long c; } IT; How can I access the structure members in a way similar to the old pascal
2
3300
by: Opteron64 | last post by:
Hi, I'm trying to create and initialise a dynamic array within a nested structure. The structure is defined as followed: (C++ code) typedef unsigned char uchar; typedef unsigned int uint; struct Results_list {
1
3023
by: =?Utf-8?B?VGhvbWFzUg==?= | last post by:
Hi together, I have following little problem with a structure which I need for unmanaged code: Public Structure Info Public Header As HeaderInfo <System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst:=256)_ Public Notes As Integer()
43
2356
by: John | last post by:
Hi This .net is driving me crazy!! In VB6 I had a type which contained a couple of multi-dimentional arrays which i used to create and read records: Type AAA : Array1(10,10,2) as Integer
0
8856
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8633
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7365
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6185
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5653
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4179
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4347
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2762
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.