c++ - While loop and bool make program end -
code analyzes integer , asks user if wants analyze integer @ end. have set in while loop run when 'again' true. @ end user inputs 'n' no, not run number. problem program runs again after again = false.
how can fix loop? think main focus @ bottom switch posted in case on looked in main part of code.
int main() { bool again = true; bool flag = false; int usernum; int count = 0; int userhold1, userhold2; userhold1 = 0; userhold2 = 0; char choice; while ( again = true ) { cout << "please enter positive integer: "; // getting number test cin >> usernum; cout << endl; ////////////////////////////////// finding factors cout << "the factors of " << usernum << " are: "; (int i=1 ; <= usernum/2 ; i++) { if( isafactor ( usernum , , flag) ) { cout << << ", "; count = count + i; // keeping track of sum of factors } } cout << "and " << usernum << endl; count = count + usernum; ////////////////////////////////// sum of factors cout << "the sum of factors is: " << count << endl; ////////////////////////////////// prime factorization cout << "the prime factorization is: "; getprimefac ( usernum ); cout << endl; ////////////////////////////////// even/odd checker cout << usernum << " "; if ( checkeven( usernum ) == true) cout << "even number." << endl; else cout << "odd number." << endl; ////////////////////////////////// prime checker primecheck (usernum); ////////////////////////////////// perfect , abundant , deficient count = 0; ( int = 1 ; <= usernum/2 ; i++ ) // adding factors of number { if( isafactor ( usernum , , flag ) ) count = count + i; // suming of factors } if ( count == usernum ) cout << usernum << " perfect number." << endl; else if ( count > usernum ) cout << usernum << " abundant number." << endl; else if ( count <usernum ) cout << usernum << " deficient number." << endl; ////////////////////////////////// squares squarecheck ( usernum ); ////////////////////////////////// twin primes if ( primecheck2 ( usernum ) == true ) { if ( primecheck2 ( usernum -2 ) == true ) { userhold1 = ( usernum - 2 ); cout << usernum << " has twin prime: " << userhold1 << " "; if ( primecheck2 ( usernum + 2 ) == true ) { userhold2 = ( usernum + 2 ); cout << "and " << userhold2; } } if ( primecheck2 ( usernum + 2 ) == true ) { userhold1 = ( usernum + 2 ); cout << usernum << " has twin prime: " << userhold1 << " "; if ( primecheck2 ( usernum - 2 ) == true ) { userhold2 = ( usernum - 2 ); cout << "and " << userhold2; } } } cout << endl << endl; ////////////////////////////////// end program again? cout << "would analyze number? (y/n) :"; cin >> choice; switch (choice) { case 'y': again = true; break; case 'n': again = false; break; } } system ("pause"); }
this line set again true , return new value of again, true.
while ( again = true )
try instead:
while ( again == true )
or better:
while ( again )
Comments
Post a Comment