Program waiting for user input.

Hello, my program pausing and waiting for user input. How to fix that?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <windows.h>

std::string input;

bool triggerEnabled(){
    if(input == "trigger on"){
        return true;
    }
    if(input == "trigger off"){
        return false;
    }
    return false;
}

int main(){
    while(true){
        if(triggerEnabled()){
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x2F);
            std::cout << "TRIGGER" << std::endl;
        }
        if(!triggerEnabled()){
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x4F);
            std::cout << "TRIGGER" << std::endl;
        }
        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0F);
        std::cout << "test";
        std::getline(std::cin, input);
        system("cls");
        Sleep(100);
    }
    return 0;
}


This loop have to show current status of "TRIGGER" (green = turned on, red = turned off) and user can change the "TRIGGER" state.
Last edited on
Look at SetConsoleMode(); turn off line input and turn off echo.

Good luck!
I dont want to block userinput..
Well, when you can’t lead a horse to water, he will never drink...

Funny how the horse says he’s thirsty tho.
Fixed by this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <windows.h>
#include <conio.h>

std::string input;

bool triggerEnabled(){
    if(input == "trigger on"){
        return true;
    }
    if(input == "trigger off"){
        return false;
    }
    return false;
}

int main(){

     while (true) {
        if(triggerEnabled()){
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x2F);
            std::cout << "TRIGGER" << std::endl;
        }
        if(!triggerEnabled()){
            SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x4F);
            std::cout << "TRIGGER" << std::endl;
        }

        SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), 0x0F);
        if(kbhit()){
            std::getline(std::cin, input);
        }
        Sleep(100);
    }

    return 0;
}


Conio.h is good in ur opinion?

Now, when I type other word than "TRIGGER ON" or "TRIGGER OFF" this bool always set to FALSE. I want to do this only react to these 2 "commands" (on or off).
Last edited on
Your question was a bit contradictory is all. You wanted the program to not stop for user input while also wanting user input. Conio.h - For getch()? I used it once for something but usually using iostream for input is just fine. If you want the user to input all of "TRIGGER ON" and "TRIGGER OFF", you'll end up typing a bit of code to check if all the letters typed lead to one of the two words rather than just using cin or getline.
Registered users can post here. Sign in or register to post.