I have to make a program that takes 4 inputs and outputs the 2 largest inputs. So I wrote this code,

#include <iostream>
using namespace std;
void largest_two ( int input[4], int output[2] )
{
     bool restart;
     int i;
     int temp;
     do
     {
         restart = false;
         for ( i = 0; i < 3; i++ )
         {
             if ( input[i] < input[i+1] )
             {
                  temp = input[i];
                  input[i] = input[i+1];
                  input[i+1] = temp;
                  restart = true;
             }
         }
     }while ( restart = true );
     output[0] = input[0];
     output[1] = input[1];
}
int main()
{
    int input[4];
    int output[2];
    cout << "Please enter four numbers:" << endl;
    cin >> input[0];
    cin >> input[1];
    cin >> input[2];
    cin >> input[3];
    largest_two ( input, output );
    cout << "The largest two of the numbers you entered are " << output[0] << "and " << output[1];
    cin.ignore();
    cin.get();
}

and compiled it. When I ran it, it allowed my to input the 4 numbers, but at the function call, it froze up. I figure its a problem with my bubblesort, since I always screw that up, but I didn't see anything wrong with it. Can someone point out my error?

}while ( restart = true ); will loop infinitely because the condition always is true. Change to }while ( restart == true );

That's exactly the mistake I made last time when implementing bubblesort, I just realized. Thanks for pointing it out.

If this is something that you commonly 'mess up', consider writing code like:
if( true == myBoolean ) instead of if( myBoolean == true ).

If in the first case you write if( true = myBoolean ) by accident, the compiler will start complaining (the same works with other conditional statements, such as while).

This article has been dead for over six months. Start a new discussion instead.