コードは整数を分析し、最後に別の整数を分析するかどうかをユーザーに尋ねます。「再び」がtrueの場合にのみ実行するように、whileループで設定しました。最後に、ユーザーはいいえの「n」を入力します。別の番号を実行しないでください。問題は、再度 = false の後でもプログラムが再度実行されることです。
ループを修正するにはどうすればよいですか? 主な焦点はスイッチがある下部にあると思いますが、コードの主要部分で何かを見落とした場合に備えて、すべてを投稿しました。
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 a positive integer: "; // Getting the number to test
cin >> usernum;
cout << endl;
////////////////////////////////// Finding the Factors
cout << "The factors of " << usernum << " are: ";
for (int i=1 ; i <= usernum/2 ; i++)
{
if( isAfactor ( usernum , i , flag) )
{
cout << i << ", ";
count = count + i; // Keeping track of sum of factors
}
}
cout << "and " << usernum << endl;
count = count + usernum;
////////////////////////////////// Sum of Factors
cout << "The sum of the factors is: " << count << endl;
////////////////////////////////// Prime Factorization
cout << "The prime factorization is: ";
getPrimeFac ( usernum );
cout << endl;
////////////////////////////////// Even/Odd Checker
cout << usernum << " is an ";
if ( checkeven( usernum ) == true)
cout << "EVEN number." << endl;
else
cout << "ODD number." << endl;
////////////////////////////////// Prime Checker
PrimeCheck (usernum);
////////////////////////////////// Perfect , Abundant , Deficient
count = 0;
for ( int i = 1 ; i <= usernum/2 ; i++ ) // Adding the factors of the number
{
if( isAfactor ( usernum , i , flag ) )
count = count + i; // Suming of factors
}
if ( count == usernum )
cout << usernum << " is a PERFECT number." << endl;
else if ( count > usernum )
cout << usernum << " is an ABUNDANT number." << endl;
else if ( count <usernum )
cout << usernum << " is a DEFICIENT number." << endl;
////////////////////////////////// Squares
SquareCheck ( usernum );
////////////////////////////////// Twin Primes
if ( PrimeCheck2 ( usernum ) == true )
{
if ( PrimeCheck2 ( usernum -2 ) == true )
{
userhold1 = ( usernum - 2 );
cout << usernum << " has a 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 a twin prime: " << userhold1 << " ";
if ( PrimeCheck2 ( usernum - 2 ) == true )
{
userhold2 = ( usernum - 2 );
cout << "and " << userhold2;
}
}
}
cout << endl << endl;
////////////////////////////////// End program again?
cout << "Would you like to analyze another number? (y/n) :";
cin >> choice;
switch (choice)
{
case 'y': again = true;
break;
case 'n': again = false;
break;
}
}
system ("pause");
}