CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 19 of 19
  1. #16
    Join Date
    Jan 2009
    Posts
    1,689

    Re: Console exits before I get a chance to see the output

    envp is on POSIX > 1.1, maybe DOS had it too, but I know it was UNIX.

  2. #17
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Console exits before I get a chance to see the output

    Quote Originally Posted by Eri523 View Post
    As I interpret the C++0x draft (§3.6.1), the name of main is standardized, as well as its return type and first two parameters if they are present. Any other parameters can of course be implementation-defined. (If I recall it correctly, at least some implementations under DOS also had an additional envp parameter. But wait..., well, that was C.)

    The problem with the default-setup Win32 console apps in VC++ is, however, that they apparently have an internal implementation of main() which calls _tmain(). Hence, if you try to roll your own main() in such an application, you'll get an error (something about main() can't be overloaded...). Setting up an empty project gets rid of that problem but, unfortunately for beginners, this is not the default setting.
    I am using Visual C++ for more than 18 years and created hundreds of console projects. I don't know any other IDE which makes it so easy to create and run a console program from the IDE in the debugger. It is right that MS changes the default of the default character set from ANSI (multi-byte charset) to UTF-16 (unicode charset) and therefore you have to change the main entry point (the _tmain allows a switch between both character sets) if you don't want that proprietary feature. But, though annoying and confusing for the first time, it can be changed within seconds even if you missed to check the Empty Project flag when creating the project. Simply change the line

    Code:
    int _tmain(int argc, _TCHAR* argv[])
    into

    Code:
    int main(int argc, char* argv[])
    or

    Code:
    int main()

    and choose 'Use Multi-Byte Character Set' in

    Projects - Properties - Configuration Properties - General - Character Set

    for both Debug and Release configuration. That's all cause the _tmain is only a preprocessor macro (same as _TCHAR), which either maps to main or to _wmain (for wide characters commandline input) and only the _wmain is a newly defined entry point.

    To break the program before exit when running in the debugger you simply can run your code in a loop what furthermore would improve your program:

    Code:
    int main()
    {
           while (true)
           {
                double f;
                double m;
                cout << "Enter the length in feet: ";
                cin >> f;
                m = f/3.28;
                cout << f << "feet is"<< m << "meters" << endl;
                cout << "Continue (y/n)? ";
                char c;
                cin >> c;
                if (c != 'y')
                     break;
            }
            return 0;

  3. #18
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Re: Console exits before I get a chance to see the output

    Quote Originally Posted by itsmeandnobodyelse View Post
    [...] That's all cause the _tmain is only a preprocessor macro (same as _TCHAR), which either maps to main or to _wmain (for wide characters commandline input) and only the _wmain is a newly defined entry point.
    Interesting. That also gives a simple explanation for the compiler complaining about an attempt to overload main() I mentioned above.

    [...] and choose 'Use Multi-Byte Character Set' [...].
    Considering all your other explanations, is this then really still required if I don't use any "_T()-aware" (which would also mean MS-specific) stuff? This stuff isn't used in the usual textbook sample code. I managed to get at least the most basic C++ "Hello world!" (using std::cout) to work without changing anything in the project settings.

    Or to put the question much shorter: Does the character set setting do anything else at all besides defining (or not defining) _UNICODE?
    Last edited by Eri523; October 19th, 2010 at 04:32 PM.
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  4. #19
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Console exits before I get a chance to see the output

    No, I think that's all the character set setting does.

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured