错误:"否则"之前应为非限定 ID

error: expected unqualified-id before 'else'

本文关键字:ID 否则 错误      更新时间:2023-10-16

我已经尝试检查其他答案,但仍然找不到答案。我收到此错误:

compilation info
prog.cpp:192:2: error: expected unqualified-id before 'else'
  else if (userInput == 3)
  ^

下面是代码行:

else if (userInputSolveFor == "t")
        {
            cout << "You are solving for time." << endl;
            cout << endl;
            cout << "What is the acceleration (in m/s/s)?" << endl;
            cin >> acceleration;
            cout << "What is the initial velocity?" << endl;
            cin >> initialVelocity;
            cout << "What is the distance (in meters)?" << endl;
            cin >> deltaDistance;
            if (initialVelocity == 0)
            {
                cout << "The time is " << sqrt(deltaDistance - (0.5 * acceleration)) << setprecision(10) << " seconds." << endl;
            }
            else if (acceleration == 0)
            {
                cout << "The time is " << (deltaDistance / initialVelocity) << setprecision(10) << " seconds." << endl;
            }
            else 
            {
                cout << "The input you have entered is not valid." << endl;
            }
        }
    }
        else
        {
            cout << "The input you have entered is not valid." << endl;
        }
    }
    else if (userInput == 3)
    {
        cout << "You are using vf^2 = vi^2 + 2(a)(x)." << endl;
        cout << endl;
        cout << "What are you trying to solve for?" <<endl;
        cout << endl;
        cout << "Use vf for final velocity (in m/s)." << endl;
        cout << "Use vi for initial velocity (in m/s)." << endl;
        cout << "Use a for acceleration." << endl;
        cout << "Use x for distance (in meters)." <<endl;
        cin >> userInputSolveFor;
        cout << endl;

其中 192 行是

else if (userInput == 3)

我不知道该怎么办。我尝试添加/删除"}",当我更改单个大括号时出现 15+ 错误,所以我认为这不是问题所在。

从这部分代码来看,您似乎有太多的右括号,请重新格式化代码并再次检查。

编辑:或者左括号太少,无论如何,就像在第一个答案中一样,正确的格式将对您有所帮助。

你的第二个 else 语句,不属于那里。 把它剪下来,在else if(userInput==3)块之后输入。

我已经解决了这个问题...我有一个太多的括号,我缺少一个括号。谢谢大家的帮助,我对编码还很陌生,非常感谢所有的帮助。到目前为止,我正在使用Notepad++,并试图在它成为坏习惯之前学习正确的缩进。再次感谢大家。详细答案:

} else if (userInput == 3) } 不是必需的,太多了

并且在另一行代码中还缺少括号,这导致了 10+ 错误。再次感谢!