Challenge 71

Description

The purpose of this challenge is to use pointers in various ways

 Requirements

  1. Define a struct called Message as below
    struct Message
    {
        string text;
        bool sent;
    };
  2. Create a global function Message * new_msg(string text). This function will create a new Message instance, assign its text property from the parameter, and set its sent property to false. The function returns the address of the newly created Message instance.
  3. Define a class called Broker as below
    class Broker
    {
      private:
        Message *one;
        int count;
        bool ready;
    
      public:
    };
  4. Add a default constructor to the Broker class to set one to NULL, count to zero, and ready to false.
  5. Create a public member function void send(). This function will check to see that one is currently assigned (not NULL), set one’s sent property to true. It will also increment count and set ready to false.
  6. Create a public member function void prepare(Message * outgoing). This function will check to see that outgoing is currently assigned (not NULL). Then, it assigns outgoing to one, and sets ready to true.
  7. Create getter functions is_ready() and get_count() to return values for ready and count respectively.
  8. Use the sample main below
  9. In the case 1 block, if m1 is NULL, ask the user to enter some text (this will be the message text). Call the global new_msg() function to create a new message instance that gets assigned to m1. If you use the getline() function to prompt the user for a sentence of text, you may have to call cin.ignore() before using getline(). Also, if m1 is not NULL, then display  a message “Only one message can be created at a time. Existing message must be prepped and sent”
  10. In case 2 block, display a message “No message to prepare – message must first be created” if m1 is NULL. Otherwise, call broker’s prepare() function, and display a message “Message is ready to be sent.”
  11. In case 3 block, if m1 is not NULL and the broker is ready (remember the getter function you wrote earlier?), then display a message “Sending…”. Then, call broker’s send() function. Also, display a message after sending. See sample interaction for this message. Additionally, delete the m1 instance and set m1 to NULL. All of the above was part of an IF-statement block — add an else clause to display a message “No message is ready to send”
  12. In case 4 block, display the number of messages the broker has sent. See sample interaction.

Sample main()

int main()
{
  int input;
  string text;
  Message * m1 = NULL;
  Broker * broker = new Broker;
  
  do
  {
    cout << endl;
    cout << "1 - Create new message" << endl;
    cout << "2 - Prepare message for sending" << endl;
    cout << "3 - Send message" << endl;
    cout << "4 - Get broker count" << endl;
    cout << "0 - Quit" << endl;
    cout << "Action: ";
    cin >> input;

    switch (input)
    {
       case 1: // Create message, more code
       case 2: // Prepare message, more code
       case 3: // Send message, more code
       case 4: // Show count, more code
    }
  } while (input != 0);
  
  if (broker->is_ready())
  {
    cout << "Broker has unsent pending messages. Quitting anyway\n";
  }

  // don't forget to delete broker pointer
  return 0;
}

Sample Interaction

1 - Create new message
2 - Prepare message for sending
3 - Send message
4 - Get broker count
0 - Quit

Action: 3
No message is ready to send

1 - Create new message
2 - Prepare message for sending
3 - Send message
4 - Get broker count
0 - Quit

Action: 1
Enter message text: Hi, there

1 - Create new message
2 - Prepare message for sending
3 - Send message
4 - Get broker count
0 - Quit

Action: 1
Only one message can be created at a time. Existing message must be prepped and sent

1 - Create new message
2 - Prepare message for sending
3 - Send message
4 - Get broker count
0 - Quit

Action: 3
No message is ready to send

1 - Create new message
2 - Prepare message for sending
3 - Send message
4 - Get broker count
0 - Quit

Action: 2
Message is ready to be sent

1 - Create new message
2 - Prepare message for sending
3 - Send message
4 - Get broker count
0 - Quit

Action: 3
Sending ... Sent 1 message(s) so far

1 - Create new message
2 - Prepare message for sending
3 - Send message
4 - Get broker count
0 - Quit

Action: 4
Broker has sent 1 message(s)

1 - Create new message
2 - Prepare message for sending
3 - Send message
4 - Get broker count
0 - Quit

Action: 1
Enter message text: How goes it? 

1 - Create new message
2 - Prepare message for sending
3 - Send message
4 - Get broker count
0 - Quit

Action: 2
Message is ready to be sent

1 - Create new message
2 - Prepare message for sending
3 - Send message
4 - Get broker count
0 - Quit

Action: 0
Broker has unsent pending messages. Quitting anyway.

LEGEND
PROGRAM OUTPUT
USER INPUT
FROM INPUT

CATALOG ID: CPP-CHAL0071

Print Requirements