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 10:51 PM
(This post was last modified: Mar 13, 2018 10:53 PM by 0xDEAD10CC.)
(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.
(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.
DO NOT DEAL WITH THIS MEMBER
(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.
(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?
![[Image: ijraXga.jpg]](https://i.imgur.com/ijraXga.jpg)
(Mar 16, 2018 11:27 AM)Hoodie+ Wrote: Well, what is your explanation on my console staying open then?
![[Image: ijraXga.jpg]](https://i.imgur.com/ijraXga.jpg)
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.
DO NOT DEAL WITH THIS MEMBER
(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?
![[Image: ijraXga.jpg]](https://i.imgur.com/ijraXga.jpg)
Are you running it inside your IDE?
(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.
|