944,149 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2389
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 18th, 2007
0

Receiving user input to direct program

Expand Post »
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:

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 13
Solved Threads: 0
Light Poster
joshua.tilson is offline Offline
47 posts
since Nov 2007
Nov 18th, 2007
0

Re: Receiving user input to direct program

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.
Reputation Points: 453
Solved Threads: 57
Posting Virtuoso
twomers is offline Offline
1,873 posts
since May 2007
Nov 18th, 2007
0

Re: Receiving user input to direct program

Click to Expand / Collapse  Quote originally posted by twomers ...
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
Reputation Points: 13
Solved Threads: 0
Light Poster
joshua.tilson is offline Offline
47 posts
since Nov 2007
Nov 18th, 2007
0

Re: Receiving user input to direct program

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Nov 18th, 2007
0

Re: Receiving user input to direct program

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!
Reputation Points: 13
Solved Threads: 0
Light Poster
joshua.tilson is offline Offline
47 posts
since Nov 2007
Nov 18th, 2007
0

Re: Receiving user input to direct program

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2283
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,963 posts
since Aug 2005
Nov 18th, 2007
0

Re: Receiving user input to direct program

Awesome, Thanks!
I will look into that as well, thank you
Reputation Points: 13
Solved Threads: 0
Light Poster
joshua.tilson is offline Offline
47 posts
since Nov 2007
Nov 18th, 2007
0

Re: Receiving user input to direct program

> 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.
c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006
Nov 18th, 2007
0

Re: Receiving user input to direct program

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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Nov 18th, 2007
0

Re: Receiving user input to direct program

> 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.
Reputation Points: 1159
Solved Threads: 285
Posting Virtuoso
vijayan121 is offline Offline
1,606 posts
since Dec 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: invention
Next Thread in C++ Forum Timeline: 2d arrays





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC