Circular Queue passing product problem

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2008
Posts: 34
Reputation: Seamus McCarthy is an unknown quantity at this point 
Solved Threads: 0
Seamus McCarthy Seamus McCarthy is offline Offline
Light Poster

Circular Queue passing product problem

 
0
  #1
May 11th, 2009
Hey, i set up a circular queue to sort and pass ints easy, but having problems passing struct of information to the queue,
wat declaration should i pass to insert function??
Here is my code so far, what would i change the int item to in insert function??
have just included main, insert and remove functions

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. void Queue_init(struct queue *the_Queue, int requiredSize);
  5. int insert(struct queue * the_Queue, int item);
  6. int Remove(struct queue* the_Queue ,int & item);
  7. int peekFront(struct queue *the_Queue ,int & item);
  8. int isEmpty(struct queue *the_Queue);
  9. int isFull(struct queue *the_Queue);
  10. void displayDetail(struct queue *the_Queue);
  11.  
  12. #define SIZE 50;//max size of any given stack
  13. const int array_size = 6;
  14. struct product
  15. {
  16. int prodId;
  17. string prodName;
  18.  
  19. };
  20. typedef struct product pArray[array_size];
  21.  
  22. struct queue
  23. {
  24. int Q_Array[50];
  25. int front;
  26. int rear;
  27. int numItems;
  28. int maxSize;
  29. };
  30.  
  31. //-----------------------------------------------------------
  32.  
  33. void main()
  34. {
  35. struct queue aQueue;
  36. int reqSize, success, item;
  37. cout << "Set up queue to hold up to 6 products" << endl;
  38. cout<< "Enter required Size of Queue"<<endl;
  39. cin >> reqSize;
  40.  
  41. //enter struct detail
  42. for(int i=0;i<array_size;i++)
  43. {
  44. cout << "enter product name" << endl;
  45. getline(cin,pArray[i].prodName);
  46. cout << endl;
  47.  
  48. cout << "enter prouduct id" << endl;
  49. cin >> pArray[i].prodId;
  50. cin.ignore();
  51.  
  52. }
  53.  
  54. Queue_init(&aQueue, reqSize);
  55.  
  56. cout << "Queue is intialised " << endl;
  57. success = insert(&aQueue, pArray[0] );
  58. success = insert(&aQueue, pArray[1] );
  59. success = insert(&aQueue, pArray[2] );
  60. success = insert(&aQueue, pArray[3] );
  61. cout << "1 to 4 is inserted to queue" << endl;
  62.  
  63. displayDetail(&aQueue);
  64.  
  65.  
  66.  
  67. success = Remove(&aQueue, item);
  68. cout << "Item is:"<< item << endl;
  69. success = Remove(&aQueue, item);
  70. cout << "Item is:"<< item << endl;
  71. cout << "1 and 2 are removed from queue"<< endl;
  72.  
  73.  
  74. }
  75.  
  76. //-----------------------------------------------------------
  77.  
  78. void Queue_init(struct queue *the_Queue, int requiredSize)
  79. {
  80. the_Queue->front= 0; //the stack is empty
  81. the_Queue->rear = -1;
  82. the_Queue->numItems = 0;
  83. the_Queue->maxSize = requiredSize;
  84.  
  85. }
  86.  
  87. //-----------------------------------------------------------
  88.  
  89.  
  90. int insert(struct queue * the_Queue, int item)// struct P * item)
  91. {
  92. if (the_Queue->numItems ==the_Queue->maxSize)
  93. {
  94. // put item at rear of queue
  95.  
  96. return 0;
  97. }
  98. else
  99. {
  100. if(the_Queue->rear == the_Queue->maxSize-1)
  101. // deal with wrap around
  102. the_Queue->rear = -1;
  103.  
  104. // increment rear and insert
  105. the_Queue->Q_Array[++the_Queue->rear] = item;
  106. the_Queue->numItems++; // one more item
  107.  
  108. return 1;
  109.  
  110. }
  111. }
  112.  
  113. //-----------------------------------------------------------
  114.  
  115. int Remove(struct queue * the_Queue , int &item)//struct P &item)
  116. {
  117. // int i ;
  118. // get value and incr front
  119.  
  120. if (the_Queue->numItems !=0)
  121. {
  122. item = the_Queue->Q_Array[the_Queue->front];
  123.  
  124. if(the_Queue->front == the_Queue->maxSize-1)
  125. the_Queue->front =-1;
  126.  
  127. the_Queue->front++;
  128. the_Queue->numItems--; // one less item
  129.  
  130. return 1;
  131. }
  132. else
  133. return 0;
  134. }
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 53
Reputation: nexocentric is an unknown quantity at this point 
Solved Threads: 4
nexocentric nexocentric is offline Offline
Junior Poster in Training

