我如何重播我的主要功能?(读描述,难字标题)
How do I replay my main function? (Read description, hard to word title)
我提前为误导性的标题道歉,我真的不知道如何表达我的问题,没有更多的空间。我将从展示main函数开始。
int main() {
int input;
List List;
cout << "Press '1' to add a node" << endl;
cout << "Press '2' to view the list of nodes" << endl;
cin >> input;
if (input == 1) {
List.addNode();
}
else if (input == 2) {
List.PrintList();
}
}
因此,正如您可以看到的主函数的性质,用户将希望输入多个节点(input 1)。现在,如果我输入一个节点,程序就结束了。在一个完美的程序,我希望能够让用户输入尽可能多的数据点,因为他们想,也能够打印出来。这两个功能现在基本上是无用的,因为需要多个数据点,并且用户会想要重新打印他们输入的点。
用描述的方式:我的问题是我如何让main函数重播自己?
你真正想要的是一个一直发生在计算机科学领域的转变。您需要稍微修改一下您的代码,您的主函数实际上已经过时了。是时候重写你的代码了。
int new_function() {
int input;
... // Do the rest of your function here
}
int main() {
int i;
for (i=0; i < XXX; i++) {
new_function();
}
}
根据你的程序是如何完全开发的,你可能想在main中循环,或者你可能想在new函数或其他地方循环。你的架构的那一部分你必须根据你的功能来决定。
祝你好运!
为什么不直接把它塞进while循环呢?
int main() {
int input = 0;
List nodeList;
/*Loop till user chooses to exit.*/
while(input != 3)
{
/*Display options for user and take output.*/
cout << "Press '1' to add a node" << endl;
cout << "Press '2' to view the list of nodes" << endl;
cout << "Press '3' to exit" << endl;
cin >> input;
/*Add a node to list.*/
if (input == 1) {
nodeList.addNode();
}
/*Display node list.*/
else if (input == 2) {
nodeList.PrintList();
}
/*Exit program.*/
else if (input == 3) {
return 0;
}
/*Re-prompt user to input again.*/
else {
cout << "Invalid input.. try again." << endl;
}
}
/*Won't reach.*/
return 0;
}
一个for循环将按照您的意愿重复多次。
int i=0;
for (i=0;i<10;i++){
// do something 10 times
}
while循环也很好,正如另一个答案所指出的。
使用do-while循环,并添加一个cout<<"按3退出"。在do-while语句中包含所有的if-else语句,以便能够循环执行直到用户点击3。在while条件中,设置while(input!=3)
相关文章:
- 使用VerQueryValue检索应用程序的文件描述
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- 如何使用重载的相等(==)运算符向测试用例添加描述
- 如何在C/C++中用FD_set Unix设置套接字文件描述符
- PC中的程序和PHONE中的本机描述应用程序之间的数据连接
- 对于C++,方括号中的标题必须总是在引号中的标题之上吗
- 从 Boost ASIO 获取 epoll 描述符 io_service对象
- 库标题在标题中不可见,但在 cmake build 下.cpp文件中完全可见.为什么?
- 为什么我不应该把所有东西都放在标题中?
- WinAPI 检查窗口是否具有常规标题栏
- I2C 文件描述符上的 I2C 总线可写/可读标志
- 如何更改窗体上所有控件的标题?[C++生成器]
- 提升用于解析标题列的精神语法
- 如何在 CompleteAsyncIO 中访问 IOMemoryBufferDescriptor,该描述符通过中断 EP
- 所以我正在为我的学校作业练习继承,但我无法正确实施标题保护
- 如何删除已输入的书以及如何使标题,语言和名称在上面放置空格不会出错?
- 使用类 ?C++ 和 SFML 以及标题中的 15 个字符
- Gtkmm 窗口为空白,不显示任何小部件或标题
- 我将如何在 cpp 中生成一个随机且独特的标题
- 我如何重播我的主要功能?(读描述,难字标题)