 |
 |
 |
 |
| Programming & Packaging A place to discuss programming and packaging. |

16th September 2004, 06:32 AM
|
|
Registered User
|
|
Join Date: Aug 2004
Posts: 1,317

|
|
|
Stupid c++ question...how do you pause output in a terminal program?
I am using c++, just kind of playing around seeing how much I (don't) remember. One thing I noticed is that outside of borland compilers it is apparently a mystery as to how to pause console output. I tried system("PAUSE"); and I get the following
Code:
EXECUTING:
/home/kevin/Projects/shopping/src/shopping
----------------------------------------------
This program is intended to test how much I remember and have forgotten.
Hello customer, I will now list the available items.
1. Hot dogs
2. Nachos
3. Sub sandwich
sh: line 1: PAUSE: command not found
----------------------------------------------
Program exited successfully with errcode (0)
Press the Enter key to close this terminal ...
Now if the program broke I would be one step closer to solving this issue but it doesn't even complain about system("PAUSE"); what command can I use to pause output. While I am at it what command can I use to clear console output, since I have a sneaking suspicision that system("cls"); wont' work either
Here is the code in question:
Code:
#include <iostream>
#include <cstdlib>
using namespace std;
int customer();
int main()
{
cout << "This program is intended to test how much I remember and have forgotten." <<endl;
customer();
system("PAUSE");
return 0;
}
Also is there any way to not have to include the same exact header files in member functions and still keep those member functions in seperate files? (It's an organizational thing that I don't want to let go of because it makes things easy to focus on for me.)
__________________
Ware wa mutekinari.
Wa ga kage waza ni kanau mono nashi.
Wa ga ichigeki wa mutekinari.
Please vote for this bug and help me make GAYT [Go As You Type ]a standard option in Firefox.
|

16th September 2004, 06:38 AM
|
|
Registered User
|
|
Join Date: May 2004
Location: Raleigh, NC
Posts: 741

|
|
|
well what are you waiting for. Like a user to select a product. Because an easy way would be have a cin of whatever. then it would wait for input. Either that or write a loop that executes many many times, and that is pascal style.
__________________
E-mail: jcstille@gmail.com
FedoraForum.org Administrator
Please read the Guidelines
So these are the people FedoraForum.org Here and their Blogs
My Blog <-Where I let you know whats going on
|

16th September 2004, 06:54 AM
|
|
Registered User
|
|
Join Date: Aug 2004
Posts: 1,317

|
|
|
well that is one way to do it, but that is hokey, I just need a general pause command so that as this program gets longer and longer the output is going to end up being so crazy that it won't be readable. I want to have it so that it waits for the user to press exit just like it waits for the user (me in this case.) to press entre before the terminal closes.
Do you know of a clrscr() type command system("cls"); doesn't work at all and generates no errors either.... (I know I could do 25 new lines but its hokey [I'll have to look around for a c++ example of how to actually make a genuine clrscr() function, I know I saw one way back.] )
__________________
Ware wa mutekinari.
Wa ga kage waza ni kanau mono nashi.
Wa ga ichigeki wa mutekinari.
Please vote for this bug and help me make GAYT [Go As You Type ]a standard option in Firefox.
|

16th September 2004, 07:18 AM
|
 |
Retired Community Manager
|
|
Join Date: Apr 2004
Location: Slovenia
Age: 30
Posts: 1,713

|
|
|
Maybe you can use 'sleep NUMBER' command. The NUMBER is how long in seconds to sleep.
You are using DOS command 'cls' unix command is 'clear' to clear the screen.
|

16th September 2004, 10:54 AM
|
|
Registered User
|
|
Join Date: Aug 2004
Posts: 1,317

|
|
|
Thanks for the info..I'll try it next time I boot Fedora, that would explain why it compiles and runs with no real errors.
__________________
Ware wa mutekinari.
Wa ga kage waza ni kanau mono nashi.
Wa ga ichigeki wa mutekinari.
Please vote for this bug and help me make GAYT [Go As You Type ]a standard option in Firefox.
|

16th September 2004, 09:21 PM
|
 |
Registered User
|
|
Join Date: Jun 2004
Location: Portsmouth, UK
Posts: 444

