如何创建无需重新运行即可获得新结果的程序?
How can I create a program that doesn't need to rerun to get new results?
我刚刚编写了基于用户输入计算乘法表的代码。问题是,在我打印结果后,程序关闭,我需要重新启动它以获得不同的输入和结果。
我想要得到结果,并按下一个键从用户那里得到一个新的值:我不想要不断地关闭和打开程序。我该怎么做呢?
这是我的代码:
#include <iostream>
using namespace std;
int main()
{
int a, range;
cout << "Introduce value: ";
cin >> a;
printf("n");
cout << "Introduce range: ";
cin >> range;
printf("n");
for (int i = 1; i <= range; ++i)
{
cout << a << " * " << i << " = " << a*i << endl;
}
printf("n");
system("pause");
}
添加while(1)
#include <iostream>
using namespace std;
int main()
{
while(1){
int a, range;
cout << "Introduce value: ";
cin >> a;
printf("n");
cout << "Introduce range: ";
cin >> range;
printf("n");
for (int i = 1; i <= range; ++i)
{
cout << a << " * " << i << " = " << a*i << endl;
}
printf("n");
system("pause");
}
}
因为while
语句中的条件总是为真,所以这里的代码将永远循环!
如果你想按一个键来决定是否继续使用另一个值,请使用do while循环。
int main(void){
char c;
do{
//......your code
cout<<"Do you want to continue?"; // press 'y' if yes
cin>>c;
}while(c=='y');
return 0;
}
按"y"继续或按其他任何停止。有了这段代码,你不需要system("Pause")在结束
相关文章:
- 为什么"do while"循环不断退出,即使条件计算结果为 false?
- valgrind-hellgrind与泄漏检查的结果不同
- 用C++20 fmt限制结果的总大小
- 如何返回一个类的两个对象相加的结果
- 使用QProcess执行命令,并将结果存储在QStringList中
- 如果我std::dynamic_pointer_cast并且底层dynamic_cast的结果为null,那么返回的sh
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- 序列化,没有库的整数,得到奇怪的结果
- 使用取消引用的指针的多态性会产生意外的结果.为什么?
- 我是否在C++中投下新的结果?
- 是迭代器增量结果是新的迭代器
- C++,打开和编辑文本文件中的数字数据,并将结果放入新文件中
- 我的代码有效,但 setter 不会创建新对象,所以我得到错误的结果
- 返回新对象的函数与将函数的结果分配给调用它的对象的 C++ 区别
- 比较旧/新 CPU 上的C++开销时出现奇怪的结果
- 什么编译机制铸造新运算符的结果
- C++:当在函数内部创建新对象并将其作为结果返回时,我必须使用新运算符来创建对象
- 将结果写入文件时无法过渡到新行
- 如何创建无需重新运行即可获得新结果的程序?
- 我可以在函数的结果上使用std::move来构造一个新对象吗