0

我正在查看我正在处理的一些代码,并且我尝试了大约一周的时间来消除 3-4 个错误,但我就是做不到!我对编程有点陌生,所以如果你能以愚蠢的形式回答,那就太好了!这是代码。

#include <iostream>
#include <string>


using namespace std;

int main() {
    string password;

    int choice;

    cout << "Command Line Multi-Tool" << endl;
    cout << endl;
    cout << "plase enter your password: " << endl;
    cin >> password;
    if (password == "creeper1") {
        cout << endl;
        cout << "Main Menu" << endl;
        cout << "1. Class Schedule" << endl;
        cout << "2. School Info" << endl;
        cout << "3. Exit" << endl;
        cin >> choice;
    }
    else {
        cout << "Incorrect, Access Denied" << endl;
        return(0);
    }

    }

    else (password == "admin1"){
        cout << "/*adminLogin=='1'*/" << endl;
        cout << endl;
        cout << "Menu::Main" << endl;
    }

    return(0);

    }

}

这是错误日志。

/Users/student/Documents/TO BE FILED/Tuesday/main.cpp:31:0 /Users/student/Documents/TO BE
FILED/Tuesday/main.cpp:31: error: expected unqualified-id before 'else'


/Users/student/Documents/TO BE FILED/Tuesday/main.cpp:36:0 /Users/student/Documents/TO BE
FILED/Tuesday/main.cpp:36: error: expected unqualified-id before 'return'


/Users/student/Documents/TO BE FILED/Tuesday/main.cpp:36:0 /Users/student/Documents/TO BE
FILED/Tuesday/main.cpp:36: error: expected declaration before '}' token

再次非常感谢!

4

4 回答 4

4

您的代码中有 1 个if但 2 个else分支。决定你想要哪一个,然后失去另一个。查看您可能想要的代码

 if (password == "creeper1") {
    cout << endl;
    cout << "Main Menu" << endl;
    cout << "1. Class Schedule" << endl;
    cout << "2. School Info" << endl;
    cout << "3. Exit" << endl;
    cin >> choice;
 } else if (password == "admin1")
    // Your processing code here
 }  else {
    cout << "Incorrect, Access Denied" << endl;
    return(0);
 }
于 2012-12-01T13:18:33.343 回答
2

不平衡else的,即没有对应的if

也许你想要类似的东西:

if (password == "creeper1") {
}
else if (password == "admin1") {
}
else {
}
于 2012-12-01T13:18:31.483 回答
0

如果在 else 之后不能使用 else,则该块将永远不会被执行。另外,正确的语法是 return 0,而不是 return(0)。
您还包括额外的大括号,但如果您以正确的方式缩进代码,您会看到块何时结束和开始,因此您很少会犯错误,例如添加额外的大括号:

#include <iostream>
#include <string>


using namespace std;

int main()
{
    string password;

    int choice;



    cout << "Command Line Multi-Tool" << endl;
    cout << endl;
    cout << "plase enter your password: " << endl;
    cin >> password;
    if (password == "creeper1")
    {
        cout << endl;
        cout << "Main Menu" << endl;
        cout << "1. Class Schedule" << endl;
        cout << "2. School Info" << endl;
        cout << "3. Exit" << endl;
        cin >> choice;
    }
    else if(password == "admin1")
    {
        cout << "/*adminLogin=='1'*/" << endl;
        cout << endl;
        cout << "Menu::Main" << endl;
    }
    else
    {
        cout << "Incorrect, Access Denied" << endl;
        return 0;
    }
    return 0;
}

这是修复了所有语法错误的代码。关于语义我不知道它是否有效,但现在您可以编译并执行它,以测试代码。

于 2012-12-01T13:22:36.377 回答
0

因为 else 不能包含判断正确的用法是这样的:

如果(密码==“爬行者1”){

}否则如果(密码==“admin1”){

} 别的{

}

于 2012-12-01T13:29:21.350 回答