Solved

<< to AfxMessageBox / cout

Posted on 2002-04-15
3
309 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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
Different colored text in ComboBox without Subclassing 8 72
sum28 challenge 31 117
wait notify demo infinite loop 3 136
FizzBuzz challenge 9 84
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: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
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.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

740 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