Thread: Cicrcular queue help

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    135

    Cicrcular queue help

    //queue.h
    Code:
    #ifndef QUEUE_H_
    #define QUEUE_H_
    
    #include <string>
    using namespace std;
    class Queue
    {
    public:
    	bool IsEmptyQueue(void) const;
    	bool IsFullQueue(void) const;
    	void initializeQueue(void);
    	int Myfront(void) const;
    	int Myback(void) const;
    	void addQueue(  int);
    	void removeQueue(void);
    	Queue(int);
    	~Queue(void);
    	void PrintNumber(void);
    	void PrintQueueNumber(void);
    private:
    	int maxQueuesize;
    	int count;
    	int front;
    	int back;
    	int * list;
    };
    
    #endif
    //queue.cpp
    Code:
    #ifndef QUEUE_H_
    #define QUEUE_H_
    
    #include <string>
    using namespace std;
    class Queue
    {
    public:
    	bool IsEmptyQueue(void) const;
    	bool IsFullQueue(void) const;
    	void initializeQueue(void);
    	int Myfront(void) const;
    	int Myback(void) const;
    	void addQueue(  int);
    	void removeQueue(void);
    	Queue(int);
    	~Queue(void);
    	void PrintNumber(void);
    	void PrintQueueNumber(void);
    private:
    	int maxQueuesize;
    	int count;
    	int front;
    	int back;
    	int * list;
    };
    
    #endif
    main.cpp
    Code:
    #include <iostream>
    #include <string>
    #include "Queue.h"
    
    using namespace std;
    
    void main(void)
    {
    	int mySize; //the amount of queue for the clinic
    	int number;
    	string name;
    	int Id;
    	cout << "Enter the queue you want for the maximum for the clinic: " ;
    	cin >> mySize;
    	cout << endl;
    	while (mySize <= 0)
    	{
    		cout << "Please enter number greater than 0: " ;
    		cin >> mySize;
    		cout << endl;
    	}
    	Queue * theQueue = new Queue(mySize);
    	system ("cls");
    
    	cout << "Welcome to my Clinic queuing system!" << endl;
    	cout << "-Press <1> to get a queue number" << endl;
    	cout << "-Press <2> to get the first patient in the queue" << endl;
    	cout << "-Press <3> to print all in the queue" << endl;
    	cout << "-Press <4> to exit from this program" << endl;
    	cin >> number;
    	while (number <= 0 || number > 4)
    	{
    		cout << "Wrong input! Enter again: ";
    		cin >> number;
    	}
    
    	if (number == 1)
    	{
    		system ("cls");
    		cout << "Welcome to my Clinic queuing system!" << endl;
    		cout << "Enter your name: " ;
    		cin >> name;
    		cout << "Enter your ID: ";
    		cin >> Id;
    		theQueue ->addQueue( Id);
    		
    		theQueue ->PrintQueueNumber();
    	}
    	system ("pause");
    }
    I need help, i am cuurently designing a queue system for a clinic. Right now, i have a problem. I want to ask the user toenter a Id and their name and store their particular into the queue. But right now, my class function addQueue only store the ID number but not the name. How should i do it such that the function can store both the name and the ID number and store it into the queue system.

    Example something like this:

    Queue number 0:
    Name: Tomy
    ID: 23456

    Queue number 1:
    Name: Rachel
    ID: 12345

    Currently my class only store Id into the queue system. I want to store the name too. My menu syetm is still halfway done but all my solution is working perfectly, just the question i got problem. I did try to add a string into the addQueue class function together with the ID but not sure how to code it into the queue.cpp file there.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well a crude way (if you're just interested in a proof of concept single implementation) would be something like
    Code:
    class myData {
      string name;
      int id;
    };
    class Queue
    {
    public:
    	bool IsEmptyQueue(void) const;
    	bool IsFullQueue(void) const;
    	void initializeQueue(void);
    	int Myfront(void) const;
    	int Myback(void) const;
    	void addQueue(  myData);
    	myData removeQueue(void);
    	Queue(int);
    	~Queue(void);
    	void PrintNumber(void);
    	void PrintQueueNumber(void);
    private:
    	int maxQueuesize;
    	int count;
    	int front;
    	int back;
    	myData * list;
    };
    Later on, look into how to write templates.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    And while we're at it, main should return int, not void, and using (void) in C++ is just silly. Just use empty parenthesis () for any function that does not take any parameters.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Quote Originally Posted by Salem View Post
    Well a crude way (if you're just interested in a proof of concept single implementation) would be something like
    Code:
    class myData {
      string name;
      int id;
    };
    class Queue
    {
    public:
    	bool IsEmptyQueue(void) const;
    	bool IsFullQueue(void) const;
    	void initializeQueue(void);
    	int Myfront(void) const;
    	int Myback(void) const;
    	void addQueue(  myData);
    	myData removeQueue(void);
    	Queue(int);
    	~Queue(void);
    	void PrintNumber(void);
    	void PrintQueueNumber(void);
    private:
    	int maxQueuesize;
    	int count;
    	int front;
    	int back;
    	myData * list;
    };
    Later on, look into how to write templates.
    Wow, is there the only way to solve it. I am ultra-confused about the competition topic in c++ and I always try to avoid it. Mostly, i used heritance on class c++ and seldom use composition. If that the case, other than adding name, and ID, do I need to include the constructor for myData class.

  6. #6
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    And is class myData a base class or derived class?

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by evildotaing View Post
    Wow, is there the only way to solve it.
    There is always more than one way of doing something.

    I am ultra-confused about the competition topic in c++ and I always try to avoid it.
    I have no idea what you're talking about or how it is relevant.

    Mostly, i used heritance on class c++ and seldom use composition.
    Well get used to it then. If you're heavily going one way or the other then chances are you're often using the wrong tool for the job.

    If that the case, other than adding name, and ID, do I need to include the constructor for myData class.
    Well you certainly have to change myData in some way because currently everything is private. Whether you add a constructor or ust set the variables directly is up to you.

    And is class myData a base class or derived class?
    No. Why do you think it has to be either?
    Last edited by iMalc; 12-29-2011 at 03:06 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    cannot work, i already received error for putiing myData in the addQueue class function, is there any way to solve my problem. And i cant use myData for the pointer to declared in list

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Post what you tried, then perhaps we can tell you what you should be doing, or what else to try.

    Don't just post "it didn't work" and hope we'll serve up a ready answer you can just take and use.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  10. #10
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    main.cpp
    Code:
    #include <iostream>
    #include <string>
    #include "Queue.h"
    #include "myData.h"
    
    using namespace std;
    
    void main(void)
    {
    	myData a;
    	int mySize; //the amount of queue for the clinic
    	int number;
    	string name;
    	int Id;
    	cout << "Enter the queue you want for the maximum for the clinic: " ;
    	cin >> mySize;
    	cout << endl;
    	while (mySize <= 0)
    	{
    		cout << "Please enter number greater than 0: " ;
    		cin >> mySize;
    		cout << endl;
    	}
    	Queue * theQueue = new Queue(mySize);
    	system ("cls");
    
    	cout << "Welcome to my Clinic queuing system!" << endl;
    	cout << "-Press <1> to get a queue number" << endl;
    	cout << "-Press <2> to get the first patient in the queue" << endl;
    	cout << "-Press <3> to print all in the queue" << endl;
    	cout << "-Press <4> to exit from this program" << endl;
    	cin >> number;
    	bool go = false;
    	
    	while (number <= 0 || number > 4)
    	{
    		cout << "Wrong input! Enter again: ";
    		cin >> number;
    	}
    
    	if (number == 1)
    	{
    		system ("cls");
    		
    		cout << "Enter your name: " ;
    		cin >> name;
    		cout << "Enter your ID: ";
    		cin >> Id;
    		
    		theQueue ->addQueue(a);
    		
    		theQueue ->PrintQueueNumber();
    	}
    
    	else if (number == 2)
    	{
    		
    	}
    	
    	system ("pause");
    }
    queue.cpp
    Code:
    #include "Queue.h"
    #include <cstdlib>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    bool Queue::IsEmptyQueue(void) const
    {
    	return (count == 0);
    }
    
    bool Queue::IsFullQueue(void) const
    {
    	return (count == maxQueuesize);
    }
    
    void Queue::initializeQueue(void)
    {
    	front = 0;
    	back = maxQueuesize - 1;
    	count = 0;
    }
    
    int Queue::Myfront(void) const
    {
    	if (!IsEmptyQueue())
    	{
    		return list[front];
    	}
    	/*
    	else
    	{
    		return 0;
    	}
    	*/
    }
    
    int Queue::Myback(void) const
    {
    	if (!IsFullQueue())
    	{
    		return list[back];
    	}
    	/*
    	else
    	{
    		return 0;
    	}
    	*/
    }
    
    
    void Queue::addQueue(myData )
    {
    	if (!IsFullQueue())
    	{
    		back = (back + 1) % maxQueuesize;
    		count++;
    		list[back] = element;
    	}
    
    	else
    	{
    		cout << "Unable to add a full queue: " << endl;
    	}
    }
    
    myData Queue::removeQueue(void)
    {
    	if (!IsEmptyQueue())
    	{
    		count --;
    		front = (front + 1) % maxQueuesize;
    	}
    	else
    	{
    		cout << "Cannot remove from an emty queue." << endl;
    	}
    }
    
    Queue::Queue(int size)
    {
    	if (size <= 0)
    	{
    		cout << "Size must be positive." << endl;
    		//cout << "An array of size 10 is created for you." << endl;
    		//maxQueuesize = 10;
    	}
    	else
    	{
    		maxQueuesize = size;
    	}
    
    	front = 0;
    	back = maxQueuesize - 1;
    	count = 0;
    	list = new int[maxQueuesize];
    
    }
    
    Queue::~Queue(void)
    {
    	delete[]  list;
    }
    
    void Queue::PrintNumber(void)
    {
    	for (int i = 0; i < maxQueuesize; i ++)
    	{
    		cout << i << "\t:\t" << list[i] << endl;
    	}
    }
    
    void Queue::PrintQueueNumber(void)
    {
    	cout << "Your queue number is " << front << endl;
    	cout << "Your queue number is #" << back << endl;
    }
    queue.h
    Code:
    #ifndef QUEUE_H_
    #define QUEUE_H_
    
    #include <string>
    using namespace std;
    class Queue
    {
    public:
    	bool IsEmptyQueue(void) const;
    	bool IsFullQueue(void) const;
    	void initializeQueue(void);
    	int Myfront(void) const;
    	int Myback(void) const;
    	void addQueue( myData);
    	myData removeQueue(void);
    	Queue(int);
    	~Queue(void);
    	void PrintNumber(void);
    	void PrintQueueNumber(void);
    private:
    	int maxQueuesize;
    	int count;
    	int front;
    	int back;
    	myData * list;
    };
    
    #endif
    mydata..cpp
    Code:
    #include <iostream>
    #include "myData.h"
    #include <string>
    
    using namespace std;
    
    myData::myData(string myName, int idNumber)
    {
    	name = myName;
    	id = idNumber;
    }
    myData.h
    Code:
    #ifndef MYDATA_H_
    #define MYDATA_H_
    #include <iostream>
    #include <string>
    
    using namespace std;
    class myData
    {
    private:
    	string name;
    	int id;
    public:
    	myData(string myName, int idNumber);
    };
    
    #endif
    Seriously, i am very lousy in c++ in using composition or templates, so I wonder if there are other ways to solve the problem.

  11. #11
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    1>------ Build started: Project: my das assignment 2, Configuration: Debug Win32 ------
    1>Compiling...
    1>main.cpp
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\my das assignment 2\my das assignment 2\queue.h(14) : error C2061: syntax error : identifier 'myData'
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\my das assignment 2\my das assignment 2\queue.h(15) : error C2146: syntax error : missing ';' before identifier 'removeQueue'
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\my das assignment 2\my das assignment 2\queue.h(15) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\my das assignment 2\my das assignment 2\queue.h(15) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\my das assignment 2\my das assignment 2\queue.h(15) : warning C4183: 'removeQueue': missing return type; assumed to be a member function returning 'int'
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\my das assignment 2\my das assignment 2\queue.h(25) : error C2143: syntax error : missing ';' before '*'
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\my das assignment 2\my das assignment 2\queue.h(25) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\my das assignment 2\my das assignment 2\queue.h(25) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\my das assignment 2\my das assignment 2\main.cpp(10) : error C2512: 'myData' : no appropriate default constructor available
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\my das assignment 2\my das assignment 2\main.cpp(50) : error C2660: 'Queue::addQueue' : function does not take 1 arguments
    1>Queue.cpp
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\my das assignment 2\my das assignment 2\queue.h(14) : error C2061: syntax error : identifier 'myData'
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\my das assignment 2\my das assignment 2\queue.h(15) : error C2146: syntax error : missing ';' before identifier 'removeQueue'
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\my das assignment 2\my das assignment 2\queue.h(15) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\my das assignment 2\my das assignment 2\queue.h(15) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\my das assignment 2\my das assignment 2\queue.h(15) : warning C4183: 'removeQueue': missing return type; assumed to be a member function returning 'int'
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\my das assignment 2\my das assignment 2\queue.h(25) : error C2143: syntax error : missing ';' before '*'
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\my das assignment 2\my das assignment 2\queue.h(25) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\my das assignment 2\my das assignment 2\queue.h(25) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\my das assignment 2\my das assignment 2\queue.cpp(29) : error C2065: 'list' : undeclared identifier
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\my das assignment 2\my das assignment 2\queue.cpp(43) : error C2065: 'list' : undeclared identifier
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\my das assignment 2\my das assignment 2\queue.cpp(54) : error C2065: 'myData' : undeclared identifier
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\my das assignment 2\my das assignment 2\queue.cpp(55) : error C2448: 'Queue::addQueue' : function-style initializer appears to be a function definition
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\my das assignment 2\my das assignment 2\queue.cpp(69) : error C2143: syntax error : missing ';' before 'Queue::removeQueue'
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\my das assignment 2\my das assignment 2\queue.cpp(69) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\my das assignment 2\my das assignment 2\queue.cpp(70) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\my das assignment 2\my das assignment 2\queue.cpp(98) : error C2065: 'list' : undeclared identifier
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\my das assignment 2\my das assignment 2\queue.cpp(104) : error C2065: 'list' : undeclared identifier
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\my das assignment 2\my das assignment 2\queue.cpp(104) : error C2541: 'delete' : cannot delete objects that are not pointers
    1>c:\documents and settings\owner\my documents\visual studio 2008\projects\my das assignment 2\my das assignment 2\queue.cpp(111) : error C2065: 'list' : undeclared identifier

    The error i received, i am completely stuck for all the error, maybe could help me what is the problem for the list and the delete objects part.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Gods, mate. You haven't fixed anything!
    And this isn't even composition, just normal use of data types! The errors stems from syntax errors and just lack of insight on the use of custom types (eg structs, classes), and I am going to leave you to find it until you address all the concerns raised above.
    Also, this

    Queue * theQueue = new Queue(mySize);

    ...is utterly pointless and stupid. Why are you using new?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    Quote Originally Posted by Elysia View Post
    Gods, mate. You haven't fixed anything!
    And this isn't even composition, just normal use of data types! The errors stems from syntax errors and just lack of insight on the use of custom types (eg structs, classes), and I am going to leave you to find it until you address all the concerns raised above.
    Also, this

    Queue * theQueue = new Queue(mySize);

    ...is utterly pointless and stupid. Why are you using new?
    For the new part, i am asking the user to create a dynamic array for the queue system, so of couse I need to use a new pointer

  14. #14
    Registered User
    Join Date
    Apr 2011
    Posts
    135
    I have no problem if all of my function are using int, but since i changed it to myData, i am completely unsure. And the changing part, means i am doing composition.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by evildotaing View Post
    For the new part, i am asking the user to create a dynamic array for the queue system, so of couse I need to use a new pointer
    Uhh what? A queue system is dynamic in nature. By dynamic, it means it can expand its size as required. You do not need a dynamic number of queues. You need ONE queue.

    Quote Originally Posted by evildotaing View Post
    I have no problem if all of my function are using int, but since i changed it to myData, i am completely unsure. And the changing part, means i am doing composition.
    All you have to do is change "int" to "myData" and be aware of the usual things when using a custom type.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. (queue*)this)->queue::qput’ does not have class type
    By brack in forum C++ Programming
    Replies: 13
    Last Post: 11-11-2010, 03:41 PM
  2. queue
    By hokuokekai in forum C++ Programming
    Replies: 1
    Last Post: 10-15-2010, 09:19 AM
  3. Help me about queue
    By tqn_hnvietnam in forum C Programming
    Replies: 7
    Last Post: 09-29-2008, 08:37 PM
  4. ADT queue
    By ^xor in forum C Programming
    Replies: 4
    Last Post: 06-15-2005, 05:43 AM
  5. Queue and Priority Queue
    By Pamela in forum C++ Programming
    Replies: 1
    Last Post: 12-07-2001, 11:09 PM