visual studio 2010 issue - c++

When I write a program using C++ and I want to run it, I can't catch the console window. I press CTRLF5 and it does not work.
I want the window to stay open and wait, even it finishes executing. Can anyone help me?
Thanks in advance.

http://connect.microsoft.com/VisualStudio/feedback/details/540969/missing-press-any-key-to-continue-when-lauching-with-ctrl-f5
In the older versions it would default to the console subsystem even if you selected "empty project", but not in 2010, so you have to set it manually. To do this select the project in the solution explorer on the right or left (probably is already selected so you don't have to worry about this). Then select "project" from the menu bar drop down menus, then select "*project_name* properties" > "configuration properties" > "linker" > "system" and set the first property, the drop down "subsystem" property to "console (/SUBSYSTEM:CONSOLE)". The console window should now stay open after execution as usual.

try using system("Pause"); as the last line on your code (before the return of your main function)

Ctrl+F5 should work. Just in case, if you have the source of your program, add the following just before the closing brace of main.
int x;
cin >> x;
the program will wait for you to enter some value.
If you want a breakpoint to be triggerred in debugger, do simple F5 instead of Ctrl+F5, after putting a breakpoint on the relevant source line (assuming the source/debug symbols are available)

Sorry to say, Ruba, but it looks like Microsoft removed this nifty little feature when moving from VS2008 to VS2010.
I can't find anything on MSDN, the web in general, or VS options to turn it back on.
My advice is to bypass the environment altogether for testing your application. Simply open a cmd.exe window in your runtime directory (debug or release or whatever), build the executable within the IDE then switch to the command window and enter testprog.exe to run your program.
Make sure you include any required command line parameters and, after you've entered it the first time, you can just use the up-arrow to retrieve the last command.
Yes, it's a bit of a pain but, until someone comes up with a better solution, it's probably the best way to ensure you see all the output while ensuring the program has shut down completely.

Just set a breakpoint at main()'s closing curly brace if you want to see the console after the program is finished.

You should create VS 2010 C++ Projects as below:
New project -> Visual C++ -> Win32 -> Win32ConsoleApplication
In this way you will be getting "Press any key to continue..." when you run program with ctrl+F5, as it was in VS 2008.
EDIT :
New project -> Visual C++ -> Win32 -> Win32ConsoleApplication -> Next -> Check 'Empty project' -> Finish = what you actually need.

Related

Is my application runs from inside Visual Studio vs. by executing an EXE file

As I often test my binaries inside/outside Microsoft Visual Studio 2017, I want to control the behavior of my code in C/C++ console projects.
One for code for when I run .exe from within Visual Studio in Release mode.
Another when I just click my .exe from Explorer.
What flag or function should I use to know if my .exe was started from inside Visual Studio or not.
What I would like to achive is the:
#if !_RELEASE
system("pause"); // prevents auto shutdown of my .exe in Explorer
// double click
#endif
where _RELEASE is some kind of trait that triggers code in Studio launches,
but not visible in Explorer double click.
What flag or function should I use to know if my process was startded from inside Visual Studio or not.
You shouldn't do such behavior control from inside your program code. That's bad design, and clutters your program code with decisions that should be left on the caller.
I'd recommend if you need different behaviors of your program (e.g. running in background or with visible GUI), this should be controlled with e.g. configuration files or command line parameters.
You can do that for both, Visual Studio settings to specify cmd line parameters, or using a different configuration file, or even a combination of both.
As you seem to insist for a solution of your idea how to fiddle with this in the best way:
You can use the WINAPI functions to iterate through your parent process IDs and check if one of these is matching the "Visual Studio" module.
Here's a Q&A which links to the technique:
How can I reliably check whether one Windows process is the parent of another in C++?
It not exactly solution, but:
Raymond Chen(Microsoft winapi guru*) is most close in spirit to the problem I facing, helping me detect in what mode or circumstances I run my console session.
How can I tell whether my console program was launched from Explorer or from a command prompt?
printf("this process = %d\n", GetCurrentProcessId());
DWORD count = GetConsoleProcessList(nullptr, 0);
if (count == 1) {
printf("I'm the last one!\n");
Sleep(2000);
}
else {
printf("I'm not the last one! %d\n", count);
}

New to Visual Studio - Can't see output

I have to do a programming project for an optimisation class, that has to be written in C or C++. So I'm trying to figure out Visual Studio 2015. I created a blank project, and opened a new C++ file, where I have the following:
#include <iostream>
int main()
{
cout >> "Hello World!/n";
cin.get();
return 0;
}
When I run it, I get a large blank white popup, and nothing else happens, even if I hit various key on the keyboard or wait for several minutes. It looks like this:
If I close the large popup, nothing happens. What Visual Studio refers to as output from build looks like this:
1>------ Deploy started: Project: LinearProgramming, Configuration: Debug Win32 ------
1>Updating the layout...
1>Deployment complete (157ms). Full package name: "53acc796-5708-4314-9034-f2a1f840a4f4_1.0.0.0_x86__eazt3av84y7ym"
========== Deploy: 1 succeeded, 0 failed, 0 skipped ==========
Could anyone please explain to me what's going on? How can I create a simple C++ project in Visual Studio 2015 and run it?
I believe you selected the "Blank App(Universal Windows)" project template, which is like a Microsoft store application. What you probably want is the "Win32 Console Application" template, which creates an application without its own window (and uses the console for I/O).
This is how I create a Win32 Console Application.
Go to File->New->Project...
Select Visual C++->Win32->Win32 Console Application and name the project.
In the Win32 Application Wizard press the Next > button and be sure Console application and Empty project are selected, and then hit the finish button.
In the Solution Explorer right click on Source Files and select Add->New Item...
Select the C++ File (.cpp), name it, and hit the Add button.
After you change the >> to << add the code above and it should run.
The quick answer is to hit ctrl-f5 to open the console. After running your script and showing your output it waits for you to hit a key before closing the console.
I eventually found the answer to the question which can be seen at How to keep the console window open in Visual C++?

How to do line by line debugging in Code::Blocks IDE?

I am using Code::Blocks IDE which is open source IDE providing multiple languages.
It is using GCC compiler.
I want to do line by line debugging of program.
Have you any about that?
You can use the "Step Into" command in the "Debug" menu which should start debugging and stop at the first line. Then continue through using the "Next Line" command (also in the "Debug" menu).
If that doesn't work as intended, you can set a breakpoint (by clicking in the left 'gutter', or 'margin') at the first line of your app, and start the debugger from the "Debug" menu, and then use the "Next Line" command in the "Debug" menu.
The shortcut keys vary based on your settings but should be listed alongside the menu command, and makes 'step'ping easier.
Since you're using gcc to compile, you can specify the -g parameter to include debugging symbols, and invoke gdb from the a command shell with the compiled binary as an argument: gdb <yourapp>.
(If [n]curses is installed, specify -tui for a more pleasing interface: gdb -tui <yourapp>.
Once in gdb, the command start will start debugging and stop automatically at main(). You can then step thru with the step command, and quit to exit.
You can always man gdb...
GCC's optimization sometimes makes debugging not easy. To improve your debugging experience, make sure you set optimization to off or to a low level via -O0 or -O1.
Additionally, make sure you have all debug information included in the binary: -g3.
Please go through these steps below:
At first click on debug Menu bar : (Debug-> Debugging windows-> Watches). Now your debugging window is on and the window will be shown at the left corner.
Add breakpoint just clicking left portion of the mouse at those lines you want to debug or test.
Again click on (Debug-> start/continue) It will show a console window. Put input on it. Now press Enter button.
Click on (Debug-> Next line) or press F7 for line by line debugging.
Happpy Coding !

Visual Studio .exe files open and immediately close

I have a problem, when I try to open my Hello World.exe file (that I created by following a tutorial). It immediately closes without giving me the chance to read or see if I have done everything correctly.
As you can see, I need help on how to keep it open, without instantly closing.
You can either put a break point before the end of main or try the following:
int main()
{
//...
std::cin.get();
return 0;
}
It is going to wait for you to press some key to exit the console.
EDIT: It is better to add break point which do not change existing code.
In console applications there are a couple of things you can do to stop the window from closing on you such as using system("pause") (not so recommended though), getch(), std::cin >> x etc at the end of the application.
Another option is to start a cmd window, cd to the location of the exe and run it like any other console application is meant to be ran, that way it wont just close on you, it'll simply exit.
In VS2017, you can specify that the executable is a Console app, not a Windows app. This makes your application run in a "Microsoft Visual Studio Debug Console", with "Press Any Key To Close This Window" appearing at the end of the console output:
Right click the project to bring up Properties.
Linker > System > Subsystem > Select "Console".
Press Apply before closing.

Starting a hidden C++ program

I am creating a C++ program with Visual Studio 2010 that is supposed to run on the background of my machine.
Therefore when I start it, I shouldn't see the CMD screen while it is running. How can I do this? Do I have to use the Win32 API or a normal C++ program will suffice?
Please note that my program has no GUI at all.
Use WinMain() :
#include <windows.h>
int WINAPI WinMain(HINSTANCE inst,HINSTANCE prev,LPSTR cmd,int show)
{
// program starts here
return 0;
}
// int main() <-- remove main()
Then ake sure your project settings are set so that you build a "Win32" program and not a "Console" program.
Edit: As #Sehe points out, winMain may not be necessary, although I am not quite sure where this option lies.
Run it as a service See here.
Very often a program running under Windows without a "face" (eg, with no user interface at all) is implemented as a Service.
In windows, Daemon programs are implemented as Services.
Simply make it a GUI application instead of a command line application. Right click the project -> Properties -> Configuration Properties -> Linker -> System -> SubSystem -> Windows (/SUBSYSTEM:WINDOWS).
You can run as a Windows process (which doesn't attach to a console), but never create a window. the main difference is the signature of WinMain, and the flags to the compiler.
Go to File -> New Project, select the standard Windows Application, then delete everything except WinMain.