![]() |
|
[C] Console application closes right away - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C] Console application closes right away (/showthread.php?tid=5811081) Pages: Pages
1
2
|
Console application closes right away - Camf ∆ - Mar 13, 2018 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 ()It stopped working with this, Code int main ()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 😉 RE: Console application closes right away - hello98321 - Mar 13, 2018 To fix the closing put system("pause"); at the end of your source code before return RE: Console application closes right away - 0xDEAD10CC - Mar 13, 2018 (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. RE: Console application closes right away - Camf ∆ - Mar 14, 2018 (Mar 13, 2018 10:51 PM)0xDEAD10CC Wrote: In all honesty that is terrible advice. -- ahhhh, okay that makes sense. Thank you very much 🤝 RE: Console application closes right away - 0xDEAD10CC - Mar 15, 2018 (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. RE: Console application closes right away - Gbulla - Mar 16, 2018 (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. RE: Console application closes right away - Hoodie+ - Mar 16, 2018 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. RE: Console application closes right away - 0xDEAD10CC - Mar 16, 2018 (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. "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. RE: Console application closes right away - Hoodie+ - Mar 16, 2018 (Mar 16, 2018 10:22 AM)0xDEAD10CC Wrote: "If you change the subsystem to console the console will stay after it outputs the text" Well, what is your explanation on my console staying open then? Spoiler (Click to View) RE: Console application closes right away - Camf ∆ - Mar 16, 2018 (Mar 16, 2018 11:27 AM)Hoodie+ Wrote: Well, what is your explanation on my console staying open then? How do I go about this? RE: Console application closes right away - wit - Mar 16, 2018 system("pause") works perfectly fine if doing this for school related projects RE: Console application closes right away - Camf ∆ - Mar 16, 2018 (Mar 16, 2018 07:04 PM)Hyphonix 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? RE: Console application closes right away - Gbulla - Mar 16, 2018 (Mar 16, 2018 07:05 PM)Camf ∆ Wrote: I'm actually learning C++ to dive deeper into Block Chain Development. 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. RE: Console application closes right away - Zettabit - Mar 17, 2018 (Mar 16, 2018 11:27 AM)Hoodie+ Wrote: Well, what is your explanation on my console staying open then? Are you running it inside your IDE? RE: Console application closes right away - Hoodie+ - Mar 17, 2018 (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. |