-
Changing Displayed Text?
Alright, here's my problem.
After messing around with C++, I've came up with this little program:
Code:
#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
unsigned int secs = 0;
unsigned int mins = 0;
unsigned long int hours = 0; //Just in case the person's afk for a looong time :P
int change = 0;
cout<<"--AFK 1.0--\n\n";
cout<<"Press [ENTER] key to continue";
cin.get();
cout<<"\n\n";
while(change == 0)
{
cout<<"\t\nAFK for "<<hours<<" hours "<<mins<<" minutes "<<secs<<" seconds.";
Sleep(1000);
secs++;
if (secs >= 60)
{
mins++;
secs = 0;
}
if (mins >= 60)
{
hours++;
mins = 0;
}
}
return 0;
}
Right?
Well, after Googling the heck out of this, I can't seem to come up with a definite solution aside from putting a above the "AFK for" statement.
What I mean is, having the seconds, minutes and hours change without having it display everything over and over again.
Also, if it helps, I am using Dev-C++ to compile all this.
Thank you very much for your time!
-
Thy this, it should clear the screen every time it goes through the loop :)
Code:
system("cls");
cout<<"\t\nAFK for "<<hours<<" hours "<<mins<<" minutes "<<secs<<" seconds.";
Sleep(1000);
secs++;
-
Or like this?
Code:
cout<<"AFK for "<<hours<<" hours "<<mins<<" minutes "<<secs<<" seconds."<<"\r";
Sleep(1000);
secs++;
This one wont clear the screen, just the line containing that counter you have going there
-
Yeah i have to agree with Loic, that's probably you're best option. I know what you want it to do, but i dont know exactly how you would go about doing it. It seems like something a windows app would be better suited for but you're not ready for that yet.
Maybe someone else can help ya but i have to say that you should try what Loic said.
Good luck! :)
-
Ahh, yes, perfect, that did the trick!
I can't thank you guys enough that's exactly what I needed, that was driving me crazy!
-
What about leap years, Feburary, leap seconds etc? :) I'd look into the functions in ctime/time.h, specifically:
Code:
time_t start, end;
double elapsed;
time(&start);
// [...]
time(&end);
elapsed = difftime(&end, &start);
std::cout << "Time elapsed: " << elapsed << 's' << std::endl;
Click on the functions in the above code to get to function documentation.
Also see http://cppreference.com/stddate/ctime.html