Abi*_*rer 1 c++ if-statement exit
在我的C++书籍示例中,我使用系统("暂停")来保持屏幕打开,这是我从操作系统中学到的仅用于作业.现在我的书中有一个例子,它使用exit(),依赖于true或false,我的屏幕消失了.如果我取出exit()程序一直在计算(这就是作者放在exit()中的原因)但是如何解决这个问题?我正在使用Microsoft VS C++ Express 2012 for Windows Desktop.
//prime.cpp
// demonstrates IF statement with prime numbers
#include <iostream>
using namespace std;
#include <process.h> //for exit()
int main()
{
unsigned long n,j;
cout << "Enter a number: ";
cin >>n; //get number to test
for (j=2; j<= n/2; j++) //divide by integer from
if(n%j==0) //2 on up; if remainder is 0,
{ //it's divisible by j
cout <<"It's not prime; divisible by " <<j<<endl;
exit (0); //exit from program
}
cout <<"It's prime\n";
system ("pause"); //the only way I can get screen to stay open
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如果答案是素数工作正常.如果答案是非主要屏幕眨眼.如果我删除exit()我得到这个结果:
输入一个数字:8它不是素数; 被2整除它不是素数; 可被4整除它按下任意键继续...
| 归档时间: |
|
| 查看次数: |
227 次 |
| 最近记录: |