| | |
Receiving user input to direct program
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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
here is my code:
The
thanks
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)
#include <iostream> #include <cstdlib> #include <windows.h> using namespace std; void stop_watch(); int main() { stop_watch(); cin.get(); return 0; } void stop_watch() { int hour = 0, min = 0, sec =0; for (hour = 0; hour <= 1; hour++){ for (min = 0; min < 61; min++){ for (sec = 0; sec < 61; sec++){ Sleep(1000); system("CLS"); cout << hour << ":" << min << ":" <<sec; } } } }
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
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:
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:
c Syntax (Toggle Plain Text)
char ch; while( !kbhit() ) { ch = getch(); // do something with the timer std::cout<< "You hit: " << ch << "\n"; }
Last edited by twomers; Nov 18th, 2007 at 7:51 am.
•
•
•
•
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
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
•
•
•
•
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
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.
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.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
> 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.
it would be easier to use a console input buffer and wait for it to be signalled.
c++ Syntax (Toggle Plain Text)
#include <iostream> #include <iomanip> #include <windows.h> using namespace std ; int main() { HANDLE input = CreateFileA( "CONIN$", GENERIC_READ|GENERIC_WRITE, 0, 0, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, 0 ) ; FlushConsoleInputBuffer(input) ; for ( int hour = 0; hour < 2 ; ++hour ) for (int min = 0; min < 60 ; ++min ) for ( int sec = 0; sec < 60 ; ++sec ) if( WaitForSingleObject( input, 1000 ) == WAIT_TIMEOUT ) cout << '\r' << hour << ':' << setw(2) << setfill('0') << min << ':' << setw(2) << setfill('0') << sec << flush ; }
Last edited by vijayan121; Nov 18th, 2007 at 1:24 pm.
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.
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.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
> 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.
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.
![]() |
Similar Threads
- Tutorial: User Input: Strings and Numbers [C++] (C++)
- Calculate GPA problems with user input, Help needed (Java)
- User Input without GUI... Need Guidance (Java)
- Type Conversion of User Input: (C++)
- Error Checking for user input (Java)
- error checking of user input (C++)
- Creating a GUI that accepts user input help (Java)
- Need Help With Error Checking User Input (C)
Other Threads in the C++ Forum
Views: 1951 | Replies: 14
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






