-4

I'm working on a simple program where it contains 2 or 3 simple games like hang man and tic tac toe. Here when user presses ESC anywhere in b/w it should exit, also when a user presses CTRL, it should switch game. How to do that?

#include "iostream"
#include "ctime"
#include "cstdlib"
#include "cstdio"
#include <stdlib.h>    
using namespace std;

int main()
{
    cout << "Test\n" << endl;
    srand((int)time(0));
    int s, x, run = 0;
    cin >> s;
    for (int i = 0; i < 6; ++i)
    {
        int r = rand() % 6 + 1;
        if (r == s)
        {
            cout << "U:" << s << endl << "AI:" << r << endl << "OUT!!!";
            break;
        }
        cout << "U:" << s << endl << "AI:" << r << endl;
        run += s;
        system("pause");
        cin >> s;
        system("cls");
    }
    cout << "total runs =" << run << endl;
    system("pause");
}
Ron
  • 14,674
  • 4
  • 34
  • 47
Sinister
  • 1
  • 6
  • If this question already exists pls. redirect me!! – Sinister Dec 16 '18 at 14:56
  • 6
    C++ doesn't have the concept of "keys" really, and as such can not detect or handle single key-strokes. You need to use operating-system specific functions for that. – Some programmer dude Dec 16 '18 at 14:58
  • 2
    Also, all system header files (including the C++ standard header files) should be included with angle-brackets, as in `#include `. – Some programmer dude Dec 16 '18 at 14:58
  • How to include operating system specific functions?? is there any significance of "<>" because it works well with " . " . – Sinister Dec 16 '18 at 14:59
  • 4
    Ctrl does not generate a readable keypress, especially in a terminal window. As far as "how", the answer is "by doing it". Questions on stackoverflow should be specific programming related question. This is not a tutorial site, sorry. – Sam Varshavchik Dec 16 '18 at 15:00
  • Thanks for helping also down voting for some reason which i will never know!! – Sinister Dec 16 '18 at 15:07
  • @Someprogrammerdude can u pls tell where can I learn that concept at least?? – Sinister Dec 16 '18 at 15:16
  • 1
    The "concept" is very simple: Poll for a key-press event. Check what key was passed in the event. If you want more details then you have to tell us your operating system (included as a tag in your question, and preferably noted inside the question itself) and also show us some attempt. – Some programmer dude Dec 16 '18 at 15:21
  • I'm sorry pls the read question properly I just started learning c++. I can't show my work I want to learn that concept from scratch. Also the question is getting edited by someone without my permission!! – Sinister Dec 16 '18 at 15:25
  • I posted very clearly that I'm new to this forum too but it got deleted in the edit – Sinister Dec 16 '18 at 15:27
  • @Sinister - welcome to SO. In regards to your post being edited "without your permission", that is how the site works. As you gain reputation you gain the ability to do more things. One of those things is to edit the question. The edits performed were to make the question more readable - code formatting, using the keyboard markdown indicators for the key events you mentioned, etc. The reason statements like "I am new here" or "just learning" get removed is because they are unhelpful and in a sense unrelated to the question. We all were new here at some point, so don't worry and keep learning! – pstrjds Dec 16 '18 at 18:42

1 Answers1

1

You need to read input from Operating system in order to solve your problem... C++ has no concept of keys like ESC or CTRL.

You could use different libraries... I suggest you three of them:

  • SFML C++ library for multimedia stuff
  • GLFW My favourite one
  • SDL2 very popular... but I still prefer GLFW

If you're workng on linux and you don't wanna get bored with Windows stuff and GUI you can also give a look to ncurses

Elvis Dukaj
  • 7,142
  • 12
  • 43
  • 85