-4

I am currently learning c++ and creating an assistant for myself. I need to make sure the if command checks for a sentence, not a word in a string how do I do that.

#include <iostream>
#include <string> 
#include <windows.h>
using namespace std;
const string YES = "yes";
const string NO = "no";
//ignore this
int main () {
    std::string Question;
    std::string check;
    std::cout << ("Hi sir how may i assist you?\n");
    //user asks the question and then a if command checks the question from a list of questions and if the line is the same as in the database it gives a output
    getline (cin, Question);
    if (Question == "whats the weather?") {
        std::cout << ("Well it is...\n");
        //then it pulls data from the web and puts it in here
    }

}

Output

Hi sir how may I assist you?
whats the weather

(nothing) else happens

user4581301
  • 33,082
  • 7
  • 33
  • 54
  • 2
    `if (Question == "whats the weather?") ...` ? – Paul Sanders May 20 '22 at 22:57
  • Why did you edit your question to invalidate my comment? That's not the done thing here ... so I've rolled it back. And what you need to do here is clarify your question, and make a better stab at it, what you're asking is much too vague for a Q&A site like this one.. – Paul Sanders May 20 '22 at 23:00
  • Depends on the definition of a sentence. More than one word and proper punctuation? – user4581301 May 20 '22 at 23:00
  • It works for me... https://ideone.com/2kq8MP `std::cout << ("Hi sir how may i assist you?\n");` and `std::cout << ("Well it is...\n");` are a little odd because there's no need for `()` but they are not syntactically incorrect. Take another look at the input you added. Is is *exactly the same* as what you're testing for? Is it maybe missing a `?`? – Retired Ninja May 20 '22 at 23:02
  • As edited, the question invalidates exiting correct answers and [performs exactly as expected](https://godbolt.org/z/YEneace9M). This makes the question not useful. – user4581301 May 20 '22 at 23:11
  • 2
    you entered 'whats the weather' but the code tests for 'whats the weather?' - it has a '?' on the end – pm100 May 20 '22 at 23:35
  • Yep. Computers am dumb. They only look smart because they're stupid really, really fast. If you don't give the computer exactly what it expects, you'll get the wrong result. Do what you can to prevent users from making mistakes because they are nearly-useless meatbags. The ones that aren't typing in bad inputs to make your program break for fun or profit are dropping near-constant typos. – user4581301 May 20 '22 at 23:53
  • Maybe you want to allow for some inexact comparison: [https://www.baeldung.com/cs/string-similarity-edit-distance](https://www.baeldung.com/cs/string-similarity-edit-distance) – drescherjm May 20 '22 at 23:56

2 Answers2

1

In this:

if (Question == whats the weather?)

Add quotes around the sentence. You want to write

if (Question == "whats the weather?")

Also, since you're using namespace std, you don't need std:: before anything.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 3
    That said, [it is generally recommended](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) that you remove the `using namespace std` rather than removing the fully qualified names. – user4581301 May 20 '22 at 23:03
  • 1
    Or specifically bring just those names into the namespace. `using std::string;`, `using std::cout;`, etc. – Chris May 20 '22 at 23:11
0

You can check for specific sentences with a syntax like:

if (Question == "whats the weather?")

However, to make your assistant more quickly usable you may instead want to lead them through some menus in the chat window, with them selecting from topics and functions. Just a suggestion to make developing your assistant easier.

Joe Moon
  • 104
  • 1
  • 10
  • 2
    Menus and other tricks to reduce the amount of typing a user must get exactly right to produce the desired response is highly rerecommended. The average human sucks at keyboard input, so if they have to get a complete sentence exactly right, it's going to take them a few kicks at the can to get right. Having to do it many times will prove a frustrating experience. Kinda kills game play. – user4581301 May 20 '22 at 23:07