非法的其他没有匹配的if

时间:2017-01-10 22:34:44

标签: c++

所以我试着建立一个简单的计算器,但我一直得到同样的错误"非法的其他没有匹配,如果"在我把第二个其他的搜索后,我搜索了另一个答案,但似乎没有任何帮助,如果有人能给我任何解决方法,我将不胜感激

   #include "stdafx.h"
   #include <iostream>

   using namespace std;

   int main() {

    int number_1;

    cout << "" << endl;
    cin >> number_1;

    int number_2;

    cout << "" << endl;
    cin >> number_2;

   int number_3 = number_1 + number_2;

   int number_4 = number_1 - number_2;

   int number_5 = number_1 * number_2;

   int number_6 = number_1 / number_2;

   if (number_1 + number_2) {

    cout << "" << number_3 << endl;
   }

   else {

    if (number_1 - number_2) {

        cout << "" << number_4 << endl;
    }
   }

   else {

    if (number_1 * number_2){ 
        cout << "" << number_5 << endl; 4
    }
         }


  else {

    if (number_1 / number_2) {

        cout << "" << number_6 << endl;
    }

   }

   cout << "Press enter to continue" << endl;
   cin.ignore(10, '\n');
   cin.get();


   return 0;
}

2 个答案:

答案 0 :(得分:2)

那是因为每个else语句(不包括最后一个块)必须与if块相关联。如果您想继续迭代这些选项,就像您正在尝试一样,您需要将代码更改为:

#include "stdafx.h"
#include <iostream>
using namespace std;

int main() {

    int number_1;

    cout << "" << endl;
    cin >> number_1;

    int number_2;

    cout << "" << endl;
    cin >> number_2;

    int number_3 = number_1 + number_2;
    int number_4 = number_1 - number_2;
    int number_5 = number_1 * number_2;
    int number_6 = number_1 / number_2;

    if (number_1 + number_2) 
        cout << "" << number_3 << endl;
    else if (number_1 - number_2) 
        cout << "" << number_4 << endl;
    else if (number_1 * number_2) 
        cout << "" << number_5 << endl; 4
    else if (number_1 / number_2) 
        cout << "" << number_6 << endl;

    cout << "Press enter to continue" << endl;
    cin.ignore(10, '\n');
    cin.get();

    return 0;
}

在我所知道的每种语言中,如下所述的陈述都是非法的:

if (/*some condition*/)
    /*do some action*/
else
{
    if (/*some condition*/)
        /*do some action*/
    else
        /*do some action*/
}
/*You're fine until right here...  The else is ALWAYS your last statement*/
/*It essentially says: "If we didn't meet ANY of our conditions, do this...*/
//Once you try doing this:
else
{
    //try doing something
}
//YOU MESSED UP
//We've already passed all the parameters we were testing

请记住:if(/*some condition*/)后面跟着你需要的else if(/*some condition*/)个,以及结束你的else阻止。

ALSO

应该注意的是,您实际上并未测试if语句中的任何条件 - 您正在完成操作。您绝对必须检查某种值(布尔值,数字或其他...)例如:

if (number_1 + number_2 != 0) 
    cout << "" << number_3 << endl;
else if (number_1 - number_2 != 0) 
    cout << "" << number_4 << endl;
else if (number_1 * number_2 != 0) 
    cout << "" << number_5 << endl; 4
else if (number_1 / number_2 != 0) 
    cout << "" << number_6 << endl;
else cout << "Invalid number." << endl;

答案 1 :(得分:1)

