#is there a way to ignore user input? like for an example

27 messages · Page 1 of 1 (latest)

sharp meadow
#

For an example I have a while loop and then there's stuff inside of it and some user input. how do I make it so that the code won't wait for the person input, instead it ignores it if there's no input and just keeps running without waiting for the person to input smth

tame steepleBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

sharp meadow
#

I'll be asleep, ill check tomorrow bye

noble goblet
#

The proper way to not wait for input is to not ask for the input really

#

What behavior are You trying to achieve?

#

Like

#

The point of asking for the input is to get the input

#

Or maybe

#

You want getline?

#
std::string a;
std::getline(std::cin, a);
#

It accepts empty line as a proper input
User just have to press enter

late moss
#

Just don't use it?

#include <iostream>

int main()
{
    int x {}:
    std::cin >> x;
    
    //any other code here not using x

    return 0;
}
sharp meadow
#

like a while true do loop, how do ppl make those snake game without needing to wait ppl input for the code to run

#

@late moss

valid wren
#

@sharp meadow I think you could achieve this by using threads, so that your program can run concurrently with user input. (User input not blocking the rest of the program)

tame steepleBOT
stark geyser
# sharp meadow but what if I want to run the code under it

_getch() from <conio.h> is one option for you.
int key = _getch();

Over all though, system specific.

#include <Windows.h>
#include <chrono>
#include <iostream>
#include <thread>

bool isKeyPressed(int key) { 
  return GetKeyState(key) & 0x8000; 
}

int main() {
  // All key codes/defines
  // https://learn.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
  bool isOpen{true};
  while (isOpen) {
    if (isKeyPressed(VK_ESCAPE)) {
      std::cout << "* Closing!\n";
      isOpen = false;
      break;
    } else if (isKeyPressed(VK_UP)) {
      std::cout << "Key: ArrowUp\n";
    } else if (isKeyPressed(VK_RIGHT)) {
      std::cout << "Key: ArrowRight\n";
    } else if (isKeyPressed('Q')) {
      std::cout << "Key: Q\n";
    }

    // this is not a good way to slow the game down
    // not consistent, but maybe good enough for you
    // meaning sometimes it will "sleep" for more than 100ms
    std::this_thread::sleep_for(std::chrono::milliseconds(100));
  }
  return 0;
}
sharp meadow
stark geyser
#

I mean let's start with something.

stark geyser
#

GetKeyState is a Windows specific function, it returns whether the key was pressed/toggled.
If certain bit in returned value is set to be true (hexadecimally 0x8000th bit) then it means that the key is pressed.
I'm using & ergo and bitwise operator to check if 0x8000th bit is set to 1.
How & works? Say you have two numbers represented in binary:
0``0``0``1``1 == 3 in decimal
0``0``1``1``1 == 7 in decimal
------------------- & we will use and operator & on them
0``0``0``1``1 == 3
Where & "finds" two 1 in both variables it operates on it will return 1 for this bit, in every other case it will return 0, that's why third bit from the right was turned to 0.

I imagine you know what bool type is, but if not then let's just say that it's a data type of minimal number of bits that serves as indicator whether something is "on" or "off", whether something is "true" or "false", in truth though it can hold numbers since usually at least there's minimal size that the processor deals with like 8 bits for example, so don't be suprised in case you do something bitwise.

bool isKeyPressed(...) simply asks system for state of a specific key and checks whether it's pressed.

Things like VK_UP are simply constants that refers to key with VK_ prefix, not all keys seem to have these type of constants, but you can usually use letter literals for these like with Q in the example.

std::this_thread::sleep_for(...) here let's us freeze console thread for a little bit as to not take out of a processor core all the juices, no need to constantly ask for key input or keep displaying or what not. You can think of it to be similar to limitting fps, but in a poor way.
Thing is while(true would keep going as fast as the processor allows it too, stressing the cpu core for no reason, you don't need this much power most likely so it's better to freeze the thread your progrma is using for a tiny little bit where it's not noticable.

#

@sharp meadow

#

std::chrono::milliseconds(100) is rougly self-explainatory, you can think of it as data structure representing time, here representing milliseconds that sleep_for expects (you can define it by nanoseconds if you want).

late moss