Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ > Re: Problem wit...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 2 Topic 45792 of 47006
Post > Topic >>

Re: Problem with structure and dynamic array

by Lionel B <me@[EMAIL PROTECTED] > May 7, 2008 at 10:58 AM

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
> DestinationAddress
> 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(int MS){
> 	elements = new QueueElementType;

Here you allocate *one* element to 'elements'. I think you probably meant:

elements = new QueueElementType[MS];

> Queue::~Queue(){
> 	delete [] elements;

Here you delete an *array* of elements. This is not going to work.

> void Queue::Enq(QueueElementType apassenger){ 
> 	if (fullqueue())
> 		cout<<"Queue is Full";
> 	else{
> 		Rear = (Rear +1) % MaxQueue;
> 		cout<<"Enter passenger first name:";
> 		cin>> apassenger.first_name;
> 		cout<<endl<<"Enter passenger last name:"; 
cin>>apassenger.last_name;
> 		cout<<endl<<"Enter 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
 




 2 Posts in Topic:
Problem with structure and dynamic array
curiousEngine <curious  2008-05-07 02:57:31 
Re: Problem with structure and dynamic array
Lionel B <me@[EMAIL PR  2008-05-07 10:58:33 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Thu Jul 24 2:05:41 CDT 2008.