Search code examples
c++stringexceptionbubble-sort

How do I implement an exception handler for strings and doubles in my bubble sort?


        #include <iostream>
       #include<conio.h>
       using namespace std;

       int main()
       {

       int hold;
       int swapNumber=0;
       int compare=0;
       int array[10];

        for(int i=0; i<10; i++)
       {
        cout<<"Enter 10 numbers: "<<endl;
        cin>>array[i]; 
} 
cout<<endl; 

// what user inputed
cout<<"Originally entered array by the user is: "<<endl;

for(int k=0; k<10; k++)
{
    cout<<array[k];
    cout<<endl; 
} 
cout<<endl;

//begin bubblesort method
for(int i=0; i<9; i++)
{

    for(int k=0; k<9; k++)
{
        compare++;
        if(array[k]>array[k+1])
{
            hold=array[k];
            array[k]=array[k+1];
            array[k+1]=hold; 
            swapNumber++;//adding swap count by 1
            }//end of if
        } //end of for j
    } // end of for i

//when sorted sm to large
cout<<"Sorted Array is: "<<endl;
for(int i=0; i<10; i++)
{
    cout<<array[i]<<endl; 
} 

//how many times numbers are swapped
cout<<"Number of times Swapped: "<<swapNumber<<endl;

//how ,many times numbers are compared
cout<<"Number of times Compared: "<<compare<<endl;

getch();
}

My assignment is to add an exception handler which states "Error! ONLY INTEGER INPUT ALLOWED ".The handler is supposed to catch doubles and strings. I've tried try and catch method but i don't know quite where to place it so that it'll work. I've also read about the catch all method, which seems like what i need to do. I've just now completed my counter methods for swap and comparisons which I do believe are working fine.


Solution

  • #include<iostream>
    #include<conio.h>
    using namespace std;
    
    int main()
    {
        int hold, swapNumber = 0, compare = 0, array[10];
        const string Error = "Error! ONLY INTEGER INPUT ALLOWED!";
    
        cout << "Enter 10 numbers: " << endl;
        try
        {
            for ( int i = 0; i < 10; i++ )
            {
                cin >> array[i];
                if ( !cin ) // Error in reading input stream
                {
                    throw Error;
                }
            }
        }
        catch ( const string& E )
        {
            cout << E << endl;
            return 0;
        }
        cout << endl;
    
        cout << "Originally entered array by the user is: " << endl;
        for ( int k = 0; k < 10; k++ )
        {
            cout << array[k] << endl;
        }
        cout << endl;
    
        for ( int i = 0; i < 9; i++ )
        {
            for ( int k = 0; k < 9; k++ )
            {
                compare++;
                if ( array[k] > array[k + 1] )
                {
                    hold = array[k];
                    array[k] = array[k + 1];
                    array[k + 1] = hold;
                    swapNumber++;
                }
            }
        }
    
        cout << "Sorted Array is: " << endl;
        for ( int i = 0; i < 10; i++ )
        {
            cout << array[i] << endl;
        }
        cout << endl;
    
        cout << "Number of times Swapped: " << swapNumber << endl;
        cout << "Number of times Compared: " << compare << endl;
    
        getch();
    }