Re: Circular Queue passing product problem

 
0
  #2
May 11th, 2009
First things first, you might want to clean things up by moving your struct and function declarations to a header file.

Next if you change the function prototype
  1. int insert(struct queue * the_Queue, int item)
to
  1. int insert(struct queue * the_Queue, struct product * item)

the function declaration from
  1. int insert(struct queue * the_Queue, struct product * item)// struct P * item)
to
  1. int insert(struct queue * the_Queue, struct product * item)

and
  1. success = insert(&aQueue, pArray[0] );
  2. success = insert(&aQueue, pArray[1] );
  3. success = insert(&aQueue, pArray[2] );
  4. success = insert(&aQueue, pArray[3] );
to
  1. success = insert(&aQueue, &pArray[0] );
  2. success = insert(&aQueue, &pArray[1] );
  3. success = insert(&aQueue, &pArray[2] );
  4. success = insert(&aQueue, &pArray[3] );

and move all struct declarations above your function prototypes you should be able to compile your program for testing.

If you still need help after that I should be here for a while.
Last edited by nexocentric; May 11th, 2009 at 9:03 am. Reason: Forgot some things
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 34
Reputation: Seamus McCarthy is an unknown quantity at this point 
Solved Threads: 0
Seamus McCarthy Seamus McCarthy is offline Offline
Light Poster

Re: Circular Queue passing product problem

 
0
  #3
May 11th, 2009
the_Queue->Q_Array[++the_Queue->rear] = item;
it say illegal conversion from int to product * for this line

and expected (
getline(cin,pArray[i].prodName); for this line its highlighting the [i]

any ideas why?
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 53
Reputation: nexocentric is an unknown quantity at this point 
Solved Threads: 4
nexocentric nexocentric is offline Offline
Junior Poster in Training

Re: Circular Queue passing product problem

 
0
  #4
May 11th, 2009
the_Queue->Q_Array[++the_Queue->rear] = item;

item ( is a struct )
the_Queue->Q_Array[++the_Queue->rear] ( is an int )

so unless you did

the_Queue->Q_Array[++the_Queue->rear] = item->prodId;

you wouldn't be able to set the value of the_Queue->Q_Array[++the_Queue->rear]
Last edited by nexocentric; May 11th, 2009 at 9:33 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 34
Reputation: Seamus McCarthy is an unknown quantity at this point 
Solved Threads: 0
Seamus McCarthy Seamus McCarthy is offline Offline
Light Poster

Re: Circular Queue passing product problem

 
0
  #5
May 11th, 2009
Thanks fixed it up it compiling now only for the ( error, i'm using the right syntax for entering and displaying struct
cin >> pArray[i].prodId;
success = insert(&aQueue, &pArray[0] );
but still saying [ should be (
Reply With Quote Quick reply to this message  
Join Date: Mar 2009
Posts: 53
Reputation: nexocentric is an unknown quantity at this point 
Solved Threads: 4
nexocentric nexocentric is offline Offline
Junior Poster in Training

Re: Circular Queue passing product problem

 
1
  #6
May 11th, 2009
That probably means that you made a mistake somewhere else in your program. The best way to find out where is to comment out the whole thing and then slowly uncomment each line until you find where the error is. Go ahead and give that a try.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 34
Reputation: Seamus McCarthy is an unknown quantity at this point 
Solved Threads: 0
Seamus McCarthy Seamus McCarthy is offline Offline
Light Poster

Re: Circular Queue passing product problem

 
1
  #7
May 11th, 2009
Ok will do, thanks for all your help!!
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC