Solved

<< to AfxMessageBox / cout

Posted on 2002-04-15
3
303 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
Comment Utility
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
Comment Utility
No wonder you made 10e6 points - concise and correct.

Thanks a million :-)
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Thanks :o)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Folder Comparison 12 52
deburging in oracle form 12 76
wait notify demo infinite loop 3 79
withoutTen challenge 14 88
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

771 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now