C++ 并非所有代码都会执行,但程序会正确退出
这是一个程序,它应该找到低于某个C++ 并非所有代码都会执行,但程序会正确退出,c++,std,C++,Std,这是一个程序,它应该找到低于某个max值的循环素数。它适用于main()中return 0前面的maxPut: bool fail=cout.fail(); cout.clear(); cout在main()中的返回0之前放置: bool fail=cout.fail(); cout.clear(); 不能刷新输出缓冲区。@HotLicks它工作了,但是std::endl不是也应该刷新cout吗?自从我弄脏cout的东西已经有十几年了,但是IIRC只有在你执行cin时才能自动刷新cout缓冲区。
max
值的循环素数。它适用于main()
中return 0
前面的maxPut:
bool fail=cout.fail();
cout.clear();
cout在main()中的返回0
之前放置:
bool fail=cout.fail();
cout.clear();
不能刷新输出缓冲区。@HotLicks它工作了,但是std::endl不是也应该刷新cout吗?自从我弄脏cout的东西已经有十几年了,但是IIRC只有在你执行cin时才能自动刷新cout缓冲区。刷新输出缓冲区。@HotLicks它工作了,但是std::endl不是也应该刷新cout吗?自从我弄乱cout的东西已经有十几年了,但是IIRC只有当你对两者都执行cin.0时,才能自动刷新cout缓冲区,在每个循环素数之后不使用和使用“cout.flush()”进行编码。@Xyzk:即使是max,我也不知道。也许cout的这种模糊性有问题。我和VC2010一起工作,从来没有遇到过这个问题。是的,endl应该flush-trystd::cout-ty以获取帮助:)0用于两者,在每个循环素数之后使用和不使用“cout.flush()”进行编码。@Xyzk:即使是max,我也不知道。也许cout的这种模糊性有问题。我和VC2010一起工作,从来没有遇到过这个问题。是的,endl应该刷新-无论如何,请尝试std::coutty以获取帮助:)
using namespace std;
int main(){
const int max = 10000;
int nrOfPrimes = 0;
int* primes = findIfPrimes(max);
for(int i = 2; i < max; i++){
//check if number is prime
if(primes[i] == 0){
nrOfPrimes++;
int l = 0;
int* permutations = findPermutations(i, l);
//variable saying lf all permutations are prime
bool allPrime = true;
//check all permutations, if they are not prime change allPrime
for(int j = 0; j < l; j++){
if(primes[permutations[j]] != 0){
allPrime = false;
break;
}
}
//if it wasnt circular prime- continue
if(allPrime == false)
continue;
//if it was circular prime, change all permutations to "circular prime"
std::cout << "Circular primes: ";
for(int j = 0; j < l; j++){
primes[permutations[j]] = 2;
std::cout << permutations[j] << " " << endl;
}
std::cout << endl;
}
}
//find total count
int result = 0;
for(int i = 2; i < max; i++)
if(primes[i] == 2)
result++;
std::cout << "total number of primes " << nrOfPrimes << endl;
std::cout << "total number of circular primes " << result << endl;
return 0;
}
bool fail = cout.fail();
cout.clear();
cout << fail << endl;
cin.ignore(cin.rdbuf()->in_avail()); // clears all remaining input
cin.get(); // waits for ENTER