Blopa Posted March 9, 2011 Posted March 9, 2011 Hello forum, I'm just starting using C++, but I've got a problem, I'm working with Dev-C++ and supposedly a code like this: #include <stdio.h> int main() { printf("whatever"); return 0; } would make an output window appear saying "whatever", I checked in many other sites and it should work, nevertheless, the window opens on my computer and quickly closes up... don't know why is it happening... By the way, I'm working with version 4.9.9.2 Thanks
Cap'n Refsmmat Posted March 9, 2011 Posted March 9, 2011 The trouble is that the program runs and terminates, and Dev-C++ closes its window as soon as the program terminates. You can try using any C++ command that requires user input, like cin.get() or getchar() or whatever stdio might use. That'll force the program to wait for you to press enter before closing.
Xittenn Posted March 9, 2011 Posted March 9, 2011 I have never been too fond of outputting textual info to a console as it rarely serves any real purpose and can be difficult when it comes to manipulating large data sets that one might wish to output. In my opinion fstream is much more useful and for educational purposes provides much more insight into how tasking is accomplished within this language.
khaled Posted March 13, 2011 Posted March 13, 2011 Hello forum, I'm just starting using C++, but I've got a problem, I'm working with Dev-C++ and supposedly a code like this: #include <stdio.h> int main() { printf("whatever"); return 0; } would make an output window appear saying "whatever", I checked in many other sites and it should work, nevertheless, the window opens on my computer and quickly closes up... don't know why is it happening... By the way, I'm working with version 4.9.9.2 Thanks It seem you are a C programmer, because in C++ you can just type # include <cstdio> anyway, you should tell the computer either to wait for any input, this can be done in many ways ... and your code will be like this: #include <stdio.h> int main() { printf("whatever"); getchar(); return 0; }
Sendou Posted March 18, 2011 Posted March 18, 2011 (edited) #include <stdio.h> int main() { printf("whatever"); system("pause>nul"); return 0; } I usually use a system pause>nul. This will leave the CMD window open without requesting any user input. But I'm a newbie, so forgive me if I'm wrong. But I'm not sure if it works in C, since I've been learning C++ with MS Visual Studio 2010 Pro. Reading online, this doesn't look like it's meant to be standard practice, soooo... take it for what it's worth. It gets the job done, but there has to be a better way. Edited March 18, 2011 by Sendou
Shadow Posted March 18, 2011 Posted March 18, 2011 (edited) #include <stdio.h> int main() { printf("whatever"); system("pause>nul"); return 0; } I usually use a system pause>nul. This will leave the CMD window open without requesting any user input. But I'm a newbie, so forgive me if I'm wrong. But I'm not sure if it works in C, since I've been learning C++ with MS Visual Studio 2010 Pro. Reading online, this doesn't look like it's meant to be standard practice, soooo... take it for what it's worth. It gets the job done, but there has to be a better way. This will only work in MSVS, in other cases you have to #include <windows.h> This also means that the code won't be portable to other architectures. I usually use the following function (not sure if you've gotten as far as functions), although understanding how it works may be out of your grasp at the moment. But don't worry, as you progress in your learning you'll soon understand it easily: #include <ctime> #include <cstdio> void timePause(int seconds) { clock_t endwait; endwait = clock () + seconds * CLOCKS_PER_SEC ; while (clock() < endwait) {} } int main() { printf("And we shall wait for 3 seconds before the window closes..."); timePause(3); return 0; } -Source I think the best thing to do is to use the Code::Blocks IDE, which pauses the console by itself. And it's also great in debugging and quite frankly in every other regard as well, at least in my opinion. Edited March 18, 2011 by Shadow
Djordje Posted March 20, 2011 Posted March 20, 2011 Hello forum, I'm just starting using C++, but I've got a problem, I'm working with Dev-C++ and supposedly a code like this: #include <stdio.h> int main() { printf("whatever"); return 0; } would make an output window appear saying "whatever", I checked in many other sites and it should work, nevertheless, the window opens on my computer and quickly closes up... don't know why is it happening... By the way, I'm working with version 4.9.9.2 Thanks You need to put some function that'll wait for user input or set program to wait at that point for text to stay on the screen of the console. return 0 exits the program immediately.
Sendou Posted March 24, 2011 Posted March 24, 2011 (edited) This will only work in MSVS, in other cases you have to #include <windows.h> This also means that the code won't be portable to other architectures. I usually use the following function (not sure if you've gotten as far as functions), although understanding how it works may be out of your grasp at the moment. But don't worry, as you progress in your learning you'll soon understand it easily: #include <ctime> #include <cstdio> void timePause(int seconds) { clock_t endwait; endwait = clock () + seconds * CLOCKS_PER_SEC ; while (clock() < endwait) {} } int main() { printf("And we shall wait for 3 seconds before the window closes..."); timePause(3); return 0; } -Source I think the best thing to do is to use the Code::Blocks IDE, which pauses the console by itself. And it's also great in debugging and quite frankly in every other regard as well, at least in my opinion. Thanks, that does indeed work to pause the console for a set amount of time and does feel a bit more proper than what I was doing before. I do have another question though. int main() { cout << "Wait forever."; while (1 > 0) {} return 0; } Anything wrong with doing it this way to make a console window stay open indefinitely while not requiring user input? I'm not familiar with the proper standards of programming and won't be taking my Intro CS course until next semester, so I'm really just playing around in MSVC to kind of dip my feet in before I actually start any learning I'm thinking it's a bad way to do it because the program would endlessly calculate if 1 > 0... Yeah, nevermind, I think I answered my own question lol... the difference in CPU process is quite noticeable between waiting for user input to close the window and trying to keep it open indefinately using a while line. (0% CPU vs ~50% CPU) Edited March 24, 2011 by Sendou
Xittenn Posted March 25, 2011 Posted March 25, 2011 (edited) You keep mentioning 'standard programming' but really what you are doing isn't exactly used, at all. Maybe a long time ago or in the most basic of cases but even then really there isn't a such thing as the standard case! If you are looping indefinitely, which for whatever reason does happen out of some desire, and you still have to press the x with your mouse to exit this is still a required input. #include <windows.h> #include <stdio.h> int main() { char str[MAX_PATH] = {0}; printf("What is the airspeed velocity of an unladen swallow? \n"); printf("velocity:"); scanf( "%s", str ); printf("\n \n \n \n"); printf("(press enter key to exit) \n"); char exit = {0}; /* --- good practice to do other stuff as well or just use getchar(); getchar(); ---- */ while( exit != 10 ) { getchar(); exit = getchar(); }; return 0; } Pausing in general is considered an anti-pattern in practice, there are usually far better things for a CPU to be doing. But none of this matters during the initial learning phase and the solutions to this problem become more dependent on the libraries that you are involving yourself with. In standard practice and where windows is concerned a window is created and a message loop checks to see if it is time to quit. This isn't even considered general practice and systems for closing software can become quite complex where application programming under c++ is concerned. Edited March 25, 2011 by Xittenn
metallicsoul92 Posted March 27, 2011 Posted March 27, 2011 This will only work in MSVS, in other cases you have to #include <windows.h> This also means that the code won't be portable to other architectures. I usually use the following function (not sure if you've gotten as far as functions), although understanding how it works may be out of your grasp at the moment. But don't worry, as you progress in your learning you'll soon understand it easily: #include <ctime> #include <cstdio> void timePause(int seconds) { clock_t endwait; endwait = clock () + seconds * CLOCKS_PER_SEC ; while (clock() < endwait) {} } int main() { printf("And we shall wait for 3 seconds before the window closes..."); timePause(3); return 0; } -Source I think the best thing to do is to use the Code::Blocks IDE, which pauses the console by itself. And it's also great in debugging and quite frankly in every other regard as well, at least in my opinion. this was my favorite response. i didnt even think about using a clock haha.
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now