c++ - 代码 : expected ' ; ' before ' { ' token -- what is causing this? 中的解析错误

标签 c++

我得到的错误是 error: expected ' ; ' before ' { ' token

我尝试通过添加 ; 来修复代码在 if (thisisanumber==5) 之后以及 else (thisisanumber!=5) 之后.虽然这解决了第一个错误,但它会产生另一个错误 error: ' else ' without a previous ' if ' .我真的很想知道我在编写代码时犯了什么错误,谢谢。

这是我的代码:

#include <iostream>

using namespace std;

int main()
{
    int thisisanumber;

    cout<<"Whats the Password?: ";
    cin>> thisisanumber;
    cin.ignore();
    if (thisisanumber==5) {
        cout<<"You've discovered the password! Wow, you're a genious you should be proud./n";
        }
    else (thisisanumber!=5) {
        cout<<"You've failed in knowing the password and therefore cannot enter, leave and do not come back. Goodbye!/n";
        }
    cin.get();
}

最佳答案

您缺少关键字 if:

else if (thisisanumber!=5) {
     ^^

或者,由于 thisisanumber == 5 的相反条件是 thisisanumber 不是 5,因此您不需要条件:

else {

关于c++ - 代码 : expected ' ; ' before ' { ' token -- what is causing this? 中的解析错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6500245/

相关文章:

c++ - 使用 Oblita/Interception 库发送鼠标点击/发送坐标?

c++ - 尝试在 C++ 中有效地存储重复的子字符串

C++ 迭代器作为 lambda 函数中的参数,断言失败 : "vector iterators incompatible"

c++ - Windows 函数 ChoosePixelFormat 在 Windows 7 上返回 ERR_OLD_WIN_VERSION

c++ - 在基类中存储指向派生类函数的指针

c++ - 灵气 : Completely ignoring output of some rules

类对象指针的 C++ vector

c# - 当C#调用C++函数时,是否可以在被调用的C++函数中初始化数组并返回到C#?

c++ - '=' 标记之前的预期构造函数、析构函数或类型转换

c++ - thread_guard 与 scoped_thread