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.

Recommended Answers

All 8 Replies

U are making a shallow copy for Message while assigning it to the data.
U need to do a deep copy instead to retain the member data.
Overload the assignment operator for Message class and use strcpy() in it for copying each of the member.

There may be some more problems. But resolve this issue first.

i would use:
- std::queue
- instead of char sender[20],recipient[20],content[100],date[10]; -> std::string
- strcpy would obsolte with std:.string
- instead of:

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 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
            throw CNoMessageReceived("No messages to receive.");
      }

or

eError 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
            return ERR_NO_MSG_RCV;
      }

- use & Reference

eError receiveMessage(Que q)

BETTER:

eError receiveMessage(Que &q)

- cout in class is always bad design, except your class method called "PrintDebugXYZ"

enough for today :)

I used string sender but occur error"string is not a type".I have a question "why this program always shows an empty queue".

i added string,string.h or cstring.But still failure!We can't use STL queue here because we have to write some functions.

Can you post your code? <<snip>>

Thanks to all of you. You gave me some good hints,but I recalled that strcpy is not a problem.Now I did it well,my teacher gave a wrong code in class Que in class.So when I checked in my textbook, I printed result well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.