Read user input, output relevant message

Hi, I am trying to create a program that reads one-word user input and outputs a relevant message. What I have so far can only read one word and give one response. It also outputs an "invalid" message when the user inputs a message that does not match any words in the "word bank". I need it to have a bank of words it can recognize and a relevant message for each, but I can't seem to get it right. Here is what I have so far:


#include <regex>
#include <string>
#include <iostream>
int main()
{
int x = 1;
while (x < 999999999999999999999) {
using namespace std;
string sentence;
cin >> sentence;
string word = "agent";
string word2 = "jam";
regex q("\\b" + word2 + "\\b");
regex r("\\b" + word + "\\b");
smatch s;
if (regex_search(sentence, s, q)) {
cout << "poop";
}
else if (regex_search(sentence, s, r)) {
cout << "are you ONEHUDRED percent confident that, your agent can get you top dollar for your property? ";
}
else {
cout << "invalid input";
}
x + 1;
}
system("pause");
return 0;
}


I have limited experience in Java and C# (we're talking days here) and next to none with C++. Any help would be greatly appreciated!

EDIT: So I figured out how to put in different words, but now the problem is that the program closes right after the user gets the output. I would like the program to allow the user to type in another word and get another message, then another word for another message, etc., instead of forcing the user to restart the program every time they want to search a new word. I think I can use a "while" loop for this, but I'm not sure how to implement it. Ideas?

EDIT2: Welp, I figured it out, though this method is very crude. The user can essentially keep inputting new words until he/she manually exits the program. Or until he/she has input 999999999999999999999 times. Or he/she dies. Whichever comes first.
Last edited on
I need it to have a bank of words it can recognize and a relevant message for each ...
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
# include <iostream>
# include <string>
# include <vector>
# include <algorithm>

struct MatchingResponse
{
    std::string m_input;
    std::string m_response;
};

int main()
{
    std::vector<MatchingResponse> data{ {"how", "are you?"}, {"happy", "new year"}, {"long", "live the revolution"},
                                    {"dog", "ate my homework"}};
    std::cout << "enter input \n";
    std::string input;
    getline(std::cin, input);
    auto itr = std::find_if(data.cbegin(), data.cend(), [&input](const MatchingResponse& mr){return mr.m_input == input;});
    if (itr != data.cend())
    {
        std::cout << (*itr).m_response << "\n";
    }
    else
    {
        std::cout << "the input string doesn't exist in the word bank \n";
    }
}

convert input string to upper/lower case throughout first if required
Last edited on
Topic archived. No new replies allowed.