• 0

input from a mesage box?


Question

hi guys, new to c++ here. So we have a school project due tomorrow, a hangman game of sort and i just wanted to go a bit above the required so i thought i would put in a message box. Now i got the working and all but the thing is, i would like to figure out that if the user clicks NO then the program exits? how can i do this?

LPCTSTR Cap = L"HOW TO PLAY HANGMAN";
	MessageBox(NULL, L"sample",Cap, MB_YESNOCANCEL );

as you can see, my msg box has 3 click able buttons, YES NO and CANCLE. How can i make my program do something is say NO is pressed?

like

if NO is pressed

exit

thank you!

Link to comment
Share on other sites

3 answers to this question

Recommended Posts

  • 0

A messagebox has a return type of int. To utilize the returned value, display your messagebox like so:

int mbox = MessageBox(NULL,
		L"Message",
		L"Title",
		MB_YESNO
	);

depending on the users choice, a different return value will be placed in the variable mbox.

typical Win32 style coding would use a switch to parse the integer and compare it against predefined values like so:

switch (mbox)
	{
	case IDYES:
		// code here
		break;
	case IDNO:
		// code here
		break;
	}

Edited by Tech God
Link to comment
Share on other sites

  • 0

Also, there is technically a bug in your code. LPCTSTR is a pointer to a const TCHAR string, not a WCHAR string. A TCHAR is a type that is defined as char if you compiled as ANSI, and WCHAR if you compile as Unicode. You are assigning it a Unicode string (the L prefix), and so it would break if compiled as ANSI. TCHAR strings have to be surround by the TEXT or _T macro.

In other words,

LPCTSTR Cap = TEXT("HOW TO PLAY HANGMAN");

MessageBox(NULL, TEXT("sample"), Cap, MB_YESNOCANCEL);

An alternative if you don't care about ANSI support (and there's no need to really, unless you want to support Windows 95. NT is all Unicode) would be

WCHAR *Cap = L"HOW TO PLAY HANGMAN";

MessageBox(NULL, L"sample", Cap, MB_YESNOCANCEL);

Link to comment
Share on other sites

  • 0
Also, there is technically a bug in your code. LPCTSTR is a pointer to a const TCHAR string, not a WCHAR string. A TCHAR is a type that is defined as char if you compiled as ANSI, and WCHAR if you compile as Unicode. You are assigning it a Unicode string (the L prefix), and so it would break if compiled as ANSI. TCHAR strings have to be surround by the TEXT or _T macro.

In other words,

LPCTSTR Cap = TEXT("HOW TO PLAY HANGMAN");

MessageBox(NULL, TEXT("sample"), Cap, MB_YESNOCANCEL);

An alternative if you don't care about ANSI support (and there's no need to really, unless you want to support Windows 95. NT is all Unicode) would be

WCHAR *Cap = L"HOW TO PLAY HANGMAN";

MessageBox(NULL, L"sample", Cap, MB_YESNOCANCEL);

I use mingw32, it complains without it.

To add to information regarding ANSI & Unicode, unicode applications also run faster on NT systems.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.