Make an input dynamic

I was curious to know that how can I make my code dynamic like when a user gives some input like 'switch on TV' then it will not be limited to that string but it can be dynamic like 'turn on the tv' or 'can you please turn on the tv'.
Please help I'm a begginer.
At the simplest level, you check for each of the strings you care about:

1
2
3
4
5
6
7
8
9
10
string input;

cin >> input;

if (input == "switch on TV" ||
    input == "turn on the tv" ||
    input == "can you please turn on the tv")
{
  turn_on_tv();
}


More sophisticated than that, you can "parse" the input. Break it into pieces and try to identify the patterns and parts of it.
As a beginner, you should focus on all of the skills that are part of the study of programming before you get into a design question like this.

It is as if you're in high school, in Algebra 1, insisting on understanding computational geometry.

Having a computer parse English is incredibly complex to do right... I mean, cell phones and other cylindrical spyware devices can do it well enough, but still only based on relatively simple commands.
At this point, I would just make it so the program can detect certain combinations of keywords. e.g. if the word contains both "on" and "tv".

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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Example program
#include <iostream>
#include <string>
#include <sstream>
#include <cctype>
#include <algorithm>

enum class Action {
    Unknown,
    TV_off,
    TV_on,
};

enum class NullableBool
{
    Null,
    Off,
    On
};

using namespace std;

string to_lower(string str)
{
    std::transform(str.begin(), str.end(), str.begin(),
        [](unsigned char c){ return std::tolower(c); });
    return str;
}

Action parse(const std::string input)
{
    bool tv = false;
    NullableBool turn_on = NullableBool::Null;
    
    istringstream iss(input);
    string word;
    while (iss >> word)
    {
        string lower = to_lower(word);
        if (lower == "tv")
        {
            tv = true;
        }
        else if (lower == "on")
        {
            turn_on = NullableBool::On;
        }
        else if (lower == "off")
        {
            turn_on = NullableBool::Off;
        }
    }
    
    if (tv && turn_on == NullableBool::On)
    {
        return Action::TV_on; 
    }
    else if (tv && turn_on == NullableBool::Off)
    {
        return Action::TV_off;
    }
    else
    {
        return Action::Unknown;   
    }
}

int main()
{
    cout << "What should I do?\n>";
    string input;
    getline(cin, input);
    
    Action act = parse(input);
    
    switch (act)
    {
      case Action::TV_on:
        cout << "Okay. Turning the TV on.\n";
        break;
      case Action::TV_off:
        cout << "Okay. Turning the TV off.\n";
        break;
      default:
        cout << "I didn't quite get that.\n";
        break;
    }
}


What should I do?
>Turn TV off
Okay. Turning the TV off.


What should I do?
>turn on tv
Okay. Turning the TV on.


What should I do?
>I hope you malfunction
I didn't quite get that.


1
2
cin >> input;
if (input == "switch on TV" ||

Note you'd have to use getline(cin, input); here if you want the string to contain spaces.

Edit: As Niccolo said, if you're an absolute beginner, even just parsing a string like this could seem overwhelming. I would just stick to more simple stuff, follow books/tutorials first. [Not that I'm implying that my example is really that complex. But if you start trying to add different commands and more dynamic phrases to it, it will blow up in complexity.]
Last edited on
Topic archived. No new replies allowed.