错误:预期的不合格ID,否则'否则'

时间:2015-02-03 14:24:08

标签: c++ if-statement

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

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)

我不知道该怎么做。我试过添加/删除&#34;}&#34;当我换一个支架时,我得到15个以上的错误,所以我不认为这是问题。

3 个答案:

答案 0 :(得分:0)

从代码的这一部分看起来你有太多的结束括号,重新格式化代码并再次检查。

编辑:或者开头括号太小,无论如何,在第一个答案中,正确的格式化将帮助您。

答案 1 :(得分:0)

你的第二个其他陈述,不属于那里。如果(userInput == 3)阻止,则将其删除并在else之后输入。

答案 2 :(得分:0)

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

} else if (userInput == 3)}}没有必要,太多了

并且在另一行代码中也缺少一个括号,其中包含10多个错误。再次感谢!