-10

I cant put cin>>x in the while(true) loop because it stops it from runing until i press a key then runs 1 time and i have to press a key again (i want the loop to run continuously)

I tried putting the cin<<x in the while(true) loop but it stops the loop until i press a key

  • 3
    Before posting any questions on stackoverflow.com, everyone should take the [tour], read the [help], understand all the requirements for a [mre] and [ask] questions here. Not doing any of this results in a poor quality question almost every time. It then gets downvoted, closed, and then deleted. Repeated low-quality questions may result in a temporary ban from asking new questions. – Sam Varshavchik Aug 31 '23 at 20:25
  • 2
    You want to *write* to standard *input*? – tadman Aug 31 '23 at 20:26
  • Note: `kbhit` and `getch` are not Standard C++ functions and may not be present on your system. If they are, you will have to include headers for and link in additional libraries. – user4581301 Aug 31 '23 at 20:38
  • @Joel a non blocking input in a infinite loop sounds like exactly what i need (i am not that experienced so i didnt know how they are called) i added the "if (kbhit()) { x = getch(); }" but i got some errors on kbhit() nad getch(), do i need any special library for them? – Andrei Farkas Aug 31 '23 at 20:39
  • Yes, you need the Borland library that has them, and link to that library, and don't forget `#include `. – Eljay Aug 31 '23 at 20:40
  • If writing for DOS (Yeah, the operating system your grandparents used) or Windows, add `#include ` to the includes at the top of the file. If writing for anything else, you need to do a lot more work, so specify the OS you are targeting so we can tell you what that extra work is. – user4581301 Aug 31 '23 at 20:40
  • @Joel i added that and the #include library but i get this error 'kbhit': The POSIX name for this item is deprecated. Instead, use the ISO C and C++ conformant name: _kbhit. See online help for details. – Andrei Farkas Aug 31 '23 at 20:47
  • @user4581301 i am using windows 11 and visual studio – Andrei Farkas Aug 31 '23 at 20:48
  • You can either just do as they say and use the functions with the `_` prefix, or you could use `#pragma warning (disable : 4996)` at the top of your file to get rid of the warning, but I would suggest using `_kbhit()` and `_getch()` instead of just blindly disabling warnings. – Joel Aug 31 '23 at 20:49
  • Handy reading: [Non-blocking console input C++](https://stackoverflow.com/questions/6171132/non-blocking-console-input-c) – user4581301 Aug 31 '23 at 20:55
  • @Joel i added underscores before kbhit() and getch() and it is non blocking, the only problem is that now the input part doesnt really work (i am using visual studio with c++ 20 if that maters ) – Andrei Farkas Aug 31 '23 at 20:56
  • 1
    Can you explain further what "doesnt really work" means? – Joel Aug 31 '23 at 20:58
  • @Joel oh sorry i just realized that in english doesnt really work means that it works a bit(my native language is not english) anyway i though why not replace getch() with cin>>x; and now it works :), i cant thank u enough for ur help – Andrei Farkas Aug 31 '23 at 21:00
  • The information Joel is looking for is what happens? Do you get error messages and warnings when building? If so provide them. If the program builds and fails to run as expected, describe what you expected to happen and what happened instead. Simply saying any variant of "It doesn't work" contains too little information for us to help you. – user4581301 Aug 31 '23 at 21:04
  • Well, what is that supposed to mean now? Does it not compile, does `_getch()` not return the expected value? If you want to get an `int` like `5` as input, you would get the corresponding ascii value if I am not mistaken. So you would have to do something like `x - '0'`, or use more complex logic for numbers with multiple digits. – Joel Aug 31 '23 at 21:05
  • @Joel if i had _getch() i just could write stuff in the console, but i replaced it with cin>>x; and now it works as i want it to so u dont have to worry – Andrei Farkas Aug 31 '23 at 21:09
  • Maybe because `_getch()` does not wait for a newline but `std::cin` does? I don't know what exactly you are trying to achieve but I am glad if I could help you get to a working solution. – Joel Aug 31 '23 at 21:11
  • You explained why `cin>>x` cannot be in your loop, but you never gave a reason for wanting it in the first place. Based solely on the information in the question, the somewhat obvious solution is to remove `cin>>x` from your loop. That meets all stated criteria (the loop will no longer wait for you to press a key). – JaMiT Sep 01 '23 at 02:53

0 Answers0