如何在句子中使用if命令

  • 本文关键字:if 命令 句子 c++
  • 更新时间 :
  • 英文 :


我目前正在学习c++并为自己创建一个助手。我需要确保if命令检查的是一个句子,而不是字符串中的一个单词。我该怎么做呢?

#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
}
}

输出
Hi sir how may I assist you?
whats the weather

(没有)其他事情发生

在此:

if (Question == whats the weather?)

在句子周围加引号。你想写

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

同样,因为你是using namespace std,你不需要在任何事情之前需要std::

您可以使用以下语法检查特定的句子:

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

然而,为了让你的助手更快速地可用,你可能需要引导他们通过聊天窗口中的一些菜单,让他们从主题和功能中进行选择。只是一个建议,让你的助手更容易发展。