首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我的条件对我的do/while循环不起作用?C++

为什么我的条件对我的do/while循环不起作用?C++
EN

Stack Overflow用户
提问于 2020-10-23 21:47:23
回答 1查看 65关注 0票数 0

我的循环设置如下所示:

代码语言:javascript
复制
#include <iostream>
#include <string>
#include <vector>
#include "Agent.h"
#include "Sage.h"
#include "Sova.h"
#include "Reyna.h"

using namespace std;

int main() {
    int choice;
    vector <Agent*> v;


    do
    {
        cout << "Choose an Agent to Reveal Agent Ability" << endl;
        cout << "---------------------------------------" << endl;
        cout << "1. Sage" << endl;
        cout << "2. Sova" << endl;
        cout << "3. Reyna" << endl;
        cout << "4. Display All" << endl;
        cout << "5. Quit" << endl;
        cin >> choice;

        switch (choice)
        {
            case 1:
                v.push_back(new Sage("Healing"));
                break;

            case 2:
                v.push_back(new Sova("Sight"));
                break;

            case 3:
                v.push_back(new Reyna("Blinding"));
                break;
            case 4:
                v.push_back(new Sage("Healing"));
                v.push_back(new Sova("Sight"));
                v.push_back(new Reyna("Blinding"));
                break;
            default:
                cout << "Bad choice! Please try again later.\n";
        }
    } while (choice <=0 || choice >=5);

    for (const auto &Agent : v){
        Agent->action();
    }
    return 0;
}

我的情况是while (choice <=0 || choice >=5)

但是,当我运行这个程序时,在我做出选择之后,信息就会输出到屏幕上,然后程序就结束了。我尝试了其他条件,但当我选择一个选择,程序将循环,但不会输出任何信息。

这是我的for循环的位置问题吗?

代码语言:javascript
复制
for (const auto &Agent : v){
    Agent->action();
}

编辑:下面是我使用(choice != 5)之类的输出的一个例子

代码语言:javascript
复制
Choose an Agent to Reveal Agent Ability
---------------------------------------
1. Sage
2. Sova
3. Reyna
4. Display All
5. Quit
1
Choose an Agent to Reveal Agent Ability
---------------------------------------
1. Sage
2. Sova
3. Reyna
4. Display All
5. Quit

如您所见,它循环,但不显示输出。

EN

回答 1

Stack Overflow用户

发布于 2020-10-23 22:27:44

你有几个问题。首先,您不处理“退出”情况,它只是继续循环,因为循环条件choice >= 5是满足的。此外,您也不处理非整数的输入。此外,您还有一个与Agent的名称冲突。此外,如果不删除代理,您可能会发生内存泄漏。我知道当进程退出时内存就会恢复,但是即使不必删除原始指针也是很好的做法,最好一开始不要使用原始指针,而是使用shared_ptr这样的智能指针,这样当对象超出作用域时,内存就会自动删除。

代码语言:javascript
复制
#include <iostream>
#include <string>
#include <vector>
#include <memory>
#include "Agent.h"
#include "Sage.h"
#include "Sova.h"
#include "Reyna.h"

using namespace std;

int main() {
    int choice;
    vector <std::shared_ptr<Agent> > v;

    do
    {
        cout << "Choose an Agent to Reveal Agent Ability" << endl;
        cout << "---------------------------------------" << endl;
        cout << "1. Sage" << endl;
        cout << "2. Sova" << endl;
        cout << "3. Reyna" << endl;
        cout << "4. Display All" << endl;
        cout << "5. Quit" << endl;
        
        if (!(cin >> choice)) {
            std::cout << "Please enter an integer." << std::endl;
            choice = -1; // so the loop condition is true and we reloop
            continue;
        }

        switch (choice)
        {
            case 1:
                v.push_back(std::make_shared<Sage>("Healing"));
                break;

            case 2:
                v.push_back(std::make_shared<Sova>("Sight"));
                break;

            case 3:
                v.push_back(std::make_shared<Revna>("Blinding"));
                break;
            case 4:
                v.push_back(std::make_shared<Sage>("Healing"));
                v.push_back(std::make_shared<Sova>("Sight"));
                v.push_back(std::make_shared<Reyna>("Blinding"));
                break;
            case 5:
                std::cout << "Quitting." << std::endl;
                return 0;
                break;
            default:
                cout << "Bad choice! Please try again later.\n";
        }
    } while (choice < 1 || choice > 5);

    for (const auto & agent_ptr : v){
        agent_ptr->action();
    }
    return 0;
}

良好的实践也是不使用using namespace std;。见:Why is "using namespace std;" considered bad practice?。您可以看到我在哪里编辑您的代码,我添加std::是出于习惯。

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

https://stackoverflow.com/questions/64507688

复制
相关文章

相似问题

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