c++ - 我可以从 C++ 中的某一行开始代码吗?
问题描述
基本上我希望代码在完成某些操作后返回并从某一行重新开始。例如,我有一个菜单,您可以在其中选择一个操作,如果该操作中的所有内容都已完成,我希望它再次显示该菜单。
这里有一些粗略的代码来说明。
int option;
cout << "1";
cout << "2";
cout << "3";
cout << "What option do you choose?";
cin >> option;
if (option = 1) {
//do something here
//if finished start again with the menu options
} else if (option = 2) {
//do something here
//if finished start again with the menu options
} else {
//do something here
//if finished start again with the menu options
}
解决方案
int option;
bool escape = true;
cout << "1\n2\n3\n";
while(escape){
cout << "What option do you choose?";
cin >> option;
if (option == 1) { // double == sign for checking. = is for assignment.
//do something here
//if finished start again with the menu options
}
else if (option == 2) {
//do something here
//if finished start again with the menu options
}
else if (option == 3) {
//do something here
//if finished start again with the menu options
}
else {
escape = false; // this is just to break the loop.
}
}
推荐阅读
- c# - 将 Json 字符串反序列化为用户定义的对象时出现异常
- python - 使用 API 和子进程远程运行 python 脚本不起作用
- sql - 一个 Oracle 包中的父子插入语句
- javascript - 无效的事件目标 - 将 fromEvent 与 Angular 材质按钮一起使用时
- tfs - 如何使用 VS 2019 Team Explorer 在浏览器中打开工作项?
- .net - ASP.NET Core“假”混合身份验证
- javascript - window.print() 随机停止工作。它的调用有限制吗?
- java - 将图像设置为自定义列表视图到列表视图中的特定位置
- c# - 使用 Ajax 更新两个下拉列表过滤器的值 C# MVC
- c# - 将 DataSet 从 WCF 返回到 .Net Core 应用程序会将 DataSet 转换为 ArrayOfXElements 类型