I'm sorry but could someone deleter this article for me? I already figured out the bug. I'm sorry :(. It's basically the function to display the message is called twice, one inside another function, and the other by me ...

----------------------------------------------

The assignment requires the implementation of both patterns I listed above, in addition to using abstract class.
And basically, I got everthing compiled just fine. It's just this one little error that frustrates me: when I run the program, and got to the point where the message would be printed out, instead of printing just once, it printed twice! given that the instance created is either ScreenLogger or FileLogger (explained below). The weird thing is though the message is printed twice on the screen, in case of the FileLogger instance is created, the message is only printed once to the output file?

[The displaymessage function in ScreenLogger would print the message to the screen only; while the displaymessage function in FileLogger would print the message to both the screen and outputfile.]

Heres the codes:

main

#include <iostream>
#include <string>
#include "CS1337Logger.h"
#include "LogSingleton.h"
using namespace std;

void message(string, char *);

int main()
{
    char msg[] = "CS 1337 Logger";
    string decision;
    cout << "Type in FileLogger or ScreenLogger: ";
    cin >> decision;

    message(decision, msg);

    return 0;
}

void message(string decision, char *msg)
{
    CS1337Logger *instance = LogSingleton::getLogger(decision);
    if (instance != NULL) // If creating new instance is success - instance should be != NULL, then
    {
        instance->setter(msg);
        instance->logMessage(msg);
        instance->displayMessage(msg);
    }
}

CS1337Logger.h

#ifndef CS1337LOGGER_H_
#define CS1337LOGGER_H_

#include <iostream>
#include <string>
#include <cstring>
#include <fstream>
using namespace std;

class CS1337Logger
{
    private:
    bool loggingEnabled;

    public:
    CS1337Logger()
    {
        loggingEnabled = false;
    }
    void setter(char *message)
    {
        if (message != NULL) // if there is a message to be printed
        {
            loggingEnabled = true;
        }
        else
        {
            loggingEnabled = false;
        }
    }
    void logMessage(char *message)
    {
        if (loggingEnabled == true)
        {
            displayMessage(message);
        }
    }
    virtual void displayMessage(char *) = 0; // pure virtual function to be overridden
    virtual ~CS1337Logger() {} // virtual destructor that does nothing
};

class ScreenLogger : public CS1337Logger
{
    public:
    virtual void displayMessage(char *message)
    {
        cout << "The message is: " << message;
    }
};

class FileLogger : public CS1337Logger
{
    public:
    ofstream outputfile;
    FileLogger()
    {
        outputfile.open("Log.txt");
    }
    virtual void displayMessage(char *message)
    {
        cout << "The message is: " << message;
        outputfile << message;
        cout << "\nIt has been read into outputfile.\n";
        outputfile.close(); // close outputfile
    }
};
#endif /* CS1337LOGGER_H_ */

LogSingleton.h

#ifndef LOGSINGLETON_H_
#define LOGSINGLETON_H_

#include <iostream>
#include <string>
#include "CS1337Logger.h"
using namespace std;

class LogSingleton
{
    private:
    LogSingleton();

    public:
    static CS1337Logger* instance; // this static pointer makes sure that only one instance of class is created.
    static CS1337Logger *getLogger(string decision)
    {
        if (instance == NULL)
        {
            if (decision == "FileLogger")
                instance =  new FileLogger;
            else if (decision == "ScreenLogger")
                instance = new ScreenLogger;
            else
                cout << "Error. No instance will be created.\n";
        } // end if statement
        else
        {
            cout << "An instance has already been created.";  // instance is pointer to already created CS1337Logger
        }
        return instance;
    } // end static function
};

CS1337Logger* LogSingleton::instance = NULL;

#endif /* LOGSINGLETON_H_ */

You just need to mark it solved.

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.