943,571 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 713
  • C++ RSS
May 11th, 2009
0

Circular Queue passing product problem

Expand Post »
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

C++ Syntax (Toggle Plain Text)
  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. }
Similar Threads
Reputation Points: 11
Solved Threads: 0
Light Poster
Seamus McCarthy is offline Offline
34 posts
since Apr 2008
May 11th, 2009
0

Re: Circular Queue passing product problem

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
Reputation Points: 26
Solved Threads: 4
Junior Poster in Training
nexocentric is offline Offline
62 posts
since Mar 2009
May 11th, 2009
0

Re: Circular Queue passing product problem

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?
Reputation Points: 11
Solved Threads: 0
Light Poster
Seamus McCarthy is offline Offline
34 posts
since Apr 2008
May 11th, 2009
0

Re: Circular Queue passing product problem

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.
Reputation Points: 26
Solved Threads: 4
Junior Poster in Training
nexocentric is offline Offline
62 posts
since Mar 2009
May 11th, 2009
0

Re: Circular Queue passing product problem

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 (
Reputation Points: 11
Solved Threads: 0
Light Poster
Seamus McCarthy is offline Offline
34 posts
since Apr 2008
May 11th, 2009
1

Re: Circular Queue passing product problem

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.
Reputation Points: 26
Solved Threads: 4
Junior Poster in Training
nexocentric is offline Offline
62 posts
since Mar 2009
May 11th, 2009
1

Re: Circular Queue passing product problem

Ok will do, thanks for all your help!!
Reputation Points: 11
Solved Threads: 0
Light Poster
Seamus McCarthy is offline Offline
34 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: IPC
Next Thread in C++ Forum Timeline: Day Of Year Problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC