Thread: Best way of using MessageBox()?

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    6

    Best way of using MessageBox()?

    Code:
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,LPSTR lpCmdLine, int nShowCmd)
    {
    	std::string line;
    	
            //load a .txt file with a question in it
    	std::ifstream myfile("question1q.txt");
    	
             while(!myfile.eof())
    	{
    		getline(myfile,line);
    		
    	}
    	myfile.close();
    
    	MessageBox(NULL,/*I want to put the string named line right here*/,"The Question",MB_OKCANCEL);
    	
    	return 0;
    }
    I can't figure out a way to convert the string into something compatible with the second argument of MessageBox() I turned the project settings to using MultiByte and that helped a bit, but I'm still not quite getting this.
    I'm just trying to make a simple program that asks a question and then shows you the answer. After I figure out how to make this part work, I'll be able to figure out the second.
    the error that I usually get is
    error C2664: 'MessageBoxA' : cannot convert parameter 2 from 'std::string' to 'LPCSTR' No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called

    I understand the gist of this message, but I'm was unable to find a way I understood to convert string to LPCSTR after scouring the web for about 15 minutes
    Thanks, mjhamrick

    p.s. if there is a better substitute for ifstream for windows programming, I'd love to hear it. This is just the only way I know to read from a text file as of now

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    MessageBoxA takes a LPCSTR (char *) as an argument...you can get one from a string through the std::string's .c_str() method.

  3. #3
    Registered User
    Join Date
    Jun 2010
    Posts
    6
    But how do you use that method? I tried, and did something wrong because it made the program compile correctly but not run.
    if I could just get the basic syntax for that method, I think I should be able to get it.

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    MessageBox(NULL,line.c_str(),"The Question",MB_OKCANCEL);
    Btw, LPCSTR is const char*. LP = pointer, C = const, STR = char, so const char *.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Yep...brain fart on my part on the LPCSTR thing. Not really spending much time in the Windows C or C++ world at all lately.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Delete files that are used
    By Yuri in forum C++ Programming
    Replies: 8
    Last Post: 10-18-2005, 01:48 PM
  2. problem with A simple modeless messagebox
    By hanhao in forum C++ Programming
    Replies: 8
    Last Post: 07-05-2005, 11:18 PM
  3. MessageBox problem
    By jjolly in forum Windows Programming
    Replies: 6
    Last Post: 06-23-2003, 12:07 AM
  4. EnterCriticalSection and MessageBox
    By novacain in forum Windows Programming
    Replies: 13
    Last Post: 01-30-2003, 08:48 AM
  5. A multiline MessageBox?
    By Unregistered in forum C++ Programming
    Replies: 5
    Last Post: 06-27-2002, 05:41 PM

Tags for this Thread