C++ 使用嵌套if语句中的函数触发操作

C++ 使用嵌套if语句中的函数触发操作,c++,function,if-statement,boolean,C++,Function,If Statement,Boolean,下面显示了带有if语句的主程序。如果If语句为true,则它将继续触发函数 如果函数中的内容为真,则触发最终操作。来自aux函数的,x仍然显示为未声明 我做错了什么 void aux(bool x) { std::string text; std::getline(std::cin, text); if (text.find("be ") != std::string::npos || text.find("do ") != std::string::np

下面显示了带有if语句的主程序。如果If语句为true,则它将继续触发函数

如果函数中的内容为真,则触发最终操作。来自aux函数的,
x
仍然显示为未声明

我做错了什么

void aux(bool x) {
    std::string text;
    std::getline(std::cin, text);
    if (text.find("be ") != std::string::npos ||
        text.find("do ") != std::string::npos ||
        text.find("can ") != std::string::npos ||
        text.find("but ") != std::string::npos) {
        x = true;
    }
}

int main() {
    std::string text;
    std::getline(std::cin, text);
    if (text.find("7 ") != std::string::npos) {
        aux();
        {
            if (x);
            {
                std::cout<<"A function to equal a group of words"<<std::endl;
            }
        }
    }
    getchar();
    return 0;
} 
void辅助(bool x){
std::字符串文本;
std::getline(std::cin,text);
if(text.find(“be”)!=std::string::npos||
text.find(“do”)!=std::string::npos||
text.find(“can”)!=std::string::npos||
text.find(“but”)!=std::string::npos){
x=真;
}
}
int main(){
std::字符串文本;
std::getline(std::cin,text);
if(text.find(“7”)!=std::string::npos){
aux();
{
if(x);
{

std::cout似乎您不知道局部全局变量

您可以使用按引用传递来解决以下问题:

void aux(bool &x)
{
    std::string text;
    std::getline(std::cin, text);
    if(text.find("be ") != std::string::npos || text.find("do ") != std::string::npos ||
       text.find("can ") != std::string::npos || text.find("but ") != std::string::npos)
            {
                x = true;
            }
else{ 
  x = false;
  }
}

但是您没有通过将任何变量作为参数/参数传递来调用它。请执行以下操作:

aux(var1);//var1 is boolean variable

2.x中的变化不会出现在那里,因为x将被视为aux(var1)函数中的一个新变量。因此,如果使用通过引用传递,则变化将持续到变量(此处为x)。

您似乎是在假设“x”应可用于主()调用aux()后的函数。C/C++不会以这种方式工作-在主函数的作用域中没有“x”,因为您从未声明任何名为x的东西

此外,除非从aux()函数返回x并将其赋给main函数中的某个对象,否则它不会具有所需的值

您应该熟悉C/C++中的“范围”主题,因为这将防止进一步的误解,但您需要用示例解决的基本问题是:

  • 您没有将bool传递给aux()函数,因此它无法工作(它是aux()的一个参数)
  • 您没有从aux函数返回x的值
  • 您不需要在主函数中声明布尔值来获取aux()函数返回的值

完全不清楚您对布尔x的期望值是什么?如果您想更改x,并且x超出了变量,则需要通过引用传递它或返回值。返回值更符合逻辑

bool
aux()
{
    std::string text;
    std::getline(std::cin, text);

    if(text.find("be ") != std::string::npos  ||
       text.find("do ") != std::string::npos  ||
       text.find("can ") != std::string::npos ||
       text.find("but ") != std::string::npos)
    return true;

    return false
}
现在,使用aux()的结果而不使用随机值悬挂x也是合乎逻辑的:

int main()
{
  std::string text;
  std::getline(std::cin, text);

  if (text.find("7 ") != std::string::npos)
  {
     if (aux()) // We dont need the variable here since we use return value of the aux
       std::cout<< "A function to equal a group of words" << std::endl;
     else
       std::cerr << "Bad string detected" << std::endl;
  }

  getchar();
  return 0;
}
intmain()
{
std::字符串文本;
std::getline(std::cin,text);
if(text.find(“7”)!=std::string::npos)
{
if(aux())//这里不需要变量,因为我们使用aux的返回值

std::cout您可能希望更改
aux
函数以返回布尔值,而不是将其作为参数:

bool aux() {
    if (/* your checks here */) {
        return true; 
    } else {
        return false;
    }
}
函数名左侧的
bool
代替
void
表示调用函数的结果将是布尔值

然后在
main
中使用
aux
如下:

void aux(bool x){
//your code here
}
if (aux()) {
    std::cout<<"A function to equal a group of words" <<std::endl;
}
if(aux()){

std::COUT此操作修复了编译器错误,但没有修复太多其他错误。如果未在
aux
中设置为
true
,则读取
x
将导致未定义的行为。调用
aux
后会有多余的大括号无效。无论
x
如何,
中的文本都将始终输出我没有注意到这些。谢谢,我现在已经按照你的建议进行了更新。谢谢。这只是构建它。不断得到错误:函数不带1个参数。我将尝试解决它。谢谢。这确实运行了;但是,在aux运行后,我没有得到最后的cout。知道吗?莱娜,什么是文本?在哪里声明?(也位于主体中。只是忽略它。)STD::String文本;STD::GETLIN(STD::CIN,文本)。它不在代码中。请更新,否则看起来很奇怪。再次建议我从一本好书中学习C++。有一本书,读它,并且总是研究。只是尝试应用。
if (aux()) {
    std::cout<<"A function to equal a group of words" <<std::endl;
}