Receiving user input to direct program

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Nov 2007
Posts: 47
Reputation: joshua.tilson is an unknown quantity at this point 
Solved Threads: 0
joshua.tilson's Avatar
joshua.tilson joshua.tilson is offline Offline
Light Poster

Receiving user input to direct program

 
0
  #1
Nov 18th, 2007
Good Morning,

This is a bit of a double question thread. First what i have done is written a basic stop watch program, it runs through the loops and iterates the time on screen.
My first question is this: is there some way to leave the program listening for user input to stop or reset the time or to exit the program?

Secondly i used the system("CLS") to clear the previous count every time, i am under the understanding that this is not portable and not a good way to get the desired result. Is there some other way to have the cout of time to overwrite the last cout?


here is my code:

  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <windows.h>
  4.  
  5.  
  6. using namespace std;
  7.  
  8. void stop_watch();
  9.  
  10. int main() {
  11. stop_watch();
  12. cin.get();
  13. return 0;
  14. }
  15.  
  16. void stop_watch() {
  17. int hour = 0, min = 0, sec =0;
  18.  
  19. for (hour = 0; hour <= 1; hour++){
  20. for (min = 0; min < 61; min++){
  21. for (sec = 0; sec < 61; sec++){
  22. Sleep(1000);
  23. system("CLS");
  24. cout << hour << ":" << min << ":" <<sec;
  25. }
  26. }
  27. }
  28. }

The system("CLS") isn't a huge deal as it works for my situation right now, however in the spirit of doing things properly i would love to hear peoples ideas on this.

thanks
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 1,873
Reputation: twomers has a spectacular aura about twomers has a spectacular aura about twomers has a spectacular aura about 
Solved Threads: 56
twomers's Avatar
twomers twomers is offline Offline
Posting Virtuoso

Re: Receiving user input to direct program

 
0
  #2
Nov 18th, 2007
For the second question you could use \r in a print statement. This goes to the start of the line on the console window. So suppose I had this:

std::cout << "hello, \rworld";

I would get out:

"world, "

Least I think it's \r Only danger with it though is that you have to be certain that what you're writing is longer or the same length as what's there. It's like writing with the insert thing off so it writes over what's in your screen.
Also, if you want to clear the screen totally you can use the last option here -- http://faq.cprogramming.com/cgi-bin/...&id=1043284385 Just add the function and call it whenever you want to do the clearin'.

