Is it possible to create "quick time events" in C++? [closed] - c++

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 4 years ago.
Improve this question
I have some knowledge about C++, but I stumbled upon an problem. I want the user to enter some text, but when it takes longer than X seconds, the program will go on. I guess, it is not possible, but maybe you know something about it.
It will be in the command line, without GUI.
I am not certain how the programm will look like, but it will be something like a CMD-RPG. I wanted to use Quick Time Events to make it a little bit more exciting.

I cant comment so I will just leave this here
Input with a timeout in C++

Since I cannot comment, I will simply leave this as an answer.
One possible way to solve this problem is to have 2 threads:
Input capture thread: since receiving input is a thread-blocking action, you should have a thread that simply polls for user input, placing that input into a thread-safe buffer
Quick-time function on main thread: a function that will be responsible for executing the quick-time event. Something like the following could be done (pseudo code):
clear input buffer //the buffer provided by the input capture thread
bool success = false;
while(current time < ending time)
{
if(input buffer isn't empty)
{
get contents of input buffer and send contents to cout
if (user has finished input correctly)
{
success = true;
break;
}
clear buffer
}
}
return success;
For this to work, you would need to turn off echo on the command prompt (see this page for more info)
Although Diogo's reference is excellent (and informative), I believe this answer is more applicable than since the OP has indicated that this program will be ran on Windows.

Related

How do I std::cout and std::cin at the same time? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I am trying to create a console application that works as a messaging app running off of a server. I want to be able to recieve messages from a friend while I am typing my own message or while the console is awaiting my input (essentially I want to be able to cout while I cin). how would I do this? I'm assuming I will have to work on multiple threads but I cant find anything useful online.
Yes, you will have to work with multiple threads. Essentially, one thread creates and sends messages, one thread receives them. So, they work in parallel and whenever a message is received it is simply printed (or written to a log or what have you) from the reader thread, and the writer thread will constantly be waiting for user input, and will allow the user to send a message. In this code, the message is printed as soon as its received. Also, this code is highly simplified since I have no idea how your message implementation is done, so all the message specifics are left out and a generic message is just printed at some interval. This kind of implementation will also result in messages received cutting off input. You would need some kind of input space separate from the output space so they don't intercept eachother visually like this, or you could use mutexes but this defeats the purpose of getting a message instantly.
#include<iostream>
#include<thread>
#include<chrono>
void reader()
{
using namespace std::chrono_literals;
for(int i = 0; i < 10; i++)
{
std::this_thread::sleep_for(1000ms);
std::cout << "This is a message...\n";
}
}
void writer()
{
std::string message;
for(int i = 0; i < 10; i++)
{
getline(std::cin, message);
}
}
int main(void)
{
std::thread reader_thread(reader);
std::thread writer_thread(writer);
reader_thread.join();
writer_thread.join();
std::cout << "done!\n";
}
EDIT: Thought I would give more pointers on cin/cout intercepting each other. Basically, input and output happens in the same line and same place in most console implementations (Unless you use redirection or pipes or something). Most actual messaging apps have separate places for input and output. For example, you input your message somewhere, but the messages you receive show up above that. This kind of implementation means that input/output never stack on top of each other, but is also pretty much impossible to do with cin/cout. So, if you really want to do this kind of messaging application, you could use pipes or files OR you could use some libraries. Pretty much any GUI lib (Gtkmm, Qt) will let you do something like this, but if you are console-bound, ncurses will let you manipulate the console in complex ways like this so you can have one section for receiving messages and one for sending them.

How to display asterisks (***) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
I am trying to create an ATM application using c++ and I want to display asterisks (****) when I input the pin code. I haven't tried anything because I have no idea on how to do it.
Could someone help me?
A very small program which should help you get started for Windows would go like this:
int main(void)
{
char character = 0;
while (true)
{
character = _getch();
if (character == 13) //Enter
break;
else
{
putc('*');
//Do whatever
}
}
}
Please note that this code is off the top of my head and still requires the correct includes, but should point you in the right direction.
Please refer to https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getch-getwch?view=msvc-160 for the documentation of _getch and http://www.asciitable.com/ for a list of control codes you might be getting.

object is not being saved even after fstream.clear() in c++ [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
I am using C++ to write a music memorize game for my school project, here the Player objects are not being saved in the PLAYER_DATA.DAT file even i tried clear() function
here is the peace of code (as the whole code is 600+ lines)
Player p_dat,plyr;
plyr.getData();
fstream P_file("PLAYER_DATA.DAT",ios::out|ios::in|ios::binary);
while(P_file.read((char*)&p_dat, sizeof(p_dat)))
{
if(nameEqual(plyr,p_dat))
{
P_file.clear();
gotoxy(1,10);
delline();
textcolor(RED);
cout<<"\t\t EXIXTING PLAYER PROFILE FOUND!\n";
int ch = playPanel("It's me", "Change Name");
if(ch == 0)
{
P_file.seekp(P_file.tellg() - sizeof(Player));
GameStarted = 1;
if(c == 1)
Campaign(p_dat,P_file);
else
Endless(p_dat,P_file);
return;
}
else
{
startGame(c);
return;
}
}
}
P_file.clear();
P_file.seekp(0,ios::end);
P_file.write((char*)&plyr,sizeof(plyr));
Just to make it short, The last line of the code is not doing anything the file already exists and of size 0kb
however,
fstream P_file("PLAYER_DATA.DAT",ios::out|ios::in|ios::binary)
P_file.write((char*)&plyr,sizeof(plyr));
is saving the file. Please help me.
EDIT 1.1
finally found this line is problematic
P_file.seekp(0,ios::end);
Its working for code, i.e correctly saving objects
P_file.clear();
P_file.write((char*)&plyr,sizeof(plyr));
P_file.seekp(0,ios::end);
while removing
P_file.seekp(0,ios::end);
making the code look like,
P_file.clear();
P_file.write((char*)&plyr,sizeof(plyr));
after the while loop, is not saving the file
again does not save the object
this line is making problem, are there any alternatives or solutions?
Compile with all warnings and debug info (g++ -Wall -Wextra -g with GCC) then use the debugger (e.g. gdb).
Read carefully documentation of C++ IO functions.
Check that your file has been opened correctly; after:
fstream P_file("PLAYER_DATA.DAT",ios::out|ios::in|ios::binary);
add
if (!P_file) { std::cerr << "failed to open PLAYER_DATA.DAT" << std::endl; };
On some systems (e.g. Linux), you might also display strerror(errno).
After some intermediate call to write, consider using flush.
Be sure that your program is started in the correct working directory.
On Linux, you might also use strace(1) to understand the system calls done by your program.
At last, did you consider using some simple database, e.g. with sqlite, or some indexed file, e.g. with gdbm ?

Get user input inside of loop [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Closed 9 years ago.
Improve this question
I've seen it once or twice in a few places, but I can't seem to find this pattern while searching. What I want is, while in a loop, pause for a couple of milliseconds for user input, then continue.
so it would look something like this:
int main()
{
while (1)
{
//do stuff
//get user input, continue if there is nothing
//do stuff based off of user input
}
}
I'm using xemacs and g++ on Fedora 18 and I'm looking for a single keyboard keypress.
You'll (almost certainly) want to use the curses library.
If you want to pause to wait for input, you call halfdelay to set the time to wait for input. If the user hasn't entered anything in that time, it'll return ERR (or some error code anyway--been a while so I can't remember for sure what). If memory serves, however, the shortest delay you can specify is 1/10th of a second; there's no way to tell it to just pause for (say) up to 2 ms. Edit: thinking about it, it seems like there is a timeout (or something like that) that lets you set a timeout in milliseconds.
More often, you just want to react to input if there is any, and just continue other processing if here is none. In this case, you'd call nodelay.
Either way, you also want to call cbreak to disable line buffering at the OS level, so any key the user presses will be available to the program immediately instead of waiting for them to press return before you receive the input.
I'd guess you probably also want to call noecho() so the user's input isn't echoed by getch. For the kind of program you seem to be talking about, echoing the input command is rarely desired.
Then inside your loop, you'll call getch() to get the key input data (if any). If you get input, you process it. If it returns the error code to indicate there's no input, you continue your other processing.
So the code would be structured something like:
cbreak();
noecho();
nodelay(mywindow, true);
while (1) {
int ch;
if (ERR != (ch = getch()))
process(ch);
update_screen();
do_other_processing();
}
In the case of wanting a keyboard input, and then firing a trigger only if it is there, your best bet is essentially to use an event handler style system, something akin to how games detect input. What happens is usually something like this:
Event e;
while(Poll for event e) // Basically check if something has been triggered.
{
if(event type == certain key pressed){
// Do related stuff
}
}
You will essentially put this inside your main program loop, and every time that key, or some other trigger you specify is fired, it will call a function or do something else you specified.

Ncurses: movement [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I have a problem with programming movement using C++ and Ncurses.
I'm programming a pacman and the main problem is, that I just want last pressed key in time period.
When I use usleep, it saves every key pressed during sleeping and then it's working with that in the order.
Thanks for ideas.
What did you try?
Something like this should work, if I understood correctly what you try to achieve:
int t = your_delay;
while (t --> 0)
{
sleep(1);
c = getch();
}
If you really need to time it down to the microsecond, this might not be the best approach, but if you can have some tolerance, this should be enough.
Are you in no-delay mode? If so, this might work:
usleep(your_delay);
last_key = ERR;
while ( (key=getch()) != ERR ) {
last_key = key;
}
// "last_key" now holds most recent key, if there was one, else ERR