C ++代码中的四个错误,我无法找到解决方案

时间:2012-12-01 13:16:46

标签: c++ compiler-errors

我正在查看我正在处理的一些代码,并且我已经尝试了大约一个星期来解决的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 个答案:

答案 0 :(得分:4)

您的代码中有一个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);
 }

答案 1 :(得分:2)

不平衡else,即。没有相应的if

也许你想要这样的东西:

if (password == "creeper1") {
}
else if (password == "admin1") {
}
else {
}

答案 2 :(得分:0)

如果在else之后,你不能使用else,那个块永远不会被执行。另外,正确的语法是返回0,而不是返回(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;
}

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

答案 3 :(得分:0)

因为别的不能妥协判断 正确的用法是这样的:

if(password ==“creeper 1”){

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

} 否则{

}