| | |
Queues. Help required in project
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Well apart from the formatting being a mess, what else would you like us to suggest?
A couple of things:
1. Your 'counter' functions should not be friends. Instead, provide a member function like isEmpty(), so you can do things like if ( q1.isEmpty() )
2. Using clock() means you're measuring processor time, not time according to the clock on the wall. Because this is only a simulation, you don't need to synchronise with the real world time.
So you could just provide a member function like
You'd call this method from main(), just like any other member function.
If later, you do need to sync with the real world, then main() can do that and all your queue logic remains unaffected.
3. An array of queues (not q1, q2, q3) may be easier to manage.
A couple of things:
1. Your 'counter' functions should not be friends. Instead, provide a member function like isEmpty(), so you can do things like if ( q1.isEmpty() )
2. Using clock() means you're measuring processor time, not time according to the clock on the wall. Because this is only a simulation, you don't need to synchronise with the real world time.
So you could just provide a member function like
C++ Syntax (Toggle Plain Text)
queue::tick ( void ) { myInternalClock++; // anything else time related, like dropping an expired entry off the queue }
If later, you do need to sync with the real world, then main() can do that and all your queue logic remains unaffected.
3. An array of queues (not q1, q2, q3) may be easier to manage.
•
•
Join Date: Mar 2008
Posts: 13
Reputation:
Solved Threads: 0
i cannot use an integer..then i wont be able to automatically queue out a person after a particular time...maybe i shud try out the function..and yes the teacher also hinted about using the to generate random time for each person to be queued out..i dunno how to work that.
C++ Syntax (Toggle Plain Text)
time();
C++ Syntax (Toggle Plain Text)
rand();
here i am, a bundle of past recollections, of present dreams, knotted up in a reasonably attractive bundle of flesh~ sylvia plath
•
•
Join Date: Mar 2008
Posts: 13
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<time> #include<stdlib> #include<stdio> using namespace std; struct node { node *next; long time_start,time_total,time_compare; }; class queue { private: node *head; node *tail; node n; long time_global; public: int count; queue(); void addnode(); void print(); void dequeue(); friend int choose_counter(queue&,queue&,queue&,queue&); friend int choose_counter1(queue&,queue&,queue&); }; queue::queue() { head=NULL; tail=NULL; count=0; time_global=time(NULL); } void queue::addnode() { if(head==NULL && tail==NULL) { node *person=new node; tail->next=NULL; time_compare=time(NULL)+rand()%10; count++; } else { node *person=new node; tail->next=person; tail=tail->next; tail->next=NULL: count++; } if (head->time_compare==time_global) {dequeue();} } void queue::print() { cout<<"STATUS OF QUEUES:"<<endl; } void queue::dequeue() { node *temp; temp=head; head=head->next; count--; if(count==0) {head=tail=NULL; } else { srand(time(NULL)); head->time_compare=time(NULL)+rand()%10; head->time_total=time_compare-time_start; } } int main() { queue q1,q2,q3,q4; char choice; cout<<"PLEASE CHOSE FROM THE FOLLOWING MENU:"<<endl<<endl<<"1. To enter a male customer, press 'M'."<<endl<<"2. To enter a female customer, press 'F'."<<endl<<"3. To exit press 'X'."<<endl; cin>>choice; switch(choice) { case 'M': {int c=choose_counter(q1,q2,q3,q4); if(c==0) {q1.addnode();} else if(c==1) {q2.addnode();} else if(c==2) {q3.addnode();} else if(c==3) {q4.addnode();} else { int d=choose_counter1(q1,q2,q3); if (d=1) {q1.addnode();} else if(d=2) {q2.addnode();} else {q3.addnode();} } break;} case 'F': { int c=choose_counter(q4,q2,q3,q1); if(c==0) {q4.addnode();} else if(c==1) {q2.addnode();} else if(c==2) {q3.addnode();} else if(c==3) {q1.addnode();} else { q4.addnode();} break; } case 'X': { exit(0);} } return 0; } int choose_counter(queue& obj1,queue& obj2,queue& obj3,queue& obj4) { if(obj1.head==NULL) return 0; else if(obj2.head==NULL) return 1; else if(obj3.head==NULL) return 2; else if(obj4.head==NULL) return 3; else return 5; } int choose_counter1(queue& obj1,queue& obj2,queue& obj3) {if(obj1.count<obj2.count && obj2.count<obj3.count) return 1; else if(obj2.count<obj1.count && obj2.count<obj3.count) return 2; else return 3;}
what i want to do is take the current time using time() as the person enters. and if that person is first in the queue, i-e head==NULL, a standard set time should be added to that person's time and when it becomes equal to global time, the person dequeues. for any other person entering, i randomly generate time and then dequeue that person after comparing..i tried to put in the random function but i doubt the correctness..secondly i cnt code up this logic..stuck at it
another problem is that when the control is gone to the create node function, how do another person enter at that time? should i use the
C++ Syntax (Toggle Plain Text)
while(!kbhit())
please help..im running almost outa tym here i am, a bundle of past recollections, of present dreams, knotted up in a reasonably attractive bundle of flesh~ sylvia plath
Here's your own code properly indented :
There are quite a lot of things wrong with this code:
It's starts with your include files:
Change it to:
line 125-129, you have an extra bracket
line 121-123
I guess you meant == instead of =? Remember: = != ==.
And where did the 'd' come from? It's never delcared?
Line 52:
time_compare is never declared
line 60:
line 86-90:
time_compare and time_start are never declared
etc. You should first get this to compile. Look careful at the compiler-errors and warning you get and try to fix it one problem at a time.
Good luck!
cpp Syntax (Toggle Plain Text)
//indented code #include<iostream> #include<time> #include<stdlib> #include<stdio> using namespace std; struct node { node *next; long time_start,time_total,time_compare; }; class queue { private: node *head; node *tail; node n; long time_global; public: int count; queue(); void addnode(); void print(); void dequeue(); friend int choose_counter(queue&,queue&,queue&,queue&); friend int choose_counter1(queue&,queue&,queue&); }; queue::queue() { head=NULL; tail=NULL; count=0; time_global=time(NULL); } void queue::addnode() { if(head==NULL && tail==NULL) { node *person=new node; tail->next=NULL; time_compare=time(NULL)+rand()%10; count++; } else { node *person=new node; tail->next=person; tail=tail->next; tail->next=NULL: count++; } if (head->time_compare==time_global) dequeue(); } void queue::print() { cout<<"STATUS OF QUEUES:"<<endl; } void queue::dequeue() { node *temp; temp=head; head=head->next; count--; if(count==0) head=tail=NULL; else { srand(time(NULL)); head->time_compare=time(NULL)+rand()%10; head->time_total=time_compare-time_start; } } int main() { queue q1,q2,q3,q4; char choice; cout<<"PLEASE CHOSE FROM THE FOLLOWING MENU:"<<endl <<endl<<"1. To enter a male customer, press 'M'." <<endl<<"2. To enter a female customer, press 'F'." <<endl<<"3. To exit press 'X'."<<endl; cin>>choice; switch(choice) { case 'M': { int c=choose_counter(q1,q2,q3,q4); if(c==0) q1.addnode(); else if(c==1) q2.addnode(); else if(c==2) q3.addnode(); else if(c==3) q4.addnode(); else int d=choose_counter1(q1,q2,q3); if (d=1) q1.addnode(); else if(d=2) q2.addnode(); else q3.addnode(); } break; } case 'F': { int c=choose_counter(q4,q2,q3,q1); if(c==0) q4.addnode(); else if(c==1) q2.addnode(); else if(c==2) q3.addnode(); else if(c==3) q1.addnode(); else q4.addnode(); break; } case 'X': { exit(0); } } return 0; } int choose_counter(queue& obj1,queue& obj2,queue& obj3,queue& obj4) { if(obj1.head==NULL) return 0; else if(obj2.head==NULL) return 1; else if(obj3.head==NULL) return 2; else if(obj4.head==NULL) return 3; else return 5; } int choose_counter1(queue& obj1,queue& obj2,queue& obj3) { if(obj1.count<obj2.count && obj2.count<obj3.count) return 1; else if(obj2.count<obj1.count && obj2.count<obj3.count) return 2; else return 3; }
There are quite a lot of things wrong with this code:
It's starts with your include files:
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<time> #include<stdlib> #include<stdio>
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<ctime> #include<stdlib.h> #include<stdio.h>
line 125-129, you have an extra bracket
else
q3.addnode();
} <--- delete this one
break;
}line 121-123
C++ Syntax (Toggle Plain Text)
if (d=1) q1.addnode(); else if(d=2)
And where did the 'd' come from? It's never delcared?
Line 52:
time_compare=time(NULL)+rand()%10; time_compare is never declared
line 60:
tail->next=NULL: <-- change this : to a ; line 86-90:
C++ Syntax (Toggle Plain Text)
{ srand(time(NULL)); head->time_compare=time(NULL)+rand()%10; head->time_total=time_compare-time_start; }
etc. You should first get this to compile. Look careful at the compiler-errors and warning you get and try to fix it one problem at a time.
Good luck!
Last edited by niek_e; Jun 13th, 2008 at 9:09 am.
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: Segmentation error
- Next Thread: Class Hierarchy Question
| Thread Tools | Search this Thread |
Tag cloud for C++
6 add api array arrays beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion convert count data delete desktop directshow dll download dynamic encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelper iamthwee ifstream input int integer java lib library linkedlist linker linux loop looping loops map math matrix memory microsoft newbie news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return string strings struct studio system template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets







would take care in future