0

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?

3
Contributors
2
Replies
3
Views
6 Years
Discussion Span
Last Post by WaltP
0

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 by gerard4143: n/a

0

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.