PDA

View Full Version : Dev C++ and troubles?



insanomano
05-28-2008, 01:46 AM
ok I am using Dev C++ I am just getting into C++ and im wondering if this is any good?????

and second I am running through a great TUT but whenever I run the code that I make it quickly flashes the command promp. and my code is saved compiled and made into executable format. it just always flashes the cmd and closes. I was wondering WHY????? :mad:

EDIT: YAY!!!!! thanks to wizzup? from another post I found out what the problem was. im gonna post what to do and keep this for other noobs to read!!!!!!

after your cout just insert

int f;
cin >> f; //This makes it so that when you type any key and press enter it closes!

:D Hope this helps anybody else that encounters this error.

boberman
05-28-2008, 01:58 PM
For any that are interested, what is happening is the system opens a console window, preforms the output, then closes the console one the output is done. By putting the input there you stop the console window from closing instantly.

Often, a better solution is to use system("PAUSE"); (I believe it is in the stdlib.h header) rather then cin >> some variable. There are some reasons for this. One is that cin doesn't provide buffer overflow protection. Many a malicious bug plays on this fact. While at the end of a short program might not be a big deal, it is a bad practice that you don't want to be in the habit of doing (if you want to pause your script that is)

Seroko
10-11-2008, 03:59 AM
For any that are interested, what is happening is the system opens a console window, preforms the output, then closes the console one the output is done. By putting the input there you stop the console window from closing instantly.

Often, a better solution is to use system("PAUSE"); (I believe it is in the stdlib.h header) rather then cin >> some variable. There are some reasons for this. One is that cin doesn't provide buffer overflow protection. Many a malicious bug plays on this fact. While at the end of a short program might not be a big deal, it is a bad practice that you don't want to be in the habit of doing (if you want to pause your script that is)

I don't agree, doing a system("PAUSE"); is like bringing in a bulldozer to move a basketball.

boberman
10-11-2008, 05:29 AM
I don't agree, doing a system("PAUSE"); is like bringing in a bulldozer to move a basketball.

Yeah, but putting a cin >> blah is just begging for a buffer overflow. Yeah, its at the end of a program, but still it isn't a good idea to end your programs with a segfault

Bobarkinator
10-11-2008, 03:04 PM
Last time I checked, system("PAUSE") doesn't work on every OS

Yakman
10-11-2008, 03:25 PM
i doubt you'll have the problem of a closing cmd window in a non-windows OS

i support the system("pause")
sometimes i do this, cause on linux its not needed


#ifdef __WIN32__
system("pause");
#endif

Seroko
10-12-2008, 12:09 AM
system("pause"); is such a crazily wasteful function look at what it does:


suspend your program

call the operating system
open an operating system shell (relaunches the O/S in a sub-process)

the O/S must now find the PAUSE command

allocate the memory to execute the command

execute the command and wait for a keystroke
deallocate the memory

exit the OS


resume your program

boberman
10-12-2008, 01:58 AM
system("pause"); is such a crazily wasteful function look at what it does:


suspend your program

call the operating system
open an operating system shell (relaunches the O/S in a sub-process)

the O/S must now find the PAUSE command

allocate the memory to execute the command

execute the command and wait for a keystroke
deallocate the memory

exit the OS


resume your program

Ok, how many times will it be called? once? The only reason this function is used is to stop the output at the end of a program to allow the programmer to analyze what is outputted. So it adds an extra *GASP* 100k onto the memory, You know what, I think any computer built in the last 15 years can handle that. Now if you are programming for a Pentium 1 76 mhz processor with 32k of memory, then you might have a point. But if that is the case you wouldn't even be using C++.

Im sorry, but a single call to a wasteful function who's entire purpose IS TO STOP THE PROGRAM doesn't seem all that bad to me. So cin stops your program that much faster Whoopty doo. I'm sure I could come up with an assembly version of that would stop the program even faster.

Seriously, this is a function that stops a program, Who cares how efficiently it brings the program to a screeching halt? You can't have multiple instances of this function (Well you could, but why on earth would you be calling multiple programming stopping functions?), so the memory it uses is minute. And again, it will be called 99% of the time a grand total of 1 time per program. For beginning programmers, I don't see that as being a big problem. More advanced programs have little need for the user to halt the console so user can read the output. (because they are often doing much more then outputting text to a console).

Wrycu
10-12-2008, 02:26 AM
ok I am using Dev C++ I am just getting into C++ and im wondering if this is any good?????

and second I am running through a great TUT but whenever I run the code that I make it quickly flashes the command promp. and my code is saved compiled and made into executable format. it just always flashes the cmd and closes. I was wondering WHY????? :mad:

EDIT: YAY!!!!! thanks to wizzup? from another post I found out what the problem was. im gonna post what to do and keep this for other noobs to read!!!!!!

after your cout just insert

int f;
cin >> f; //This makes it so that when you type any key and press enter it closes!

:D Hope this helps anybody else that encounters this error.

Ignore the argument going on and use system("pause");

And with Dev C++, you don't need any additional includes to do it.

Seroko
10-12-2008, 02:35 AM
The point is to get people to not be in the habit of using System("pause"); I agree it serves he purpose when testing but if you are calling it fairly often you are wasteful. Its more to not to get into the habit of doing it then anything, so please don't make rude comments because you disagree, google it. Theres thousands of documents that will tell you the exact same thing.

boberman
10-12-2008, 03:07 AM
The point is to get people to not be in the habit of using System("pause"); I agree it serves he purpose when testing but if you are calling it fairly often you are wasteful. Its more to not to get into the habit of doing it then anything, so please don't make rude comments because you disagree, google it. Theres thousands of documents that will tell you the exact same thing.

actually, there are two articles, and yes I have read them both. But based on the system("pause"); arguments, we should never use, VB, C#, java, python, perl, php, ect because they have extra libraries they have to load and that takes memory and time making them "slow". Sorry, but there is a reason these languages exist and have gained popularity, its because computers have gotten fast enough.

Even overused, the fact still remains, the purpose is to completely stop the program. Oh crap, I just wasted 5 processor cycles to stop my program when I could have stopped it in 2. That has degraded my CPU by 0.000000000001 second....

Please don't make uninformed comments based on the first page you googled. Rather, make an informed opinion about something. I didn't insult you at all in my previous post, so if you found that offensive I apologize. However, I don't in anyway conceed that what you are saying is a valid argument, because it isn't. Because what you are saying is that system("pause") makes your programs slower (less efficient as you put it), Well duh, that is kind of the point. Not only that, but 1000 calls to system("pause") wont increase the memory footprint of your program any more then one call will. It adds maybe a couple of bytes onto your program for each time you call it.

BTW, on the first link the argument about the increase in executible size because of the added <cstdlib> is laughable at best. generic C++ program without any includes.. 3.0kb With cstdlib and system("pause"); 3.3kb. Yeah, that .3 kb is just going to kill a lot of people.

Yakman
10-12-2008, 09:48 AM
system("pause"); is such a crazily wasteful function look at what it does:


suspend your program

call the operating system
open an operating system shell (relaunches the O/S in a sub-process)

the O/S must now find the PAUSE command

allocate the memory to execute the command

execute the command and wait for a keystroke
deallocate the memory

exit the OS


resume your program


i can also use google

"premature optimization is the root of all evil"

Enchanted
11-01-2008, 09:28 AM
for(;;) std::cin.get();

Ftw.