|
|
Pausing
As has already been eluded too, if you want to pause then the easiest way of doing this is to just sit in a loop for a period of time.
for(loop=0;loop<5000000;loop++)
{
// I'm paused
}
I guess you are a newbie starting to put code together, and so that for loop will probably do for now, but it's not a very good pause at all.
Under release builds (i.e. get the compiler to optimise the code) it would probably notice that the pause code does nothing and remove it, thus removing your pause.
What you are trying to do is tackle 3 basic problems at once, these are:
1. Some kind of processing (remembering what somebody ordered)
2. Some kind of screen IO
3. A system timer function
For the screen IO & System timer, you have lots of different options open to you depending on what you are trying to achieve. If you want textured, lit, polygons then GLUT & OpenGL would be a good library to use, and the setting up and using of timers and IO under those is well documented here: http://www.opengl.org/
If you are looking for a more GNOME style interface, then you are best using KDEVELOP & the KDE project, other's can give you more info.
As you are just experimenting with code though, the way you are going is probably the best. Issue the SYSTEM command and just call native stuff. Good luck learning to program, it's a steep learning code but gets substantially easier later on, and it will reward you richly if\when you master it!
|

16th September 2004, 10:02 PM
|
|
Registered User
|
|
Join Date: Aug 2004
Posts: 3,857

|
|
|
ShadowSkill,
Do you want a pause that is not controlled by the user or do you want the screen to pause until the user presses a key? And do you want the user to have to press Enter after he presses the key? And should the method be portable or will you settle for one that only works in Unix/Linux?
__________________
"Never let the task you are trying to accomplish distract you from the study of computers."
|

16th September 2004, 10:30 PM
|
|
Registered User
|
|
Join Date: May 2004
Location: Raleigh, NC
Posts: 741

|
|
|
You can try this If you include the windows.h file you can use the Sleep();command, with the duration within the parenthesis.
__________________
E-mail: jcstille@gmail.com
FedoraForum.org Administrator
Please read the Guidelines
So these are the people FedoraForum.org Here and their Blogs
My Blog <-Where I let you know whats going on
|

17th September 2004, 12:01 AM
|
|
Registered User
|
|
Join Date: Aug 2004
Posts: 1,317

