Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/133.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 在if/elseif语句之间使用std::cout时发生编译错误_C++ - Fatal编程技术网

C++ 在if/elseif语句之间使用std::cout时发生编译错误

C++ 在if/elseif语句之间使用std::cout时发生编译错误,c++,C++,我想知道,当我试图在if语句和elseif语句之间使用std::cout时,为什么会出现编译错误。 例如: if (condition) {body} std::cout << "hello world" << std::endl; else if (condition) {body} 正确答案是: if (condition) { std::cout << "hello world" << std::endl; } else if (c

我想知道,当我试图在if语句和elseif语句之间使用std::cout时,为什么会出现编译错误。 例如:

if (condition)
{body}
std::cout << "hello world" << std::endl;
else if (condition)
{body}
正确答案是:

if (condition)
{
    std::cout << "hello world" << std::endl;
}
else if (condition)
{body}
if(条件)
{

这就是缩进很重要的原因

if (condition)
{   body
    std::cout << "hello world" << std::endl;
}
else if (condition)
{    
    body
}
if(条件)
{正文

std::cout让我先弄清楚:
无论条件是否满足,也就是说,无论if的主体是否执行,您都希望在条件之间执行cout语句

正如前面的评论者所指出的,您不能在if块的作用域末尾和
else
关键字之间放置某些内容

通过将if-else-if块拆分为两个单独的if块来实现这一点怎么样:

if (condition1) {
    body1
}
cout << "hello world" << endl;
if (!condition1 && condition2) {
    body2
}
if(条件1){
车身1
}

这将是你的一个选择

   if (fistCondition)
   { 
        /*code for fistCondition*/ 
        std::cout << "hello first" << std::endl;
   }
   else if (secondCondition)
   {
       /*code for secondCondition*/ 
       std::cout << "hello second" << std::endl;
   }

在这种情况下,
&&!firstCondition
模拟一个
else
关键字用于您的目的。

除了if和else if循环的封闭体之外,您不能在if和else if之间添加任何可执行代码

if (firstCondition)
{ 
    /*code for firstCondition*/ 
    //code anything here   
}
//not here #######
else if (secondCondition)
{
    /*code for secondCondition*/ 
    //code anything here
}

你忘了把
cout
放在
if
块内(在
{…}
之间)。输出应该在
if
主体之外吗?没有“else-if”语句。条件的形式是
if(condition)主体
if(condition)body1 else body2
body2
当然可以是相同形式的条件。为什么奇怪?错误消息很清楚;-)这也是缩进不重要的原因。对编译器来说,缩进语句不会以任何方式改变代码的含义。例如,通过将其与前面的语句分组。您是对的@Pet呃,我的意思是正确的缩进会使错误变得显而易见……让我们就“这就是为什么自动缩进有帮助”达成一致吧?@downvorters:请评论!谢谢!
     if (fistCondition)
     { 
         /*code for fistCondition*/ 
         std::cout << "hello first" << std::endl;
     }
     std::cout << "posterior to first and prior to second if statement" << std::endl;
     if (secondCondition && !firstCondition)
     {
         /*code for secondCondition*/ 
         std::cout << "hello second" << std::endl;
     }
if (firstCondition)
{ 
    /*code for firstCondition*/ 
    //code anything here   
}
//not here #######
else if (secondCondition)
{
    /*code for secondCondition*/ 
    //code anything here
}