0

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?

Deanie
  • 2,316
  • 2
  • 19
  • 35
TriX
  • 3
  • 4
  • reduce the time to 5 seconds and run it thru a debugger. i mean, first you push sum into the output file, and then you push what you read from the input file into it? the input file could still have that old 1 in it. as long as you update only 'out' your 'in' and therefore sum will have the same value... why are you even using two files for this? – Shark Jul 30 '12 at 18:11
  • I don't know much about ifstream/ofstream and I thought this was the only way to get the number from the .txt file and then increment it. Also, would you mind showing me in code on what you mean? – TriX Jul 30 '12 at 18:43
  • plesae post the contents of the in.txt and out.txt files... – Shark Jul 30 '12 at 18:44
  • The in.txt isn't visible as far as I can tell. The out.txt file just says "2" at the moment. – TriX Jul 30 '12 at 18:46
  • aha, as i suspected. look below. – Shark Jul 30 '12 at 18:47
  • Saying "in.txt isn't visible as far as I can tell" is kind of a bizarre statement when `in.txt` is used as input to set the initial value of `sum`. What is the purpose on `in.txt` in the first place? If you have no idea, then you have no idea what `sum++` should result in anyway (not to mention the rather strange `out << in.rdbuf()`), and nothing should surprise you about what's in `out.txt`. – Michael Burr Jul 30 '12 at 19:13
  • C does not have namespaces, the << operator is only defined for int operands, and :: is a syntax error. Did you mean C++ ? – wildplasser Jul 30 '12 at 19:14

2 Answers2

5

This line is undefined behavior:

sum = sum++;

You can't modify a variable more than once between sequence points. You should just write this instead to increment the sum variable:

sum++;
Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
  • 2
    lol i was gonna mention it but then i saw i overlooked it in my post as well. *facepalm* – Shark Jul 30 '12 at 19:12
2

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

 out << in.rdbuf();

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...

if((minutes >= 1) && (count%3000==0))
{
    time_t date = time(NULL); //store the time in our date
tm* timePtr = localtime(&t); //now we can extact dates out of it

    int day = timePtr->tm_mday;
    int month = timePtr->tm_mon;
    int year = 1900 + timePtr->tm_year; //trust me, you gotta 1900 to it.
    char* filename;
    sprintf(filename, "log-%d-%d-%d.txt", day, month, year);
    ifstream in(filename);
 //   float sum; why is this float when we do sum++ below?
    int sum;
    in >> sum;
    sum++;
    in.close();
    ofstream out(filename);
    out << sum;
    out.flush();
    out.close();
}
Shark
  • 6,513
  • 3
  • 28
  • 50
  • After I comment that out, it doesn't increment anymore. The out.txt just says "1". – TriX Jul 30 '12 at 18:53
  • bah. how bout now? ;) remember, you need to flush the stream in order to complete all IO on it. it might've happened that the new number wasn't written to the file before it was closed. also, can you print sum in the loop? cout << sum or something? – Shark Jul 30 '12 at 19:05
  • Oh my goodness! Thank you so, so much! It works now. Could I ask another simple question or would that be inappropriate because of the title? Yes I can print the sum. – TriX Jul 30 '12 at 19:13
  • sure, shoot as far as i'm concerned. maybe someone will split it, maybe you'll get scolded for it but we'll probably get it fixed before anyone notices ;) – Shark Jul 30 '12 at 19:14
  • Haha, thank you for your generosity. Is it possible to change the title of the document depending on the date of the day? – TriX Jul 30 '12 at 19:15
  • 1
    @Shark: just an FYI - the `flush()` call is not necessary here since `close()` will also flush the stream. – Michael Burr Jul 30 '12 at 19:16
  • you mean like, use files such as "file-07-30-2012.txt" ? yes. generate the string using sprintf or something and use that. just make sure you test that the timestamp string works. – Shark Jul 30 '12 at 19:16
  • @Michael: i'm no C++ guru, at first i thought this was C then C#. and my experience taught me it's better to flush before closing simply because it's C/C++. – Shark Jul 30 '12 at 19:18
  • Shark, yes. I know there's a function in C++ to get the date. How can I put that in the title of the .txt file? – TriX Jul 30 '12 at 19:18
  • help me out here a bit, paste the function signature or tell me whats the name of the function so i can look it up and we'll plug it in the above code. without the signature or fn name i have no idea how to convert it to a string and thus append to the file name. – Shark Jul 30 '12 at 19:20
  • (This is what I know, so it might not be 100% right) But to get the computer's date is: time.wDay, time.wMonth, time.wYear. Or something. – TriX Jul 30 '12 at 19:39
  • try it. or adapt to C++, i'm high so i'm limited to C atm ;) – Shark Jul 30 '12 at 19:40
  • I feel like I'm close to the variable filename. But I guess I'll just ask a completely different question so others can help. Thank you so much, Shark, for your help! – TriX Jul 30 '12 at 20:14
  • whats wrong with the code i posted? it doesn't use any nonportable code either..... unless localtime is UNIX-only :) – Shark Jul 30 '12 at 20:27