Solved

<< to AfxMessageBox / cout

Posted on 2002-04-15
3
311 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mixString challenge 36 137
Change owner from userA to server Local Administrators 7 74
Use of condition with 'serial' in ansible 2 102
Increment column based of a FK 8 68
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.

734 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