当我看到你的直接问题和你提供的代码时,我看到已经回答了你的问题的答案和评论,首先要记住的是一个简单的规则,也应用了这样做的风格

  • if ... else if else {}阻止陈述的规则。

    • 第一个语句以if(条件||一组条件)开头,它们将通过条件布尔比较或逻辑数学评估比较评估为true或false
      • 实施例
        • 布尔值:if ( something (x) is true or false )
        • 逻辑:If ( somevalue (x) is <, >, <=, >=, !=, == somevalue (y)or someFunction or operation done to either (x) or (y) or both )
    • 下一步是分配块语句的开头,这可能会略有不同:
      • 当且仅当if语句后面有一行执行时,可以省略开括号{}并且可以后跟if else( ... )else语句。
      • 如果需要在这些scope的{​​{1}}中执行多行代码执行,则需要使用开始和结束括号block statements来定义封闭块声明以定义其范围。 *如果不确定查找{}scope:所有块 - 以block开头且以{结尾的代码段都有自己的本地范围。
    • 返回: - 当在一个不返回值的函数中找到这些}语句时,唯一应该调用的是if ... else if ... else语句,当条件给出从该返回的结果时功能;并非所有return;状态都会导致函数终止但很多都会终止,否则如果该函数确实有if else if else并且在return value语句范围内执行某些代码块会导致要从该函数返回,那么该语句的所有块也应返回该函数数据类型或抛出某种类型的错误或异常。
  • <强>风格

    • 打开和关闭括号的缩进和使用if else if else
    • 间距
    • 嵌套循环

      {}

我更喜欢带有起始括号的紧凑版本,它为以下任何内容定义了一个新的作用域:命名空间,类声明,函数定义,枚举,switch语句及其case语句,if ... else if else语句,while和while循环,for循环,try和catch语句。我更喜欢默认选项卡或4个空格,用于所有嵌套代码块语句或新(嵌套范围)的缩进。我更喜欢保留我的其他if,而不是在初始if语句的右括号的同一行,以便我知道它属于该代码分支。实施例

<强> MyStyle.cpp

// Here are a couple of different styles
// Tip about indentation - single tab normally 4-5 white spaces
// with 4 being most common sometimes 3, but very rare: 
// I prefer 4. Which ever indent spacing you decide to choose 
// make sure all of your code follows that convention.

// Style 1a: Single Execution with omitted braces
if ( some condition )  // Braces {} omitted    
    // single execution;  // Single Execution without any following 
                        // else if...else blocks
// Next statement of execution that doesn't belong to the above if;

// Style 1b: Single Execution with braces
if ( some condition ) 
{                       // Beginning Brace Started on separate line.
   // single execution;
}            
// next statement not belonging to if statement;

// Style 1c: Single Execution with braces
if ( some condition ) { // Opening Brace on Same Line as if statement.
    // some execution;
}
// next statement not belonging to if statement;

// Style 2: Concerning  else, if else, and if else else when using braces and indentation
// Style 2a: Opening and closing block statement as well as conditions on separate lines.
if ( condition ) 
{
    // Statement or statements of execution;
} 
else if ( some other condition )
{
    // Statement or statements of execution;
}
else
{
    // Statement or statements of execution;
}

// Style 2b: More compact
if ( condition ) {
    // Statement or statements of execution;
} else if ( some other condition ) {
    // Statement or statements of execution;
} else {
    // Statement or statements of execution;
}

// These can be applied to nested loops as well.

关于你选择的风格的一般想法应该是:选择适合你的东西,坚持使用那种风格,只要该风格保持可读性并保持简单,以便其他读者或用户轻松遵循代码块或遵循你的学校,工作或团队使用的风格。

关于你的代码与你的if语句的条件有关,请考虑这一点。

int main() {
    if ( condition || conditions ) {
        // Do Work;

     } else if ( some other condition || conditions ) {
        // Do This Instead;

     } else {
        // Otherwise; Finish with this;
     }

     // Nested If else if else
     if ( ... ) {
         if ( ... ) {
             // code;

         } else if ( ... ) {
             // code;

             if ( ... ) {
                 // code;
             } else {
                 // code;
             }

             // maybe more code;
         } else {
             // some other code;
         }
    } else if ( ... ) {
        if ( ... ) {
            // code;
        } 
        if ( ... ) {
            // code;
        }
        if ( ... ) {
            // code;
        } else {
            // code;
        }
    } else {
        // code;

    }

    return 0;
}