Hello. I'm new to these forums much like I am to C++. I'm likely to have many novice questions, and I apologize in advance for them.
So, I'm just starting out with some beginner code - cout << "Never fear, C++ is here!"; - and I have already encountered what I suspect is a problem.
I'm using Microsoft's Visual Studio compiler and I copied the code exactly as it is shown in my book. When I press "build" it initiates like it is running, but everything closes and nothing happens. When I click Debug it also closes the whole program.
I'm not sure what is happening here. Does anyone have a clue?
Try going to the menu bar and under the Debug menu select Start without debugging. The likelihood is that the command window closes after your program finishes, Start without debugging will hold the window open so you can see the results.
If this is the problem, read this article :
http://www.cplusplus.com/forum/articles/7312/
Yeah, that didn't work either. I will read the article, though I'm not sure if I will benefit from it. I'm a newbie, afterall. I'll see what I can glean from it.
Can you post the full code? If you use the code tags it will be displayed neatly, i.e. type [code], then paste you code, then close the tag with [/code].
Out of interest, what version of VS are you using and what book?
As masiht, sorry jayt, suggested; you can put the line system("PAUSE"); in your code just before the return 0;. This basically asks the system to run the batch file command 'pause', keeping the window open until it returns from this call. The use of system calls in this manner is generally considered bad practice and not a habit you should get into.
If you did use the Start without debugging menu option and the console window did not stay open, then you have another problem.
I notice that you have #include <stdafx.h> in your code, what type of project did you create?
Please don't instruct people to use system commands! System commands are evil (even though I use them anyway...). Use cin.get(), the user can press enter to exit.
I only ever use system() because I have to or if I'm making a Windows-specific app. For example, if I were to 're-write' cmd (which I was going to do, but thought it would be pointless and take too long) using system() commands (which is essentially what cmd does - it passes the parameters you enter to the application you type in, e.g. passing '-s -t 0' to 'shutdown.exe' or similar).
Greywolf: - I didn't notice the dates either. It's only 10 days anyway.
Computerquip - The only reason I didn't do that was really for other commands. Can you think how we could have achieved ASSOC? (changes file associations) or shutdown? I mean, you could do system("shutdown"); but then you'd need to have some way for the user to pass their own parameters to the program.
It's "Press any key to continue..." anyway. Can you imagine Microsoft saying please?
You could do this:
cout << "Press any key to continue...";
char i=getch();
switch (i) {
case VK_[key here]:
break;
break;
default:
break;
break;
}
By the way I'm 15 today, and have been for an hour and 45 minutes (it is 1:45 AM here. I'm bored and can't sleep.) Where are my presents, guys?