void combination::OnButton2()
{
// TODO: Add your control notification handler code here
  m_progress.SetPos(0);
  m_progress.SetRange(0,100);

combination a;
a.make_combinations(0);
}
void combination:: make_combinations(int lo_val)
{
  srand(time(0));
  m_progress.StepIt();
ofstream fout("combination.txt",ios::app);
ofstream fout2("time.txt",ios::app);

for(int i=0; i<theApp.no_of_process; i++)
{
    //m_progress.OffsetPos(100/4);
    //m_progress.SetStep(200);
    clock_t begin=clock();
    arr[lo_val] = i;
    if(lo_val==(theApp.no_of_tasks)-1)
    {
        for( int j=0; j<theApp.no_of_tasks; j++)
        {
            int number = arr[j];
            fout<<Matrix[j][number];
        }

        fout<<endl;
    }
    else
    {
        //Sleep(2);
        make_combinations(lo_val+1);
        clock_t end=clock();
    theApp.combination_time[i][0]=(diffclock(end,begin))/1000;
    fout2<<theApp.combination_time[i][0]<<endl;
    }

}
}

there is a dialog on mfc with a button behine that button am calling a recursive function. i placed a progress bar on the same dialog that should tell me the progress of recursion. but iam gettin an error on clicking the button. debug assertion failed. your program caused an assertion failure. i dnt know what is wrong with my code. please help!!

ISSUE NO 2: Iam making this project on MFC! it includes brute force capability. It also has file handling in it!! am stuck at another point! i have a couple of files with file writing! my project has multiple files .txt format! on the main MFC board i want to add an option for browsing those written files . they should open in the format they are written! any help how it can be done?? just like a browsing menu! help??

Dani AI

Generated

Most likely cause of the debug assertion: the progress-control wrapper (m_progress) is being used on an object that has no HWND. In the original OnButton2 a fresh local combination instance is created and make_combinations is invoked on that instance; that object's m_progress was never subclassed/created, so CWnd methods hit an ASSERT. noticed this — remove the extra local object and call the recursion on the dialog instance, and call control methods only after DDX_Control/OnInitDialog. Also set the range before setting the position, as suggested.

Long-running recursion on the UI thread freezes repainting and can make progress updates appear broken. Move the heavy work to a worker thread and marshal progress updates back to the dialog with PostMessage or a custom WM_USER handler. Minimal pattern:

static UINT RunCombThread(LPVOID p)
{
    reinterpret_cast<combination*>(p)->make_combinations_worker();
    return 0;
}

::AfxBeginThread(RunCombThread, this);   // start from the dialog
// worker posts progress:
::PostMessage(m_hWnd, WM_USER+100, newPos, 0);

Handle WM_USER+100 in the dialog to call m_progress.SetPos((int)wParam) and m_progress.UpdateWindow().

Other practical fixes and cautions: call srand(time(NULL)) once (not inside recursion); open the ofstream objects once before recursion and pass references or use class members (repeatedly opening/appending inside deep recursion is slow and error-prone); compute the total number of combinations beforehand and use SetRange32(total) so progress maps correctly. For timing, prefer chrono:

auto t0 = std::chrono::steady_clock::now();
// work
auto t1 = std::chrono::steady_clock::now();
double secs = std::chrono::duration_cast<std::chrono::duration<double>>(t1 - t0).count();

For the “browse files” feature either populate a dynamic menu from the output folder (CFileFind or std::filesystem, then CMenu::AppendMenu and map IDs to paths) or use CFileDialog. To open a selected .txt with the system default:

ShellExecuteA(GetSafeHwnd(), "open", fullPath.c_str(), NULL, NULL, SW_SHOWNORMAL);

Finally, verify indexing when measuring per-item times in recursion (the loop index i may not map straightforwardly once recursion is involved).

Recommended Answers

All 2 Replies

I would start commenting out lines until the assertion failure stops.

Should the position be set before the range is declared?

combination a;
a.make_combinations(0);

If your combination class has member m_progress which you are initializing in OnButton2() function. But to call
make_combinations fucntion you are creating another object of class combination whose m_progress is not yet initialized.

see this code

void combination::OnButton2()
{
    // TODO: Add your control notification handler code here
    m_progress.SetRange(0,100); // first set range
    m_progress.SetPos(0); // then set position
    make_combinations(0);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.