Fedora Linux Support Community & Resources Center
  #1  
Old 16th September 2004, 06:32 AM
Shadow Skill Offline
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.
Reply With Quote
  #2  
Old 16th September 2004, 06:38 AM
jcstille Offline
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
Reply With Quote
  #3  
Old 16th September 2004, 06:54 AM
Shadow Skill Offline
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.
Reply With Quote
  #4  
Old 16th September 2004, 07:18 AM
ghaefb Offline
Retired Community Manager
 
Join Date: Apr 2004
Location: Slovenia
Age: 31
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.
__________________
_ghaefb


blog & sysinfo
Reply With Quote
  #5  
Old 16th September 2004, 10:54 AM
Shadow Skill Offline
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.
Reply With Quote
  #6  
Old 16th September 2004, 09:21 PM
PompeyBlue Offline
Registered User
 
Join Date: Jun 2004
Location: Portsmouth, UK
Posts: 448
Talking 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!
Reply With Quote
  #7  
Old 16th September 2004, 10:02 PM
tashirosgt Offline
Registered User
 
Join Date: Aug 2004
Posts: 3,964
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."
Reply With Quote
  #8  
Old 16th September 2004, 10:30 PM
jcstille Offline
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
Reply With Quote
  #9  
Old 17th September 2004, 12:01 AM
Shadow Skill Offline
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.
Reply With Quote
  #10  
Old 17th September 2004, 03:44 PM
tashirosgt Offline
Registered User
 
Join Date: Aug 2004
Posts: 3,964
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."
Reply With Quote
  #11  
Old 18th September 2004, 12:05 AM
Chas.H Offline
Registered User
 
Join Date: Aug 2004
Location: Stokes County, North Carolina
Age: 55
Posts: 248
Lightbulb 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.
__________________
Cheers,
Charles
Booke Seminars
Gilded Age
9 Dragon Palace Restaurant

Still using FC2
Upgraded to FC4 July 16, 2005!!
Still using FC4

Registered Linux User #373127
Reply With Quote
  #12  
Old 18th September 2004, 02:18 AM
Shadow Skill Offline
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.
Reply With Quote
  #13  
Old 18th September 2004, 04:32 PM
Chas.H Offline
Registered User
 
Join Date: Aug 2004
Location: Stokes County, North Carolina
Age: 55
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.
__________________
Cheers,
Charles
Booke Seminars
Gilded Age
9 Dragon Palace Restaurant

Still using FC2
Upgraded to FC4 July 16, 2005!!
Still using FC4

Registered Linux User #373127
Reply With Quote
  #14  
Old 18th September 2004, 09:05 PM
Shadow Skill Offline
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.
Reply With Quote
  #15  
Old 19th September 2004, 05:22 PM
tashirosgt Offline
Registered User
 
Join Date: Aug 2004
Posts: 3,964
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."
Reply With Quote
Reply

Tags
output, pause, program, questionhow, stupid, terminal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Copying lengthy program output from Gnome-terminal? kurtdriver Using Fedora 2 11th February 2009 02:32 AM
Reading output from program, called inside another program clearer Programming & Packaging 2 20th February 2008 08:52 AM
Quick question: FC5 / gnome - can I RUN a program w/o terminal? SJLinuxGuy Using Fedora 0 29th April 2006 11:23 PM
Program question, launching from terminal mattmns Using Fedora 9 30th January 2005 11:50 PM
How to program print output? General Question skoona Programming & Packaging 3 14th May 2004 02:52 AM


Current GMT-time: 08:44 (Tuesday, 21-10-2014)

TopSubscribe to XML RSS for all Threads in all ForumsFedoraForumDotOrg Archive
logo

All trademarks, and forum posts in this site are property of their respective owner(s).
FedoraForum.org is privately owned and is not directly sponsored by the Fedora Project or Red Hat, Inc.

Privacy Policy | Term of Use | Posting Guidelines | Archive | Contact Us | Founding Members

Powered by vBulletin® Copyright ©2000 - 2012, vBulletin Solutions, Inc.

FedoraForum is Powered by RedHat
Zabbar Travel Photos on Instagram - Bismil - Vemalwada Travel Photos on Instagram