Search code examples
c++newlinemessagebox

C++ Identifier "Environment" is undefined


According to this post, I tried to include a new line in a MessageBox like below:

std::wstring msg = "Text here" + Environment.NewLine + "some other text";

MessageBox(nullptr, msg.c_str(), L"Hello World!", MB_ICONINFORMATION);

But compiler generated the error:

E0020: identifier "Environment" is undefined

I tried including <windows.system.h>, but it did nothing.

Project type: C++ ATL

Thanks in Advance.


Solution

  • You can't include Environment.NewLine in your native C++ application because it is a .NET construct. For a newline character in standard C++ use std::endl or a '\n' character:

    #include <iostream>
    int main(){
        std::cout << "Hello World." << std::endl;
        std::cout << "This prints\n text on two lines.";
    }
    

    For a newline in the MessageBox WinAPI function use \r\n characters. Your code should be:

    #include <Windows.h>
    #include <string>
    int main(){
        std::wstring msg = L"Text here \r\n some other text";
        MessageBox(NULL, msg.c_str(), L"Hello World!", MB_ICONINFORMATION);
    }