0

Code analyzes a integer and asks the user if he wants to analyze another integer at the end. I have it set up in a while loop to run only when 'again' is true. At the end a user inputs a 'n' for no, do not run another number. The problem is the program runs again even after again = false.

How can I fix my loop? I think the main focus is at the bottom where my switch is but i posted it all just in case i over looked something in the main part of the 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 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");
}
4

3 回答 3

1

This line will set again to true and then return the new value of again, so it will always be true.

while ( again = true )

Try this instead:

while ( again == true )

Or even better:

while ( again )
于 2013-09-04T03:40:28.733 回答
1

Change

while(again=true)

to

while(again == true)

or

while(again)
于 2013-09-04T03:41:14.287 回答
0

Using = is an assignment statement. To compare, you use ==

while (again == true) {
    //do code
}
于 2013-09-04T03:40:12.697 回答