我的循环设置如下所示:
#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循环的位置问题吗?
for (const auto &Agent : v){
Agent->action();
}编辑:下面是我使用(choice != 5)之类的输出的一个例子
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如您所见,它循环,但不显示输出。
发布于 2020-10-23 22:27:44
你有几个问题。首先,您不处理“退出”情况,它只是继续循环,因为循环条件choice >= 5是满足的。此外,您也不处理非整数的输入。此外,您还有一个与Agent的名称冲突。此外,如果不删除代理,您可能会发生内存泄漏。我知道当进程退出时内存就会恢复,但是即使不必删除原始指针也是很好的做法,最好一开始不要使用原始指针,而是使用shared_ptr这样的智能指针,这样当对象超出作用域时,内存就会自动删除。
#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::是出于习惯。
https://stackoverflow.com/questions/64507688
复制相似问题