[ 3 / biz / cgl / ck / diy / fa / ic / jp / lit / sci / vr / vt ] [ index / top / reports ] [ become a patron ] [ status ]
2023-11: Warosu is now out of extended maintenance.

/sci/ - Science & Math


View post   

File: 46 KB, 400x518, WrongProgramming.jpg [View same] [iqdb] [saucenao] [google]
4829693 No.4829693 [Reply] [Original]

So I picked up a C++ book from the library and downloaded Dev C++ as my compiler (bloodshed.net).

I'm into the third chapter, and I found a way around my problem, but it's just becoming too bothersome.

Once I've written the code for the example problems and they've compiled just fine, I go to run them, but the application window only stays open for a split second. I know the code has worked fine because if I hold the key down a bunch of windows pop up quickly and it allows me to see that what is supposed to be printing to the screen is, in fact, showing up. I just can't get it to STAY up (haha).

As an aside, I've been writing a 'quit' sequence into the programs just to keep them up.

The book is old, "C++ From the Ground Up" By Herbert Schildt.

Is it the code? The compiler? I feel like something isn't clicking because no mention of this has been made in the book.

>> No.4829703

you have to include

system("PAUSE");

in your code.

>> No.4829704

or you could request a character as input in order to pause the system.

I think getch() works, not sure if it's c or c++

>> No.4829708
File: 21 KB, 438x438, brofistcover.jpg [View same] [iqdb] [saucenao] [google]
4829708

You're probably compiling command-line programs; this is basically the default setting, and it's useful because you can use keyboard input and output functions like cin, cout, stdin, stdout, etc.

When you run a command-line program directly from your IDE (in your case Dev C++), it will first open a command-line interface to run your program. It will then usually close the command line interface the second your program terminates, effectively hiding the output from you.

If you ran your program directly from the command line (e.g. what you would typically do if you were developing in Linux with make-files and whatnot), the command line screen would obviously not close the second your program finishes, since you originally CALLED your program from the command line :)

An easy way around this problem is to add a call to an input function, such as getch, cin, or stdin, and the very end of your program. Until you press enter (or in the case of getch, press any key) the program will keep running and wait for your input, leaving the command line interface open long enough for you to see the input :)

Happy coding!

>> No.4829709

Run your programs from a command window. Whenever windows opens a program that uses a command window it closes it right away; I'm not sure if its possible to turn it off.

>> No.4829716

Aside from what others in this thread have said, you can also try using an IDE with a build-in command line so you don't lose your output. I suggest Eclipse, NetBeans, or Microsoft Visual Studio.

>> No.4829718

>>4829704
>>4829703
Both system("pause"); and getch(); will work. Just make sure you add the stdlib.h or conio.h libraries respectively.
Also, I wouldn't recommend you to start with DevC++. Find a better compiler (Code blocks for instance would do) and start from C instead of C++.

>> No.4829719

>>4829718
>Start from C rather than C++
LOL, real old school guy here!

>> No.4829734

>>4829719
Whatever, man.

>> No.4829745

Thanks for the help, guys! I got it :)

>> No.4829778
File: 1014 KB, 664x1019, brofist.png [View same] [iqdb] [saucenao] [google]
4829778

>>4829718
bamp for code::blocks

>> No.4829782

>>4829693
Use MinGW + Code::Blocks or just dev in a Unix environment.

>> No.4829786

Install gentoo

>> No.4829792

cmd.exe has a /k <command> option to keep the console open after <command> has exited.

You could probably bang your head against the registry and find a way to make this the default. I suspect this because in earlier versions of windows, I remember this being a checkbox option for cmd or bat shortcuts. I think.

>> No.4829800

>>4829786
this!

Also, even in windows you can get around this by just using your compiler to ONLY compile the program, then leave a cmd.exe window open and actually start the program from the command line, then the window will stay open without you having to do system("PAUSE");

and then it's even better if you have a command line compiler like gcc.

>> No.4829816
File: 58 KB, 400x400, heil-arnold.jpg [View same] [iqdb] [saucenao] [google]
4829816

anyone else thinks that we should ban evolutionary programming ? i mean, we dont need it and it could be our doom