|
-
October 19th, 2010, 09:07 AM
#16
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.
-
October 19th, 2010, 10:29 AM
#17
Re: Console exits before I get a chance to see the output
 Originally Posted by Eri523
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
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;
-
October 19th, 2010, 12:41 PM
#18
Re: Console exits before I get a chance to see the output
 Originally Posted by itsmeandnobodyelse
[...] 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.
-
October 19th, 2010, 01:29 PM
#19
Re: Console exits before I get a chance to see the output
No, I think that's all the character set setting does.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|