Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

Console application closes right away

Hey guys, I am going through the beggining stages of C++.

I ran into a few problems, and got most of them sorted but I'm still stuck with the error that once it runs all of the line of code the application closes.

I did some searching and found a fix using "cin.get()" but as I'm going through tutorials it's not working any more.


It works with,

Code
int main ()
{

cout << "Hello World! \n new line";
cin.get();
return 0;
}


It stopped working with this,

Code
int main ()
{
    int a;
    int b;
    int sum;

        cout << "Enter a number! \n";
        cin >> a;
    
        cout << "Enter Another number";
        cin >> b;

        sum = a + b;

        cout << "the sum is " << sum << endl;

cin.get();
return 0;
}

How can I fix this closing issue permanently?

I am also curious what exactly cin.get() does and what makes it a temporary fix?


Thanks everyone! I look forward to joining this section over the next few weeks 😉
Just a hippie in his bag.
To fix the closing put system("pause"); at the end of your source code before return
(Mar 13, 2018 09:23 PM)hello98321 Wrote:  To fix the closing put system("pause"); at the end of your source code before return

In all honesty that is terrible advice. --

I'd suggest him learning how to actually program and understand that from the input provided by the user there's a left behind '\n' in the buffer that cin.get() grabs right before the return 0... This is because it isn't needed to be extracted when the integer is parsed out from the data in the stream. system("pause") is the very LAST thing he should do, if that.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(Mar 13, 2018 10:51 PM)0xDEAD10CC Wrote:  In all honesty that is terrible advice. --

I'd suggest him learning how to actually program and understand that from the input provided by the user there's a left behind '\n' in the buffer that cin.get() grabs right before the return 0... This is because it isn't needed to be extracted when the integer is parsed out from the data in the stream. system("pause") is the very LAST thing he should do, if that.

ahhhh, okay that makes sense. Thank you very much 🤝
Just a hippie in his bag.
(Mar 14, 2018 12:50 AM)Camf ∆ Wrote:  ahhhh, okay that makes sense. Thank you very much 🤝

system() under any circumstance except NULL passed in is undefined behavior.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(Mar 13, 2018 09:23 PM)hello98321 Wrote:  To fix the closing put system("pause"); at the end of your source code before return

This is the horrible advice. Don't listen to this.

@OP:
The reason your program stops is because you don't tell it to keep running.
Think like a programmer.
Didn't see any good answer at all. Since the Window closes you are most likely using an IDE which opens a new console window each time the program is run.
My guess is that you did not specify the subsystem od the application in your IDE. For example in Visual Studio, when you create a new Empty project the subsystem will not be set and if you make a hello world program the console will close as soon as it outputs the text. If you change the subsystem to console the console will stay after it outputs the text.
(Mar 16, 2018 02:14 AM)Hoodie+ Wrote:  Didn't see any good answer at all. Since the Window closes you are most likely using an IDE which opens a new console window each time the program is run.
My guess is that you did not specify the subsystem od the application in your IDE. For example in Visual Studio, when you create a new Empty project the subsystem will not be set and if you make a hello world program the console will close as soon as it outputs the text. If you change the subsystem to console the console will stay after it outputs the text.

"If you change the subsystem to console the console will stay after it outputs the text"

That's not true. A console application still doesn't have any message loop so there's no reason for it to stay open after.
Getting Started: C/C++
[C/C++/.NET/x86 ASM - Automation Programmer]
(Mar 16, 2018 10:22 AM)0xDEAD10CC Wrote:  "If you change the subsystem to console the console will stay after it outputs the text"

That's not true. A console application still doesn't have any message loop so there's no reason for it to stay open after.

Well, what is your explanation on my console staying open then?

Spoiler (Click to View)
(Mar 16, 2018 11:27 AM)Hoodie+ Wrote:  Well, what is your explanation on my console staying open then?

Spoiler (Click to View)

How do I go about this?
Just a hippie in his bag.
system("pause") works perfectly fine if doing this for school related projects
!
(Mar 16, 2018 07:04 PM)wit Wrote:  system("pause") works perfectly fine if doing this for school related projects

I'm actually learning C++ to dive deeper into Block Chain Development.

What makes System("pause") a temp fix?
Just a hippie in his bag.
(Mar 16, 2018 07:05 PM)Camf ∆ Wrote:  I'm actually learning C++ to dive deeper into Block Chain Development.

What makes System("pause") a temp fix?

The system() function is exclusive to windows, as it calls a windows commandline function. It will not be able to cross-compile.
Not to mention, it hogs a shitload of system resources and is extemely inefficient.
Using std::cin.get() is a much better method.
(Mar 16, 2018 11:27 AM)Hoodie+ Wrote:  Well, what is your explanation on my console staying open then?

Spoiler (Click to View)

Are you running it inside your IDE?
шкідливе ПО для ентузіастів

Jabber: [email protected]
[email protected]
(Mar 17, 2018 12:27 AM)Zettabit Wrote:  Are you running it inside your IDE?

Of course I am.
Who would use an IDE and not run his app in it.

On the other end if you want the end user to use your appliaction, any normal end user will open up cmd or powershell, cd to the program's directory and executing the program that way and they don't have to worry about the console closing.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by absojediMay 17, 2019 12:17 PM 1 178 May 17, 2019 12:17 PM
Last Post: StarZ
[C#] by VelvetCodingApr 17, 2019 09:39 PM 1 203 Apr 17, 2019 09:39 PM
Last Post: SIM-3991524
[Python] by VIXVApr 7, 2019 08:25 PM 9 260 Apr 7, 2019 08:25 PM
Last Post: red