C++函数无论输入都会运行
C++ Functions run no matter input
下面代码的问题是,无论控制台中的输入是什么,所有函数都像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)
相关文章:
- 为什么无论你输入什么,这"while(cin.get(str,3))"只运行一次?
- 如何根据用户在C++中的输入运行不同数量的 for 循环
- 我可以根据用户输入在运行时生成"循环"吗?
- 在运行时为动态分配的内存输入值
- 在Visual Studio中运行/调试C ++时,如何在结束时等待输入
- 在 Linux 上C++:在作为 systemd 服务运行时侦听键盘输入
- 在运行时无法输入
- 需要有关运行具有输入类型的编译器的帮助
- 使用 (cin) 用户输入将其粘贴到 std::system 中,并在另一个终端中运行带有输入的命令
- C++ .我正在尝试通过使用开关内的数组来获取用户输入,但是当我运行代码时,它显示分段错误?
- 如何通过一次运行将多个输入样本馈送到C++张量流模型
- 为什么当我'y'作为输入时会循环,但当我给出'n'时运行良好
- 这段代码在获得错误输入时不会阻止 stoi 运行
- 为什么如果我从键盘输入EOF克里昂不要在运行窗口打印程序的输出?
- 如何让程序只运行用户输入的号码?
- 如何检查输入是否为字符并显示它是"invalid input",否则它将按原样运行
- 字母输入运行无限循环
- 无法为在 neovim 中运行的程序提供输入
- 在给定相同的输入的情况下,某些代码怎么可能花费更多时间来运行,这似乎只是因为它处于循环中?
- 运行输入的不同部分