// Compiles under Microsoft Visual C++
// Link with /MDd (Debug) and /MD (Release)

#include <iostream>
#include <string>
#include <exception>

using namespace std;

#import "..\GC\GC.exe"

int main(int argc, char* argv[])
{
    ::CoInitialize(NULL);

    bool bCaughtException = true;
    try
    {
        string s;
        gcLib::IPrimePtr ptr(__uuidof(gcLib::Prime));
        while (true)
        {
            cout << "Enter a number > 2 (0 to quit): ";
            cin >> s;
            if (s == "0")
            {
                bCaughtException = false;
                break;
            }

            try
            {
                BSTR bstrFirst = NULL, bstrSecond = NULL;
                long lNumTrials = 0, lRet = 0;
                ptr->GetGBPairUsingRM(
                    s.c_str(),
                    lNumTrials,
                    &bstrFirst,
                    &bstrSecond,
                    &lRet,
                    130
                );
                if (lRet)
                {
                    cout << _bstr_t(bstrFirst, false) << " + " << _bstr_t(bstrSecond, false) << endl;
                }
                else
                {
                    cout << "Goldbach Conjecture is false for the even natural number, " << s << endl;
                }
            }
            catch (const _com_error& e)
            {
                cout << e.ErrorMessage() << endl;
                if (e.ErrorInfo() != NULL)
                {
                    cout << e.Description() << endl;
                }
            }

            continue;
        }
    }
    catch (const _com_error& e)
    {
        cout << e.ErrorMessage() << endl;
        if (e.ErrorInfo() != NULL)
        {
            cout << e.Description() << endl;
        }
    }
    catch (const exception& e)
    {
        cout << e.what() << endl;
    }
    catch (...)
    {
        cout << "Errors occurred." << endl;
    }

    if (bCaughtException)
    {
        cout << "Press any key to continue" << endl;
        char c;
        cin >> c;
    }

    ::CoUninitialize();

    return 0;
}

