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

时间:2014-09-12 22:50:43

标签: c++ function

以下代码的问题是,无论控制台中的输入是什么,所有函数都运行,就像它们是真的一样。举个例子,我可以输入“vvvvvvvvvvvvvvvv”并输出“hello”和“good thank you”。它好像功能的参数无关紧要。

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

3 个答案:

答案 0 :(得分:0)

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

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

说         if(greeting = true); 首先将greeting设置为true,然后不执行任何操作(因为条件子句后面的分号)。 括号内的以下语句将被执行,因为它们不是if的一部分。再次,因为条件子句后面的分号。

使用更像

的内容
if (greeting)
{
  // conditional code here
}

我认为你的意思是if (greeting == true)if (greeting)相同。

答案 1 :(得分:0)

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

if (greeting = true)

...你在说&#34;将问候语设为真。如果此操作的结果返回true,则...&#34;您想要使用的是:

if (greeting == true)

这说&#34;比较问候语为真。如果此操作的结果返回true(即如果它们相等),那么......&#34;但是,你可以用这个更简洁一点:

if (greeting)

答案 2 :(得分:0)

您的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 question\n";
  }
  if (Is_Word_In_Array(greeting_words, greeting_word_quantity, text))
  {
    cout << "sentence has a greeting.\n";
  }

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

相关问题