-
October 17th, 2010, 09:29 AM
#1
Console exits before I get a chance to see the output
Hi,
I am quite new to programming and have an issue with running a basic program. Here is the code:
Code:
#include <iostream>
using namespace std;
int main()
{
double f;
double m;
cout << "Enter the length in feet: ";
cin >> f;
m = f/3.28;
cout << f << "feet is"<< m << "meters";
return 0;
When I run the program and enter a number to respond to the prompt and then press enter, the console app exits so I wont get a chance to see the output. What is missing from the code?
Secondly , I am using Visual studio 2010 and I would like to know why is there no intellisense when writing C++ code. I know there is when writing C# code.
Thanks
-
October 17th, 2010, 09:37 AM
#2
Re: Console exits before I get a chance to see the output
Originally Posted by fsdama
When I run the program and enter a number to respond to the prompt and then press enter, the console app exits so I wont get a chance to see the output. What is missing from the code?
Nothing, except that you should be running your program from a command prompt. That said, in a recent thread, Eri523 observed that "VC++ 2010 Express does exactly that for Win32 console apps (if ran without debugging)", where "that" refers to pausing the console window for you. Therefore, as an alternative you could try creating a Win32 console application project for your program.
-
October 17th, 2010, 09:39 AM
#3
Re: Console exits before I get a chance to see the output
Code::Blocks has intellisense.
-
October 17th, 2010, 09:42 AM
#4
Re: Console exits before I get a chance to see the output
Running it from a command prompt? I am using VS 2010, so why cant I run it from here?
-
October 17th, 2010, 09:51 AM
#5
Re: Console exits before I get a chance to see the output
Originally Posted by fsdama
Running it from a command prompt? I am using VS 2010, so why cant I run it from here?
You can run your program from your IDE, as you yourself have observed. You also observed that "the console app exits so I wont get a chance to see the output". Running from a separate command prompt window solves this problem. Using a Win32 console application project might also solve this problem. Making the program wait for additional input at the end would also solve this problem, but then it would also make it awkward when you actually want to properly run this program from a command prompt.
As for intellisense: Visual Studio 2010 should also be providing intellisense for C++. You might just be dealing with some corrupted project files or something.
-
October 17th, 2010, 10:31 AM
#6
Re: Console exits before I get a chance to see the output
Originally Posted by fsdama
Running it from a command prompt?
So if you didn't have Visual Studio installed, as 90+% of users of Windows do not have Visual Studio, how would you run your program?
Command-line programs are meant to be run from the command-line. If you run it from anywhere else, that's up to the environment you're running from as to what it does with command-line programs.
So the bottom line is this -- if you want to see how your program works, unadulterated and with none of the idiosyncrasies that Visual Studio has, you run it from the command-line.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; October 17th, 2010 at 12:38 PM.
-
October 17th, 2010, 11:58 AM
#7
Re: Console exits before I get a chance to see the output
Originally Posted by fsdama
Hi,
I am quite new to programming and have an issue with running a basic program. Here is the code:
Code:
#include <iostream>
using namespace std;
int main()
{
double f;
double m;
cout << "Enter the length in feet: ";
cin >> f;
m = f/3.28;
cout << f << "feet is"<< m << "meters";
return 0;
When I run the program and enter a number to respond to the prompt and then press enter, the console app exits so I wont get a chance to see the output. What is missing from the code?
Secondly , I am using Visual studio 2010 and I would like to know why is there no intellisense when writing C++ code. I know there is when writing C# code.
Thanks
Before the statement
return 0;
you need to put
system("PAUSE") ;
-
October 17th, 2010, 12:13 PM
#8
Re: Console exits before I get a chance to see the output
Originally Posted by ammanvalley
Before the statement
return 0;
you need to put
system("PAUSE") ;
You don't need to do that. You could, and again it would solve the problem just as making the program wait for additional input at the end would, but it is ultimately just a crutch.
-
October 17th, 2010, 12:55 PM
#9
Re: Console exits before I get a chance to see the output
Personally if you want to run code in the IDE and dont want the window to close on you until commanded to then stick a breakpoint on the last line of main. Much more preferable to using system("pause").
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
-
October 17th, 2010, 11:34 PM
#10
Re: Console exits before I get a chance to see the output
Originally Posted by laserlight
Nothing, except that you should be running your program from a command prompt. That said, in a recent thread, Eri523 observed that "VC++ 2010 Express does exactly that for Win32 console apps (if ran without debugging)", where "that" refers to pausing the console window for you. Therefore, as an alternative you could try creating a Win32 console application project for your program.
Sorry, your link is broken. You most likely meant this thread.
fsdama, yes, VC++ would automatically pause after executing your program if you set up a Win 32 console project. But you have most likely set up a CLR console project. With the correct project type, intellisense would also work. It's a well known flaw of VC++ 2010 that it doesn't for CLR projects.
It should also be noted that VC++ 2010 creates a header line like
Code:
int _tmain(int argc, _TCHAR* argv[])
for your main() function, while in all the general C++ books (and other development packages) this line usually reads
Code:
int main(int argc, char* argv[])
But you can safely ignore this for now.
In addition, to make sample programs from text books work like expected, you should set the "Character Set" setting of your project to "Multi-byte character set" after creating them. To do this, take the following steps:
- Right klick on the project node in the Project Explorer (not the project folder node that is right above the project folder node) and choose Properties from the context menu.
- In the configuration dialog showing up now, select "All configurations" in the "Configuration" field in the top left corner of the dialog.
- In the "Configuration -> General" page (should already show up from the start), set the "Character set" seting to "Use multi-byte character set" instead of the pre-set "Use Unicode character set".
- Click OK and you're done.
Sorry, i know that's tideous... Also, I might not have used the exact terms in all these instructions, as I had to translate them back from the German VC++ version. Maybe someone with an English version can correct me in that case and/or assist with more detailed explanations.
Originally Posted by Russco
Personally if you want to run code in the IDE and dont want the window to close on you until commanded to then stick a breakpoint on the last line of main. Much more preferable to using system("pause").
This is a possible option, but IMO not a good one: When the breakpoint gets hit, the debugger IDE pops in front and the console window gets hidden. It first has to be put to the foreground again to be visible. Particularly annoying for beginners, IMO.
Last edited by Eri523; October 18th, 2010 at 06:56 AM.
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 18th, 2010, 12:24 AM
#11
Re: Console exits before I get a chance to see the output
As for your intellisense issue, try deleting your project's .ncb file. It's probably in the same folder as your project file. That should force VS to regenerate the intellisense. It's an issue that happens from time to time on big projects - I've not come across it on small projects, so I'm not 100% sure it will fix it.
-
October 18th, 2010, 08:12 AM
#12
Re: Console exits before I get a chance to see the output
You can "pause" the screen with this, just before the return point.
Then you're allowed to see it all and when you press a key it will shut down.
-
October 19th, 2010, 07:58 AM
#13
Re: Console exits before I get a chance to see the output
Originally Posted by Eri523
It should also be noted that VC++ 2010 creates a header line like
Code:
int _tmain(int argc, _TCHAR* argv[])
for your main() function, while in all the general C++ books (and other development packages) this line usually reads
Code:
int main(int argc, char* argv[])
But you can safely ignore this for now.
In addition, to make sample programs from text books work like expected, you should set the "Character Set" setting of your project to "Multi-byte character set" after creating them. To do this, take the following steps:
- Right klick on the project node in the Project Explorer (not the project folder node that is right above the project folder node) and choose Properties from the context menu.
- In the configuration dialog showing up now, select "All configurations" in the "Configuration" field in the top left corner of the dialog.
- In the "Configuration -> General" page (should already show up from the start), set the "Character set" seting to "Use multi-byte character set" instead of the pre-set "Use Unicode character set".
- Click OK and you're done.
Sorry, i know that's tideous... Also, I might not have used the exact terms in all these instructions, as I had to translate them back from the German VC++ version. Maybe someone with an English version can correct me in that case and/or assist with more detailed explanations.
Update: I just found out that all that hassle isn't really required at all. Just check the option "Empty project" when creating the Win32 console project. You then can simply use the usual main() function and changing the character set setting isn't required either.
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, 08:24 AM
#14
Re: Console exits before I get a chance to see the output
main is not standardized. UNIX systems have 3 parameters, OSX has 4 parameters.
-
October 19th, 2010, 08:56 AM
#15
Re: Console exits before I get a chance to see the output
Originally Posted by ninja9578
main is not standardized. UNIX systems have 3 parameters, OSX has 4 parameters.
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 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.
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
|