Hey guys,
I am learning C++, and I was wondering why my console programs close right after they open? I am using Dev C++, and right after I compile, my program will close out. It doesn't even leave me time to read it. Thanks for your answers.
Code::Blocks has a special console wrapper that avoids this, and it's a lot better than the old outdated Dev-C++. Use that.
If you don't want to switch for some reason, use std::cin.get(); at the end of your program.
^^^
The program runs, then closes when it's done.
So, at the end, add the following:
sleep(X)
Where X is the number of milliseconds
Or:
system("pause")
[QUOTE=Eudoxia;16330811]The program runs, then closes when it's done.
So, at the end, add the following:
sleep(X)
Where X is the number of milliseconds
Or:
system("pause")[/QUOTE]
Don't do this.
Use cin.get() or getchar().
[QUOTE=gparent;16329333]Code::Blocks has a special console wrapper that avoids this, and it's a lot better than the old outdated Dev-C++. Use that.
If you don't want to switch for some reason, use std::cin.get(); at the end of your program.[/QUOTE]
Yeah, I would recommend Code::Blocks or VC++. Dev C++ is old and ugly.
[editline]02:42PM[/editline]
[QUOTE=Eudoxia;16330811]The program runs, then closes when it's done.
So, at the end, add the following:
sleep(X)
Where X is the number of milliseconds
Or:
system("pause")[/QUOTE]
System commands are bad practice, they're Window's only.
[QUOTE=Eudoxia;16330811]The program runs, then closes when it's done.
So, at the end, add the following:
sleep(X)
Where X is the number of milliseconds
Or:
system("pause")[/QUOTE]
Brace for shitstorm.
[QUOTE=Spartan117;16330865]System commands are bad practice, they're Window's only.[/QUOTE]
They aren't even Windows-only.
You have no idea if "pause" is even going to exist in future versions of Windows. You should never call a second application for something that can be done easily without it.
[QUOTE=Eudoxia;16330811]The program runs, then closes when it's done.
So, at the end, add the following:
sleep(X)
Where X is the number of milliseconds
Or:
system("pause")[/QUOTE]
OS-dependency = baaaaaad, mmkay?
I'm aware, it was just a suggestion (Someone already posted the cin.get() instruction(
[QUOTE=Eudoxia;16333323]I'm aware, it was just a suggestion (Someone already posted the cin.get() instruction([/QUOTE]
He's new to programming though and doesn't know what they do though. At least explain why cin.get() is better so he doesn't develop bad habits, mmkay?
Who's saying that writing for a specific OS [b]isn't[/b] bad?
You want to open your program to everyone possible.
[QUOTE=Klownox;16338981]Who's saying that writing for a specific OS [b]isn't[/b] bad?
You want to open your program to everyone possible.[/QUOTE]
Writing for a specific OS isn't inherently bad, sometimes your program is only useful on one OS or one family of OSs, other times it could rely on an OS specific feature. The system function isn't a "bad" function, but it spawns some bad coding patterns, like using system("pause") to pause execution.
system("pause") uses relatively lots of resources to open a new process (namely the "pause" command), then it waits for this process to end before it continues. Both the C and C++ standard libraries (and underlying API's, like the Windows API) offers ways of waiting for input before continuing execution without forking a new process, and thus uses much less resources and CPU time to do this. Since performance generally isn't an issue when you're waiting for a keypress; you end up with the fact that using the standard library your code doesn't rely on an environment specific feature. Using the system function also means that it will silently fail; if using system("pause") is the only reason your program doesn't work on the users environment, it will still compile just fine, but when you run it, the system function suddenly returns immediately instead of waiting! Proper use of the system function include checking its return value to see if it succeeded, but then there's no point because the getchar and cin.get functions don't require this and are guaranteed to work.
[QUOTE=Klownox;16338981]Who's saying that writing for a specific OS [b]isn't[/b] bad?
You want to open your program to everyone possible.[/QUOTE]
Didn't you call someone close-minded in another thread?
[QUOTE=efeX;16340265]Didn't you call someone close-minded in another thread?[/QUOTE]
And?
I would think that trying to keep programs working on all Operating Systems, if possible, would be the best thing for a programmer to do.
Keeping in mind what ja_cop said.
Does anyone know how prevent it to close instead of using 2 cin.get(); after each other? Does it even matter if you do it?
[QUOTE=Swebonny;16347975]Does anyone know how prevent it to close instead of using 2 cin.get(); after each other? Does it even matter if you do it?[/QUOTE]
If you need 2 gets then there's something left in the stream. I'm guessing you read in an integer?
Thanks for the answers guys, sorry I didn't get back sooner, I was busy today. I will probably use cin.get(), infact I remember seeing that but didn't realize to use it. I do want to practice good habits, and don't want to close off other future os's. I probably will also get code::blocks.
:clint:
That's a good man.
if you use pause in other editors/ compilers , using the stdlib.h library and using system pause I.g.
#include <iostream>
#include <stdlib.h>
Using namespace std;
Int main(){
Cout<<"hello world";
System("PAUSE");
Return 0;
}
It will pause anytime the function pause is used and will only continue after you have pressed a key.
edit:
late to the party .....by a couple of years , and the code is bugged...please ignore this post, Thank You
[QUOTE=Redaer;44931994]Another option would be, using the stdlib.h library and using system pause I.g.
#include <iostream>
#include <stdlib.h>
Using namespace.std;
Into main(){
Cout<<"hello world";
System("PAUSE");
Return 0;
}
It will pause anytime the function pause is used and will only continue after you have pressed a key.[/QUOTE]
Your code won't work, and you bumped a thread from 2009. GJ
sorry I realized it after I posted, is their any way to remove the post it showed up on my phone 's browsers in the first couple of pages[QUOTE=Asgard;44932026]Your code won't work, and you bumped a thread from 2009. GJ[/QUOTE]
[QUOTE=Redaer;44932037]sorry I realized it after I posted, is their any way to remove the post it showed up on my phone 's browsers in the first couple of pages[/QUOTE]
There's no way to delete the post. Just let this thread die.
[QUOTE=-Matt-94;16329264]Hey guys,
I am learning C++, and I was wondering why my console programs close right after they open? I am using Dev C++, and right after I compile, my program will close out. It doesn't even leave me time to read it. Thanks for your answers.[/QUOTE]
Most languages do this. Assuming you're using basic console applications, put System ("pause"); at the end of your code. This tells it to wait for an input before going on to the next step. consequently, if you put it before your code, it would wait for input before starting
edit: oh fuck me with a rake
edit2: well for posterity: system accesses the windows console which takes batch commands. It's frowned upon because closed-platform programming means that if a user wants to run your program on, say, linux, they'll have to go through and remove closed-platform code. However, since the OP is just starting out and is probably practicing with simple I/O console applications, it's totally fine to use system("pause"); in order to stop the program before closing. Just be aware in the future of other platforms.
Sorry, you need to Log In to post a reply to this thread.