>> is there some way to leave the program listening for user input to stop or reset the time or to exit the program?
Listening for user input... you could use getch() (conio.h, but it's not portable), kbhit() (conio again, I think, either that or windows), or a combination of both:

  1. char ch;
  2. while( !kbhit() ) {
  3. ch = getch();
  4. // do something with the timer
  5. std::cout<< "You hit: " << ch << "\n";
  6. }
Last edited by twomers; Nov 18th, 2007 at 7:51 am.
I blag!?
"Mr Kitty, you have to live in the attic now. Here, write a diary."
I am the Walrus!
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 47
Reputation: joshua.tilson is an unknown quantity at this point 
Solved Threads: 0
joshua.tilson's Avatar
joshua.tilson joshua.tilson is offline Offline
Light Poster

Re: Receiving user input to direct program

 
0
  #3
Nov 18th, 2007
Originally Posted by twomers View Post
For the second question you could use \r in a print statement. This goes to the start of the line on the console window. So suppose I had this:

std::cout << "hello, \rworld";

I would get out:

"world, "

Least I think it's \r
twomers, the \r works perfectly, also probably a bit more portable that the system("cls").
is there some where i can find out about the diferent \'s? i know \n and \r now, what other ones are there?

I am also working on the user input problem, will update every one once i figure something out.

thanks
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,670
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Receiving user input to direct program

 
0
  #4
Nov 18th, 2007
Originally Posted by joshua.tilson View Post
is there some where i can find out about the diferent \'s? i know \n and \r now, what other ones are there?
here

>>I am also working on the user input problem, will update every one once i figure something
I would create another thread and put the timer in that thread then you can use normal standard input functions in the primary thread.
Last edited by Ancient Dragon; Nov 18th, 2007 at 11:19 am.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 47
Reputation: joshua.tilson is an unknown quantity at this point 
Solved Threads: 0
joshua.tilson's Avatar
joshua.tilson joshua.tilson is offline Offline
Light Poster

Re: Receiving user input to direct program

 
0
  #5
Nov 18th, 2007
Ancient Dragon Thanks for the page on escape sequences, i wasn't even sure what to call then.

I think multiple threads is a bit beyond my experience, I understand the concept just not how to implement it, any pointers on that?

thanks again!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,670
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1502
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: Receiving user input to direct program

 
0
  #6
Nov 18th, 2007
There are several ways to create threads, depending on the operating system. MS-Windows can use CreteThread() win32 api function, or _beginthread(). I've always used CreateThread() in my windows programs. Its not really all that difficult and you should be able to use google and find lots of examples.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 47
Reputation: joshua.tilson is an unknown quantity at this point 
Solved Threads: 0
joshua.tilson's Avatar
joshua.tilson joshua.tilson is offline Offline
Light Poster

Re: Receiving user input to direct program

 
0
  #7
Nov 18th, 2007
Awesome, Thanks!
I will look into that as well, thank you
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Receiving user input to direct program

 
0
  #8
Nov 18th, 2007
> I think multiple threads is a bit beyond my experience...
it would be easier to use a console input buffer and wait for it to be signalled.
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <windows.h>
  4. using namespace std ;
  5.  
  6. int main()
  7. {
  8. HANDLE input = CreateFileA( "CONIN$",
  9. GENERIC_READ|GENERIC_WRITE, 0, 0,
  10. OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0 ) ;
  11. FlushConsoleInputBuffer(input) ;
  12.  
  13. for ( int hour = 0; hour < 2 ; ++hour )
  14. for (int min = 0; min < 60 ; ++min )
  15. for ( int sec = 0; sec < 60 ; ++sec )
  16. if( WaitForSingleObject( input, 1000 )
  17. == WAIT_TIMEOUT )
  18. cout << '\r' << hour << ':' << setw(2)
  19. << setfill('0') << min << ':' << setw(2)
  20. << setfill('0') << sec << flush ;
  21. }
Last edited by vijayan121; Nov 18th, 2007 at 1:24 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Receiving user input to direct program

 
0
  #9
Nov 18th, 2007
If you wish this to be "portable" across at least Windows and Unix, you should make use of the curses library. You can get PDCurses which works on both systems, and a few others to boot. Most *nix systems come with ncurses (or at least curses) already installed and usable. (Most basic curses functions are common to all three packages.)

Using curses allows you to directly control device input. You can test for input (whether there is any to read or not) and do fancy output, all using a library that is very portable.

Else:

To create a new thread in windows, all you really need is a function that will be called in the new thread. So your program will have a main() function, which gets executed when your program runs normally, and another function which is like main() but for that thread. You will also have to look up how to send signals between the threads, because you can't just modify common data without the possibility of corrupting it. The only exception would be if only one thread can write to a single-byte variable (such a boolean), and the other(s) may only read it. Not the best answer, but the simplest.

There is nothing wrong with using a system program to clear the screen. It is simple and you can count on "cls" or "clear" being present on most systems. The drawback is, of course, that it is a foreign program. See here for more.

Both \r and \b are almost guaranteed to work on any tty device.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Receiving user input to direct program

 
0
  #10
Nov 18th, 2007
> If you wish this to be "portable" across at least Windows and Unix, you should make use of the curses library.
if the definition of 'user input' is limited to characters typed in on the keyboard (except things like ALT+TAB) while the console/terminal has input focus, curses is a portable option. if 'user input' has a more general meaning, it would include input via menu, mouse, focus etc. i think it would be possible to write a library that would work in both windows and unix for these, but i do not know if such a library has been written.
Last edited by vijayan121; Nov 18th, 2007 at 9:40 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1951 | Replies: 14
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC