I’ve been creating an idle program to count in minutes when the mouse and keyboard are inactive. This is what I have so far:
using namespace std;
while(true)
{
GetLastInputInfo(&last_info);
tickCount = GetTickCount();
int minutes = (tickCount - last_info.dwTime) / 60000;
count++;
if((minutes >= 1) && (count%3000==0))
{
ifstream in("in.txt");
ofstream out("out.txt");
float sum;
in >> sum;
sum = sum++;
out << sum;
out << in.rdbuf();
out.close();
in.close();
}
std::cout << "Idle Time: " << minutes << " minutes." << std::endl;
}
}
When I run it idle for one minutes the “sum” says it’s 1, I then close the program and open it up for one minutes again and the “sum” says it’s 2. I close the program and open it for one more minute and it’s back down to 1. Why is this happening?
Here’s what i think happens.
contents of in.txt
1
contents of out.txt
2
1
2
1
when you read your value from in into sum, sum = 1;
sum++ happens, sum becomes 2.
2 goes into out.txt; then 1 goes into out.txt. then you print “idle time”. and it goes round and round and round since sum is always initialized to 1.
try commenting out this line
or declaring sum with a greater scope (outside the file reading loop)
also you never seem to add minutes to it…
EDIT:
lets try this…