Solved

<< to AfxMessageBox / cout

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Running shell command from Access 3 69
Define unique primary key 9 89
Is there a simple front-end menu system. 9 70
x-bar in Google Sheets 2 25
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…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

912 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

20 Experts available now in Live!

Get 1:1 Help Now