This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
C/C++: Capture characters from standard input without waiting for enter to be pressed
Is there a way to read standard input continuously? I want to make an X and O game and I want the player to use the the arrows to move its position on the grid. I can't use cin because it requests you to press enter to get the input data into variables. So, does a function or an object doing that exist?
For console (text) applications you'd use something like ncurses, which does terminal 'GUI' controls.
For graphics applications you would use something like SDL which enables event-driven programming.
Related
This question already has an answer here:
adding text to another programs text box c++
(1 answer)
Closed 2 years ago.
So I have a console application and I want the console application to print/paste characters into any other programm. Is it possible for a console application to write something outside the console window?
Unfortunately you can not. If you develop the other application you could find a way through IPC or shared memory.
This question already has answers here:
Hide user input on password prompt [duplicate]
(3 answers)
Closed 6 years ago.
I'm on Ubuntu with C++
How do I hide the red box (user input) as shown in the image above on my terminal ?
char *MESSAGE=getpass("");
the code above would leave a blank line after each input and I dont want the message to be hidden while the user typing the message.
In short, I want the message to be visible as I'm typing the message but goes invisible on my terminal as soon as he entered.
EDIT : Can someone please enlighten me on how this question is duplicated to that thread?.
You could clear the terminal after a message has been sent and reprint the whole chat afterwards.
If you can print the name first and then read the input message, I think your problem might be solved. Have you tried that?
You cannot do that in a reliable and portable way with only functions from the standard C library, not even with Posix one.
If you now that you are using a Windows console, the Windows console functions could allow you to erase specific portions of the screen, if you know that you are using a terminal emulator conformant to one standart (VT100, xterm, ...) you can output special control sequence to do the same.
The only portable way would be to use a screen management library like curses that will do the low level work for you.
This question already has answers here:
Non-blocking console input C++
(13 answers)
Closed 6 years ago.
First of all let me state that I am new to C++. I received an assignment to make a mini "snake" game meaning I need to make a grid and have a box move around in it. It is a simple 1x1 box, and no fruits to catch or anything.
I am able to create the grid, and have a box exist in it that I can move up, down, left, right using user input. My problem is I need the box to move automatically every second in the direction it was headed. I have no problems with the sleep command (I think), my problem is that when I am waiting for the user to enter his direction the program stops until that input is given. I need a way to have the box continue moving while I wait for input. For this I need either to just have the code continue while waiting for the input, or to redirect it to doing a function while waiting for input.
I have been googling this and trying things for some hours now and all I keep finding is multi-threading. I tried using thread, pthread, boost/thread.hpp but I can't get any of them to work.
In any case multi-threading is way more advanced than what my class is doing and there is no chance that is what my professor want's us to do. He said something about having a function of cin that allows such things, but I can't find it anywhere.
Is there a simple way of doing this?
From what I'm understanding from the question, it seems that you need to implement a timer of sorts that will move the move the box every second. I'm not sure if g++ has something similar to System::Timers as visual studio does but here's a link that shows how to make a timer yourself (though it's written in C). With a timer in place (created, started, and your code to get input and moving of the box happens within the timer's tick function) you could use a following if statement to keeping moving the box. Here's a link to the .NET's timer for more information on how it works. This has code for a timer class that Matthew Hoggan has created for C++ in linux. Hope this helps.
if (user input)
{
change direction
}
move the box
This question already has answers here:
Opening a document programmatically in C++
(4 answers)
Closed 7 years ago.
I use system() function to open a pdf in c++. It works correctly but while opening pdf, the command prompt window appear and disappear. I don't want this window to appear. What should I do?
You probably want a different higher-level API. What that would be depends on what platform you are on, what framework you are using.
Something like win32's ShellExecute or this in MacOSX/Cocoa:
NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
[workspace openFile:aFilePath];
If you are using win32, then use
ShowWindow(GetConsoleWindow(),SW_HIDE);
This will do the trick.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a Mac, so I don't have access to any windows.h functions, or any other windows libraries in C++. I am creating a game in C++. The controls of the game are the characters w,a,s,d. Currently I have the input method as std:cin. However, the problem is after the user types each character they must hit enter every time. Furthermore, the fact the std::cin does not timeout means that the user can essentially 'pause' the game to think about what move to make next (which ruins much of the fun of this game).
I need a function like std::cin but with a timeout of about .25 seconds. A function that will return as soon as the user types the character (without the need of the user hitting enter) would also work; but a function like std::cin with a timeout would be preferred. Please don't suggest window's library functions as I am again a Mac user using terminal.
Are there any standard c++ functions within the standard mac Libraries which will function equivalently to std::cin with a timeout of T that will run correctly in a Mac terminal?
Any reason you can't use int istream::get() ?
It's a blocking call though. I'm sure there is a better way to do it, but the quickest hack I can think of is to spawn a thread (just before calling get()) that waits for 0.25 secs and then writes some 'timeout char' say 't' to teh stream.
You can use the ncurses library, putting the terminal in cbreak() (non-blocking) mode, and disabling input delay with nodelay():
WINDOW *window = initscr();
cbreak();
nodelay(window, TRUE);
Then the getch() function will not block, returning ERR if no character is ready, so you can poll it in a loop and break when your timeout has been reached.
However, this may also require that you use ncurses for screen output, which may be a dealbreaker.