I am having a lot of trouble getting the following to work and I think it is because of file issues:

int GetMinutes()
{
    fstream file(FNAME);
    char num[101];
    if (!file.is_open())
        Error("FILE NOT OPEN!");
    file.getline(num,100);
    file.close();
    return atoi(num);
}
int main(int argc, char *argv[])
{
    LARGE_INTEGER timestart, timestop, tickspersecond;
    QueryPerformanceFrequency(&tickspersecond);
    QueryPerformanceCounter(&timestart);
    Sleep(5);
    HWND window=GetForegroundWindow();
    int minutes=GetMinutes();
    char *caption;
    sprintf(caption, "%i minute timer.", minutes);
    bool counting=true;
    do
    {
        QueryPerformanceCounter(&timestop);
        int secondsleft=(minutes*60)-((timestart.QuadPart-timestop.QuadPart)/tickspersecond.QuadPart);
        char *message;
        sprintf(message, "%02i:%02i.%02i", secondsleft/3600, (secondsleft-((secondsleft/3600)*3600))/60, secondsleft-(((secondsleft-((secondsleft/3600)*3600))/60)*60)-((secondsleft/3600)*3600));
        counting=(secondsleft>=0);
    }while (counting);

I left out some of the code that I wish to keep to myself. But I have isolated the problem and it seems as though while loop is never excecuting. This happened before I changed from while{} to do{}while as well. Any ideas why this isn't working?

Your reading 100 digits, that's one big number. Its even bigger than an int.

A safer conversion function would be strtol(), atoi() does not detect errors.

Edited 5 Years Ago by gerard4143: n/a

I left out some of the code that I wish to keep to myself. But I have isolated the problem and it seems as though while loop is never excecuting. This happened before I changed from while{} to do{}while as well. Any ideas why this isn't working?

It's working fine. But for all intents and purposes, it does nothing useful.

do
    {
        QueryPerformanceCounter(&timestop);
        int secondsleft=(minutes*60)-((timestart.QuadPart-timestop.QuadPart)/tickspersecond.QuadPart);
        char *message;
        sprintf(message, "%02i:%02i.%02i", secondsleft/3600, (secondsleft-((secondsleft/3600)*3600))/60, secondsleft-(((secondsleft-((secondsleft/3600)*3600))/60)*60)-((secondsleft/3600)*3600));
        counting=(secondsleft>=0);
    }while (counting);

All the work this loop does is destroyed as soon as the loop is finished because all the values are local to the loop only.

And your sprintf() is tromping on memory since there is no space allocated for the pointer message.

And if you are going to use (secondsleft>=0) to exit the loop, just use it to exit the loop. There is no need to hide it in a boolean variable.

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