并非所有代码都执行,但程序正确退出

时间:2013-12-01 14:12:40

标签: c++ std

这是一个程序,应该找到低于某个max值的圆形素数。它适用于max<=1000,但如果max=10000,程序结束时没有错误,但不会将最后两行打印到控制台,即使它始终应该。它也不会打印出更多的循环素数,但这可能是我的算法问题,我稍后会担心它。

注意:我正在使用MVS 2010,std::之前没有必要cout,因为它有时会说cout是不明确的。

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;
}

1 个答案:

答案 0 :(得分:1)

return 0放在main()

之前
bool fail = cout.fail();
cout.clear();
cout << fail << endl;
cin.ignore(cin.rdbuf()->in_avail()); // clears all remaining input
cin.get(); // waits for ENTER

是什么告诉fail

fail = cout.good();执行相同操作。是什么告诉它呢?