I have a code that uses queue in c++ and 3 class Message, MessageSender, and MessageReceiver to express queue content. The program runned but it expressed wrong output as I hope. I inputted 3 message object but queue expressed no messages received. Please help me find errors. I think this may occur with some functions in file "que.h".Here is my code:
//file que.h
#include<iostream>
#include "Message.h"
#ifndef QUE_H
#define QUE_H
class Node
{
public:
Message data;
Node *next;
};
class Que
{
public:
Node *front;
Node *rear;
int numItem;
Que()
{
front=NULL;
rear=NULL;
numItem=0;
}
void add(Message m)
{
Node *newNode;
newNode=new Node;
newNode->data=m;
newNode->next=NULL;
if(isEmpty())
{
front=newNode;
rear=newNode;
}
else
{
rear->next=newNode;
rear=newNode;
}
numItem++;
}
Message remove()
{
Message m;
Node *temp;
if(isEmpty())
{
cout<<"\nthe queue is empty.";
}
else
{
m=front->data;
temp=front;
front=front->next;
delete temp;
numItem--;
return m;
}
}
bool isEmpty()
{
bool status;
if(numItem>0)
status=false;
else
status=true;
return status;
}
int size(){return numItem;}
bool isFull()
{
return false;
}
};
#endif
//file Message.h
#ifndef MESSAGE_H
#define MESSAGE_H
#include<string.h>
class Message
{
public:
char sender[20],recipient[20],content[100],date[10];
Message()
{
strcpy(sender," ");
strcpy(recipient," ");
strcpy(content," ");
strcpy(date,"00/00/0000");
}
Message(char* se,char* re,char* cont,char* da)
{
strcpy(sender,se);
strcpy(recipient,re);
strcpy(content,cont);
strcpy(date,da);
}
char* getDate()
{
return date;
}
};
#endif
//file MessageSender.h
#ifndef MESSAGESENDER_H
#define MESSAGESENDER_H
#include "Message.h"
#include<iostream>
using namespace std;
#include "Que.h"
class MessageSender
{
public:
void sendMessage(char* send,char* re,char* cont,char* da,Que q)
{
Message m(send,re,cont,da);
if(!q.isFull())
{
q.add(m);
cout<<"\nMessage is placed in queue.";
}
else cout<<"\nCannot send - Queue 's full.";
}
};
#endif
//file MessageReceiver.h
#include<iostream>
#include<string.h>
#include "Message.h"
#include "Que.h"
using namespace std;
#ifndef MESSAGERECEIVER_H
#define MESSAGERECEIVER_H
class MessageReceiver
{
public:
void receiveMessage(Que q)
{
Message m=q.remove();
if(strcmp(m.sender," ")!=0)
{
cout<<"\nDate: "<<m.getDate();
cout<<"\nFrom: "<<m.sender;
cout<<"\nTo: "<<m.recipient;
cout<<"\nContent:"<<m.content;
}
else
cout<<"\nNo messages to receive.";
}
void showQueue(Que q)
{
Message m;
cout<<"\nQueue contains "<<q.size()<<" messages";
if(q.isEmpty()==true)
cout<<"\nQueue is empty.";
else
{
Node *currNode=q.front;
while(currNode)
{
m=currNode->data;
cout<<m.getDate()<<" From: "<<m.sender<<" To: "<<m.recipient<<endl;
currNode=currNode->next;
}
}
}
};
#endif
//file drivermain.cpp
#include<iostream>
#include<conio.h>
#include "Message.h"
#include "MessageSender.h"
#include "MessageReceiver.h"
#include "Que.h"
using namespace std;
int main()
{
Que holder;
MessageSender m1,m2,m3;
m1.sendMessage("Rudy","Aini","Assalamu'alaikum","20/09/2007",holder);
m2.sendMessage("Rida","Ahmad","What does the mean?","12/2/1999",holder);
m3.sendMessage("Hakiem","Husein","See you later","21/10/2007",holder);
MessageReceiver m4;
m4.receiveMessage(holder);
m4.showQueue(holder);
getchar();
return 0;
}
Output:
Message is placed in queue.
Message is placed in queue.
Message is placed in queue.
the queue is empty.
No message to receive.
Queue contains 0 messages.
Queue is empty.