|
|
I want the program to simply wait until the user presses entre before continuing with execution (I miss getch(); ) I remember the sleep() function dimly, but it had not occured to me because I was going for a pause that would last until the user pressed a key (I don't know how to make it so that ONLY Entre would work...I never learned anything about how to bind keys even thougfh I knew it could be done.....) I am going for something that will work on both *nix and windows.
Pompey:
Yea I am just experimenting right now, trying to get at least some of the stuff I used to know to come back to me, I am very rusty I didnt keep up with it after high school [about 2 years now] and most of what I knew and was avle to do with relative consistency has all but dissapeared now.  My goal is to expand the above code as I pick up new (and old) things to sort of keep it in my head before my actual courses start [even though it probably won't c++ or even c#, I hope it ends up being c# .net though. It better not be VB or Java *shudders]
__________________
Ware wa mutekinari.
Wa ga kage waza ni kanau mono nashi.
Wa ga ichigeki wa mutekinari.
Please vote for this bug and help me make GAYT [Go As You Type ]a standard option in Firefox.
|

17th September 2004, 03:44 PM
|
|
Registered User
|
|
Join Date: Aug 2004
Posts: 3,857

|
|
|
I've asked the same question as you. If you are just fooling around you can use the system(...) command to have a shell read some variable (which it will not use.) This probably looks like
system("read $variable");
But I'm not sure about the exact syntax.
If you want to write a "serious" Unix/Linux terminal program then you should look a the ncurses functions - or that is what my advisors tell me. There is a getch() function ( man 3 curs_getch ). I've never done this because one would have to learn the functions that set up the terminal. ( If you figure out how to set one up, please post an example!)
Of course, my advisors always ask me "Why do you want a terminal program? Just write a gui interface. It's simple nowadays.". That may be true. But I prefer to write C programs using the system call rather than writing scripts. And scripts aren't gui.
__________________
"Never let the task you are trying to accomplish distract you from the study of computers."
|

18th September 2004, 12:05 AM
|
 |
Registered User
|
|
Join Date: Aug 2004
Location: Stokes County, North Carolina
Age: 54
Posts: 248

|
|
I have an idea
Quote:
|
Originally Posted by Shadow Skill
I am using c++, just kind of playing around seeing how much I (don't) remember. One thing I noticed is that outside of borland compilers it is apparently a mystery as to how to pause console output. I tried system("PAUSE"); and I get the following ....
|
Shadow Skill,
Sometimes when I have a hard time finding the function I want, I just make one.
Check This out, I did it in KDevelop...
Code:
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
void delay(int num)
{
int now, later;
time_t seconds; // time_t struct
now = time(NULL); //get current time
later = now + num; //determine how long from now to stop (future time)
while(time(NULL) < later)
{/*do nothing*/} /*this just hangs out until it reaches future time*/
return;
}
int main(int argc, char *argv[])
{
cout << "Hello, world!" << endl;
delay(3); /*******************call the function***************/
cout << "Goodbye world!" << endl;
return EXIT_SUCCESS;
}
delay() is the function I made up. It takes an int for the argument. I included the main() to show how to call it. In this case the number 3 is the delay time in seconds. It lest you be very specific on how long a delay you want to put in your app.
I hope this is what you are looking for.
|

18th September 2004, 02:18 AM
|
|
Registered User
|
|
Join Date: Aug 2004
Posts: 1,317

|
|
|
I don't know if you know this but there was at least a delay function in turbo c++ [My high school was too cheap to get us real IDE's, this being betwwen 2k1 and 2k3..so it isnt like proper non dos IDE'S were still buggy etc.] that behaved in almost the exact same manner as the code above except that that it took time in milliseconds [I might be confusing the data entry method with sleep(); .)so you would need to do delay(3000); for 3 seconds of delay time. But this and sleep() will come in handy, thanks alot..Now if only I could remember how to make header files and proper classes I could make the above portable.[IMO one's that can be called with just the function name itself none of that hokey constructor crap.] (Cracks open standard c++ bible [probably sorely outdated by now.] )
__________________
Ware wa mutekinari.
Wa ga kage waza ni kanau mono nashi.
Wa ga ichigeki wa mutekinari.
Please vote for this bug and help me make GAYT [Go As You Type ]a standard option in Firefox.
|

18th September 2004, 04:32 PM
|
 |
Registered User
|
|
Join Date: Aug 2004
Location: Stokes County, North Carolina
Age: 54
Posts: 248

|
|
Actually, no. The units in my delay are seconds.
delay(3) = 3 seconds
delay(3000) = 50 minutes
After posting, I was thinking I could use a float for the argument so you could use fractions of seconds. I had something similar in a header file but also forgot how to do it. (I use Delphi and php at work. Also, *gulp* windows 2000.)
Time to crack my book.
By the way, I still use a C++ book from 1996.
|

18th September 2004, 09:05 PM
|
|
Registered User
|
|
Join Date: Aug 2004
Posts: 1,317

|
|
Heh I need one on building GUI's the fundamental stuff appears to be pretty much the same, but the book I have is alittle more windows centric, and only teaches you how to do stuff for console commands.  Do you know of any c++ books that are written in english (not nerd speak.) that handle GUI's specifically *nix gui programming? (would prefer something that talks about things I can use on windows as well.) To be honest I am alitttle intimidated by some of the c++ code I have seen that is used to make dialouge boxes and such.
__________________
Ware wa mutekinari.
Wa ga kage waza ni kanau mono nashi.
Wa ga ichigeki wa mutekinari.
Please vote for this bug and help me make GAYT [Go As You Type ]a standard option in Firefox.
|

19th September 2004, 05:22 PM
|
|
Registered User
|
|
Join Date: Aug 2004
Posts: 3,857

|
|
|
My opinion is that the typical C++ material on guis ( be it books, docs or the experts who pose in the various mailing lists) assumes a veritable mastery of C++. This is complicated by the fact that there are many different libraries for writing guis. For C++, one has Qt, Gtkplus, WxWindows and (my favorite) the Fox toolkit. If you want a simple way of doing guis, I'd advise learning a new language! For example Tcl/Tk .
__________________
"Never let the task you are trying to accomplish distract you from the study of computers."
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
Current GMT-time: 12:19 (Wednesday, 19-06-2013)
|
|
 |
 |
 |
 |
|
|