C++函数无论输入都会运行

C++ Functions run no matter input

本文关键字:运行 输入 函数 C++      更新时间:2023-10-16

下面代码的问题是,无论控制台中的输入是什么,所有函数都像true一样运行。例如,我可以输入"vvvvvcvvvv",然后输出"你好"answers"非常感谢"。就好像函数的参数无关紧要。

#include <windows.h>
#include <iostream>
#include <string>
#include <chrono>
#include <thread>
void main() 
{
void confirmChk(bool &confirm);
bool confirm = false;
void greetingChk(bool &greeting);
bool greeting = false;
void questionChk(bool &question);
bool question = false;
void youChk(bool &you);
bool you = false;
std::string text;
std::getline(std::cin, text);
    for (int read = 0; read < text.length(); read++)
    {
        greetingChk(greeting);
        if (greeting = true);
        {
            youChk(you);
            if (you = true);
                {
                    std::cout<<"hello" <<std::endl;
                }
        }
        questionChk(question);
        if (question = true);
        {
            youChk(you);
            if (you = true);
            {
                std::cout<<"good thank you" <<std::endl;
            }
        }
    std::chrono::milliseconds dura(2000);
    std::this_thread::sleep_for(dura);
    system("cls");
    }
}
//////////////////Functions//////////////////
void greetingChk(bool &greeting)
{
    std::string text;
    std::getline(std::cin, text);
    if(text.find("hi ") != std::string::npos ||
       text.find("hello ") != std::string::npos ||
       text.find("hey ") != std::string::npos ||
       text.find("yo ") != std::string::npos || 
       text.find("sup ") != std::string::npos ||
       text.find("howdy ") != std::string::npos ||
       text.find("wazzup ") != std::string::npos ||
       text.find("hiya ") != std::string::npos)
            {
                greeting = true;
            }
    else
            { 
                greeting = false;
            }
}
void youChk(bool &you)
{
    std::string text;
    std::getline(std::cin, text);
    if(text.find("ya ") != std::string::npos ||
       text.find("you ") != std::string::npos ||
       text.find("yah ") != std::string::npos ||
       text.find("yall ") != std::string::npos || 
       text.find("you're ") != std::string::npos)
            {
                you = true;
            }
    else
            { 
                you = false;
            }
}
void questionChk(bool &question)
{
    std::string text;
    std::getline(std::cin, text);
    if(text.find("are") != std::string::npos ||
       text.find("am") != std::string::npos ||
       text.find("can") != std::string::npos ||
       text.find("could") != std::string::npos ||
       text.find("do") != std::string::npos ||
       text.find("does") != std::string::npos ||
       text.find("did") != std::string::npos ||
       text.find("has") != std::string::npos ||
       text.find("had") != std::string::npos ||
       text.find("have") != std::string::npos ||
       text.find("is") != std::string::npos ||
       text.find("may") != std::string::npos ||
       text.find("might") != std::string::npos ||
       text.find("shall") != std::string::npos ||
       text.find("should") != std::string::npos ||
       text.find("was") != std::string::npos ||
       text.find("would") != std::string::npos ||
       text.find("were") != std::string::npos)
            {
                question = true;
            }
    else
            { 
                question = false;
            }
}
void confirmChk(bool &confirm)
{
    std::string text;
    std::getline(std::cin, text);
    if(text.find("ok") != std::string::npos ||
       text.find("yup") != std::string::npos ||
       text.find("yes") != std::string::npos ||
       text.find("affirm") != std::string::npos ||
       text.find("affirmative") != std::string::npos ||
       text.find("confirm") != std::string::npos ||
       text.find("confirmed") != std::string::npos ||
       text.find("confirming") != std::string::npos ||
       text.find("endorse") != std::string::npos ||
       text.find("endorsed") != std::string::npos ||
       text.find("approve") != std::string::npos ||
       text.find("approved") != std::string::npos ||
       text.find("approving") != std::string::npos ||
       text.find("of course") != std::string::npos ||
       text.find("got it") != std::string::npos ||
       text.find("will do") != std::string::npos ||
       text.find("alright") != std::string::npos ||
       text.find("fine") != std::string::npos ||
       text.find("varify") != std::string::npos ||
       text.find("ratify") != std::string::npos ||
       text.find("validate") != std::string::npos ||
       text.find("understood") != std::string::npos ||
       text.find("justify") != std::string::npos)
            {
                confirm = true;
            }
    else
            { 
                confirm = false;
            }
}

您的if语句的条目比少女电话的条目多。:-)

您应该投资于一些结构、容器和循环。例如:

const static std::string question_words[] =  
{"am", "are", "can", "did", "could", "do", "does"};
const static unsigned int word_quantity =
    sizeof(question_words) / sizeof(question_words[0]);
//...
bool is_question = false;
for (unsigned int i = 0; i < word_quantity; ++i)
{
  if (text == question_word[i])
  {
    is_question = true;
    break;
  }
}

您可以将这个逻辑放入一个函数中,并将单词数组和单词传递给它。这样,您就可以为不同的单词容器调用函数,而不需要编写额外的代码。您的代码想要:

  bool is_question = false;
  is_question = Is_Word_In_Array(question_words, word_quantity, text);
  if (is_question)
  {
    cout << "sentence is a questionn";
  }
  if (Is_Word_In_Array(greeting_words, greeting_word_quantity, text))
  {
    cout << "sentence has a greeting.n";
  }

如果对数组进行排序,则可以使用现有的搜索函数(如std::binary_search)来查找单词。

我几乎不知道从哪里开始。主循环的执行次数与输入字符串中的字符数一样多。

您的所有if语句都是true,因为您是这样设置的。

说if(问候语=true);首先将greeting设置为true,然后什么也不做(因为条件子句后面有分号)。以下大括号内的语句将被执行,因为它们不是if的一部分。同样,因为条件子句后面有分号。

使用更像的东西

if (greeting)
{
  // conditional code here
}

我想你说的是if (greeting == true),和if (greeting)是一样的。

这里有一个问题:您使用的是赋值运算符=,而不是等式运算符==。所以,当你使用:

if (greeting = true)

你在说"将问候语设置为true。如果此操作的结果返回true,那么…"你想使用的是:

if (greeting == true)

上面写着"将问候语与true进行比较。如果此操作的结果返回true(即,如果它们相等),那么…"然而,您实际上可以通过以下操作使其更加简洁:

if (greeting)