Weird C++ Program Thing?

Home » Programming & Design » Weird C++ Program Thing?
Programming & Design No Comments

When I compile it in my IDE (CodeBlocks), the program runs smoothly, as when I input I get my output. However, when I open the separate .exe file, and input the number it asks me to, the program just closes without giving an output. Any reasons why?

https://gyazo.com/2d7ac0d8264b95638c94f3231369867a

Here's

When I compile it in my IDE (CodeBlocks), the program runs smoothly, as when I input I get my output. However, when I open the separate .exe file, and input the number it asks me to, the program just closes without giving an output. Any reasons why?

https://gyazo.com/2d7ac0d8264b95638c94f3231369867a

Here's the program for reference.

Other answer:

Matthew:
No obvious reason.
I'ld say probably you ran the program in a context in which it doesn't have access to the dynamic library. Then you can for instance see if your IDE has an option for static linking.

Are you sure the program gives no output? Neither or stdout nor or on stderr? Have you launched it from a shell?

husoski:
This is because Windows deletes the console window for a console application as soon as the application terminates. Console programs are supposed to be run from the command prompt or a batch/script file, not from double-clicking an icon.

Because of that, Code::Blocks "wraps" your execution with an outer process that opens the console window, runs your program–as if from a script engine–and then pauses for a keystroke so you can view the output.

You can do something similar with a batch file. In the directory with your executable, create a text file named "launch.bat", with the following lines:

@echo off
myprogram.exe
pause

Then, double-click on the batch file to run it "by mouse", or just use the program name by itself (myprogram.exe or whatever it's called) from the command prompt. You should also use that batch file to run from with Start>Run dialog, since that runs a program just like a double-click in the explorer does.

Laurence I:
text input is a funny thing. it works like a file. you have to understand what a Carriage return is and what a line feed is and what an end of file does. you could try using redirection to take input from a TEXT file eg someapp.exe <input.txt… and see what happens. in short you need to understand what the CONSOLE is.
james l:
" the program just closes without giving an output."

it gives output

it just goes by so fast that you never see it

and then it does a return 0 and exits

so what you could do is put

— your program here —
— and then —
std::cout << "enter anything to exit" << std::endl;
string just_to_keep_open;
std::cin >> just_to_keep_open;
return 0;

LEAVE A COMMENT