首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++代码中的四个错误,我找不到解决方案

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

Stack Overflow用户
提问于 2012-12-01 21:16:46
回答 4查看 400关注 0票数 0

我正在看一些我正在处理的代码,有3-4个错误我已经尝试了大约一周的时间来处理,但我就是做不到!我是编程新手,所以如果你能用愚蠢的方式回答,那就太好了!下面是代码。

代码语言:javascript
复制
#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);

    }

}

这是错误日志。

代码语言:javascript
复制
/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

再次,非常感谢!

EN

回答 4

Stack Overflow用户

发布于 2012-12-01 21:18:33

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

代码语言:javascript
复制
 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);
 }
票数 4
EN

Stack Overflow用户

发布于 2012-12-01 21:18:31

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

也许你想要这样的东西:

代码语言:javascript
复制
if (password == "creeper1") {
}
else if (password == "admin1") {
}
else {
}
票数 2
EN

Stack Overflow用户

发布于 2012-12-01 21:22:36

你不能在else之后使用else if,这个块永远不会返回,正确的语法是executed.Also 0,而不是return(0)。

您还可以包含额外的大括号,但是如果您以正确的方式缩进代码,您将看到代码块何时结束和开始,因此您很少犯像添加额外的大括号这样的错误:

代码语言:javascript
复制
#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;
}

这是带有所有语法错误的代码,fixed.About语义我不知道它是否可以工作,但是现在你可以编译并执行它,来测试代码。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13659805

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档