Solved

<< to AfxMessageBox / cout

Posted on 2002-04-15
3
308 Views
Last Modified: 2013-11-20
My program can be run from the command line or using an MFC GUI.  During calculation, I would like error messages to be directed either to cout or popped up in a MessageBox, depending on how the user is running it.  How best to do this?

My current plan is to set a flag on startup, and check it every time there is output, something like:

void ShowErrorMessage(std::string msg)
{
 if flag
   std::cout << msg;
 else
   MessageBox(msg);
}

This feels wrong - rather than
ShowErrorMessage("my message");
I'd like to be doing
errOutput << "My message";

Is this a better way?  How would I best implement it?  Although at the moment, the program will only run on Windows, I'd like to make the code as platform independent as possible.
0
Comment
Question by:IainHere
  • 2
3 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 100 total points
ID: 6941852
class CErrOutput {

public:
 enum AppType {
 GUI,
 CONSOLE
 };
 CErrOutput ( AppType type) { m_appType = type};
 virtual ~CErrOutput () {};

 void operator << ( const std::string& msg);

private:
 AppType m_appType;
};

void
CErrOutput::operator << ( const std::string& msg) {

// pseudocode from here :o)
if ( m_appType != GUI)
  std::cout << msg;
else
  MessageBox(msg);

}
0
 
LVL 4

Author Comment

by:IainHere
ID: 6941992
No wonder you made 10e6 points - concise and correct.

Thanks a million :-)
0
 
LVL 86

Expert Comment

by:jkr
ID: 6942006
Thanks :o)
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Tab names are off by one 5 54
how to monitor remote shell execution on linux 9 104
How to know only "File created" with EventLog 3 35
Increment column based of a FK 8 31
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question