Why c++ exe files closes immeditely? [duplicate] - c++

This question already has answers here:
What is the Best Practice for Combating the Console Closing Issue?
(11 answers)
Closed last month.
I have 1 simple code in c++. That code calculate sum of 2 entered numbers. I convert that code to .exe for running windows. My problem is it will work but it doesn't show sum. After entering second number program closes immediately. I have no idea why this happen.
Here is my code:
#include <iostream>
using namespace std;
int main()
{
int a, b;
cout << "Please enter first number \n";
cin >> a;
cout << "Please enter second number \n";
cin >> b;
int z = a + b;
cout << "Sum of these numbers are: " << z;
}
I find solution via command prompt. But I want to completely run my .exe file without command prompt

Closing the console after program finishes is a normal behaviour when you don't start it from the terminal. You could delay the closing of console by using
std::cin.get(). The function waits until recieving input from terminal, so when you press any key, the program will end and console will close

Related

When I run the program in DevC++ 5.11 TDM-GCC 4.9.2 doesn't work how I expect, but in an online compiler it just works [duplicate]

This question already has answers here:
Uninitialized variable behaviour in C++
(4 answers)
Closed 2 years ago.
I'm a newbie in the programming world, and i've decided to start with C++ code a few days ago as my first programming language.
I just started to read an online course which i'm guiding on (and aply while i'm reading it).
The course in question assigns a serie of small optional exercises, wich go hand in hand with the topic that is being dealt at that moment.
One of this optional exercises is: "Create a program that multiplies two whole numbers in the following way: it will ask the user for a first whole number. If the number that you type is 0, it will write on the screen "The product of 0 by any number is 0". If a number other than zero has been entered, the user will be prompted for a second number and the product of both will be displayed."
How it says, I did my best to coding that program.
The code of what I did is:
#include <iostream>
using namespace std;
int main ()
{
int a;
int b;
int solve;
cout << "Enter a number: ";
cin >> a;
if (a!=0)
{
cout << "Enter another number: ";
cin >> b;
}
if (b!=0)
{
solve = a * b;
cout << "The result of your operation is: " << solve << endl;
}
else cout << "The product of 0 by any number is 0." << endl;
return 0;
}
So, I press F9 to compile, then F10 to run it.
I proceed to testing it.
I put and different number from zero, I put another one. Throws me a multiplication of both. Nice.
I put and different number from zero, I put another one that actually its zero. Throws me the message of "else" order. Nice.
**BUT
I put and number equal to zero, and throws me the message of the "cout" order of "if (b!=0)".**
I didn't really know what I had done wrong, so, I ask for help from a friend who has some more experience than me, and tells me that actually it's nothing wrong. In fact, he proved me sending to me a screen cap of his Dev C++ with my code in it, and how it runs just how it had to be.
Then, I opened an online compiler (https://www.onlinegdb.com/online_c++_compiler#) to get down my doubts, and yes, there runs correctly too.
So, here's my question?
What's the problem? Why that happens?
I have the DevC++ 5.11 TDM-GCC 4.9.2, and I'm using the deafult compiler.
Please, I would like some of help, I feel more comfortable compiling in PC than online, it's more quick.
Thank you anyway for reading until here.enter image description here
The reason why this happens, as #TrebledJ mentioned, is that you've not initialised the variable b. So you can get over this problem just by initialising b to any value of your choice (preferrably 1 or 0 for simplicity). But there's a workaround if you don't want to initialise the values. I would transform your code to something like this:
if (a!=0)
{
cout << "Enter another number: ";
cin >> b;
if (b!=0)
{
solve = a * b;
cout << "The result of your operation is: " << solve << endl;
}
else cout << "The product of 0 by any number is 0." << endl;
}
else cout << "The product of 0 by any number is 0." << endl;
Basically, what I'm doing is, if the value of a after input is 0, I'm not running the part for taking the user input of b.

[C++]Eclipse ignores console input during debugging

During debugging Eclipse doesn't "see" INPUT from built-in console, just ignores it. Simple example:
#include <iostream>
using namespace std;
int main() {
int a;
cin >> a;
cout << a << endl;
cin >> a;
cout << a << endl;
return 0;
}
works perfectly fine when just run, but when I try to debug it, first "data on input"(?) is always a number around 40, and only zeros next, no matter what i will write to console.
So program executes, first variable is set to ~40 and all next to zeros.
Output works fine, values are written to console, only input doesn't work.
I work on Windows 10 and use MinGW.
Thanks in advance.
#EDIT
Everything works, when I use native Windows console
(.gdbinit file with set new-console on line)

C++ Code to calculate sum of unknown number of numbers

Im trying to write a code in c++ that takes an unknown number of numbers and adds them all together. I do not get any errors but when i input numbers it wont do anything.
#include <iostream>
int main()
{
int sum = 0, val;
while (std::cin >> val)
sum += val;
std::cout << "Sum is: " << sum << std::endl;
return 0;
}
My guess is you never ended your input. It will continue looping and adding numbers until you trigger end of file. Just pressing enter won't do that - you need to hit ctrl+d on Linux or ctrl+z on Windows to end the standard input file, allowing the while loop to exit.

"logout" word on every program

When I write every C++ program, such as that one:
#include <iostream>
#include <string>
using namespace std;
int main()
{
int n;
cout << "Tell me where to start: ";
cin >> n;
while (n>0) {
cout << n << "\n";
n = n-1;
}
cout << "FIRE!";
return 0;
}
I compile it with G++ and, when I run it, it works well, but when it finishes it displays the "logout" word after the program's last word, like this:
Tell me where to start: 10
10
9
8
7
6
5
4
3
2
1
FIRE!logout
[Process completed]
Why? And how can I remove it?
It's not from your program. It's because the terminal is opened with the sole purpose of running your program, and as such when it exits, the terminal shuts down.
If you open a shell and manually run your executable, instead of this message, you'll simply get another command prompt.

Visual C++ holding the console open, strange behavior

I've searched around here first, and seen many solutions to making Visual C++ hold the console open at the end of program's execution, but I'm getting some weird behavior from one of the methods, and I don't understand where this behavior is coming from.
So (a) what's the best method (besides crtl-F5 and setting subsystem to console) to hold the console open?
and (b) why am I getting the strange behavior that I will now elaborate on?
Here is the code
/*
Learning how about variables and accepting inputs
*/
#include <iostream>
using namespace std; // cout and cin from the standard namespace.
// using std::cout; -- alterative declarations
// using std::cin;
int main()
{
char first, last;
cout << "Please enter your first and last names:\n";
cin >> first; // get one char
cin.ignore(256,' ');
cin >> last; // get one char
cout << "Your first and last initials are " << first << last;
cin.ignore('\n');
return 0;
}
The strange behavior is, I have to press the Enter exactly 4 times before it will exit the console.
Why 4 times?