| View previous topic :: View next topic |
| Author |
Message |
Travis13 Expert Cheater
Reputation: 0
Joined: 17 Feb 2007 Posts: 199
|
Posted: Tue Sep 30, 2008 9:28 pm Post subject: [C++] Timer Problem +rep for a fix(maybe)(its wierd) |
|
|
Hey guys take a quick look at MY code that i wrote and did not look at any tutorials or anything.
| Code: | #include <iostream>
#include <windows.h>
using namespace std;
bool Exit = false;
bool repeat = false;
int number = 0;
void timer()
{
while(repeat == false)
{
number = number + 1;
Sleep(1000);
cout << "The # is " << number << endl;
}
}
int main()
{
cout << "Welcome to a little time thingy :O" << endl;
cout << "F1 = start" << endl;
cout << "F2 = stop" << endl;
cout << "F3 = EXIT" << endl;
while (Exit == false)
{
if(GetAsyncKeyState(VK_F1))
{
timer();
}
else if(GetAsyncKeyState(VK_F2))
{
repeat = false;
}
else if(GetAsyncKeyState(VK_F3))
{
Exit = true;
}
}
return 0;
}
|
so heres the deal.........when it opens and i press f3.......it closes When it opens and i press f1 to start the timer...it goes.....and goes..and goes.
FOREVER.
F2 for some reason does not stop it and F3 doesnt close the program anymore.........please PLEASE someone tell me why i really want this to get working so i can add cooler things on it later
edit: please make sure if u correct me on something that its actually right and will make it work (as in tested it) _________________
Learning C++, trying, failing, never gonna give up tho 
Last edited by Travis13 on Tue Sep 30, 2008 9:56 pm; edited 1 time in total |
|
| Back to top |
|
 |
kitterz Grandmaster Cheater Supreme
Reputation: 0
Joined: 24 Dec 2007 Posts: 1268
|
Posted: Tue Sep 30, 2008 9:47 pm Post subject: |
|
|
| Code: | | while(repeat == false) |
means it will loop as long as repeat is false.
Though on F2
Here...you are just making restart false...doing nothing
What you want is to change it so repeat is true, and so that the statement
| Code: | | while(repeat == false) |
is no longer true.
So change
to
_________________
|
|
| Back to top |
|
 |
Travis13 Expert Cheater
Reputation: 0
Joined: 17 Feb 2007 Posts: 199
|
Posted: Tue Sep 30, 2008 9:51 pm Post subject: |
|
|
hey thanks for replying and all but not trying to be mean but i said tha tu should try it before u think ur all right lol.......cuz trust me im not THAT stupid at c++ and i tried it both ways and it doesnt work..............btw i also said that f3 doesnt work while the timer is going too so thats why im so confused :S
dont get me wrong kitterz i know ur good at coding but even u can agree with me its weird.......f3 works before the timer starts but it doesnt when its going........even thought i told it to keep going until exit = false; or w/e _________________
Learning C++, trying, failing, never gonna give up tho  |
|
| Back to top |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Tue Sep 30, 2008 9:56 pm Post subject: |
|
|
Well....say you press F2..you call the timer function....
and then display 1..2...3..4... etc
but then you are locked in that function with no way to stop it..
also, putting
| Code: | else if(GetAsyncKeyState(VK_F2))
{
repeat = false;
} |
is kinda pointless, you cant stop it unless you have started it..
my suggestion, add a condition in the timer() function
This works....
| Code: | #include <iostream>
#include <windows.h>
using namespace std;
bool Exit = false;
bool repeat = false;
int number = 0;
void timer()
{
while(repeat == false)
{
number = number + 1;
Sleep(1000);
cout << "The # is " << number << endl;
if(GetAsyncKeyState(VK_F2))
{
repeat = true;
cout << " Timed up to.. " << number << endl;
}
}
}
int main()
{
cout << "Welcome to a little time thingy :O" << endl;
cout << "F1 = start" << endl;
cout << "F2 = stop" << endl;
cout << "F3 = EXIT" << endl;
while (Exit == false)
{
if(GetAsyncKeyState(VK_F1))
{
timer();
}
else if(GetAsyncKeyState(VK_F3))
{
Exit = true;
}
}
return 0;
}
|
_________________
|
|
| Back to top |
|
 |
Travis13 Expert Cheater
Reputation: 0
Joined: 17 Feb 2007 Posts: 199
|
Posted: Tue Sep 30, 2008 10:03 pm Post subject: |
|
|
OMG......BRILLIANT  
lol thank you so much ur my idol now (not really but ur still great)
i like it cuz u thought outside of the box and got it right instead of trying to find something WRONG in my coding (if u know wat i mean :S)
and plus u added the little cout at the end that displays the final number....i would of never thought of that cuz if u put number in there again it would completely stop and stuff so i never thought it it stopping cuz of the repeat = false thing and NVM i tend to babble on
anyways thanks for ur help and ill rep u as promised since u fixed it, said it nicely, and added something small and not confusing
(please tell me u didnt do it for the rep tho lol) anyways thanks......yeah im hyper atm
edit: i was the last one to rep u lmfao :S so idk if i should do it again _________________
Learning C++, trying, failing, never gonna give up tho  |
|
| Back to top |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Tue Sep 30, 2008 10:11 pm Post subject: |
|
|
Lol...it really doesnt matter rep or not..Im just here to learn too
I'm actually suprised with myself too, as a matter of fact.
OH and u wont get very far with ME as ur idol LOL _________________
|
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Tue Sep 30, 2008 10:15 pm Post subject: |
|
|
Why are you using GetAsyncKeyState for this?
Anyway, keeping with the spirit:
| Code: | #include <iostream>
#include <windows.h>
using namespace std;
int number = 0;
void timer(void)
{
do{
number++;
cout << "The # is " << number << endl;
Sleep(1000);
}while(!GetAsyncKeyState(VK_F2));
}
int main()
{
cout << "Welcome to a little time thingy :O" << endl;
cout << "F1 = start" << endl;
cout << "F2 = stop" << endl;
cout << "F3 = EXIT" << endl;
do{
if(GetAsyncKeyState(VK_F1)){
timer();
}Sleep(10);
}while(!GetAsyncKeyState(VK_F3));
return 0;
} |
I feel so dirty
Last edited by hcavolsdsadgadsg on Tue Sep 30, 2008 10:21 pm; edited 1 time in total |
|
| Back to top |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Tue Sep 30, 2008 10:18 pm Post subject: |
|
|
slovach saves the day, rep him instead.
as far as loops go,
do = while ????? _________________
|
|
| Back to top |
|
 |
Travis13 Expert Cheater
Reputation: 0
Joined: 17 Feb 2007 Posts: 199
|
Posted: Tue Sep 30, 2008 10:28 pm Post subject: |
|
|
ahhh do while loops :S im still a nub lol......slovache cant u just keep it simpler
anyways next question if ppl are still in this thread :S......
i want it to count minutes and hours i tried this.......
| Code: | #include <iostream>
#include <windows.h>
using namespace std;
bool Exit = false;
bool repeatseconds = false;
bool repeatminutes = false;
bool repeathours = false;
int seconds = 0;
int minutes = 0;
int hours = 0;
void timerseconds()
{
while(repeatseconds == false)
{
seconds = seconds + 1;
Sleep(1000);
cout << "The # is " << seconds << endl;
if(GetAsyncKeyState(VK_F2))
{
repeatseconds = true;
cout << "Time up to: " << seconds << endl;
}
}
}
void timerminutes()
{
while(repeatminutes == false)
{
minutes = minutes + 1;
Sleep(60000);
cout << "The # is " << minutes << endl;
if(GetAsyncKeyState(VK_F2))
{
repeatminutes = true;
cout << "Time up to: " << minutes << endl;
}
}
}
int main()
{
cout << "Welcome to a little time thingy :O" << endl;
cout << "F1 = start" << endl;
cout << "F2 = stop" << endl;
cout << "F3 = EXIT" << endl;
while (Exit == false)
{
if(GetAsyncKeyState(VK_F1))
{
timerseconds();
timerminutes();
}
else if(GetAsyncKeyState(VK_F3))
{
Exit = true;
}
}
return 0;
}
|
but it still didnt count the minutes (having a bad c++ day)
edit: plz answer this quickly......why did slovache use while(!GetAsyncKeyState blah blah..........
like why did u use the " ! " _________________
Learning C++, trying, failing, never gonna give up tho  |
|
| Back to top |
|
 |
hcavolsdsadgadsg I'm a spammer
Reputation: 26
Joined: 11 Jun 2007 Posts: 5801
|
Posted: Tue Sep 30, 2008 10:35 pm Post subject: |
|
|
It's the logical negation operator.
I could have said | Code: | | while(GetAsyncKeyState(VK_F2) == false) | also
You'd probably have an easier time if you took a look at the time functions.
http://msdn.microsoft.com/en-us/library/ms725473(VS.85).aspx
Last edited by hcavolsdsadgadsg on Tue Sep 30, 2008 10:38 pm; edited 1 time in total |
|
| Back to top |
|
 |
Travis13 Expert Cheater
Reputation: 0
Joined: 17 Feb 2007 Posts: 199
|
Posted: Tue Sep 30, 2008 10:37 pm Post subject: |
|
|
| slovach wrote: | It's the logical negation operator.
I could have said | Code: | | while(GetAsyncKeyState(VK_F2) == false) | also |
ok sweet thanks im learning so much today ....but how do i get the minutes and hours and etc working? how come it doesn't work? sorry for the questiongs i just really want to get good at all this nub c++ crap and get into all the hard stuff later _________________
Learning C++, trying, failing, never gonna give up tho  |
|
| Back to top |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Tue Sep 30, 2008 10:44 pm Post subject: |
|
|
you mean keep counting in seconds and then at the end say 250 sec...
you timed 4 minutes and 10 seconds?
2 ideas (may be wrong)
-Make a loop so every 60 seconds you
add 1 to minutes and restart seconds at 0
or
- just take total seconds and divide by 60+ use the remainder operator to get remaining seconds and display them also _________________
|
|
| Back to top |
|
 |
Travis13 Expert Cheater
Reputation: 0
Joined: 17 Feb 2007 Posts: 199
|
Posted: Tue Sep 30, 2008 10:51 pm Post subject: |
|
|
I LOVE the second idea ill just make another function called like....minutes or w/e lol and return the minutes and % or w/e
sweet im gonna post it in the thread if i get it to work.....never thought of that idea thanks
quick question before i write a whole thing and get errors.....can u call functions inside of a function? _________________
Learning C++, trying, failing, never gonna give up tho  |
|
| Back to top |
|
 |
manc Grandmaster Cheater
Reputation: 1
Joined: 16 Jun 2006 Posts: 551
|
Posted: Tue Sep 30, 2008 10:54 pm Post subject: |
|
|
| Travis13 wrote: |
quick question before i write a whole thing and get errors.....can u call functions inside of a function? |
HELL YEA U CAN! _________________
|
|
| Back to top |
|
 |
Travis13 Expert Cheater
Reputation: 0
Joined: 17 Feb 2007 Posts: 199
|
Posted: Tue Sep 30, 2008 11:04 pm Post subject: |
|
|
lol manc (dont know what manc means btw but ANYWAYS)
omg im so sorry but i want this to be a little cool neat program and i have always wondered how to do this..........how the fuck to i make just one thing coloured :S:S........i want to make the answer in colour like............
The # of seconds is 5
and 5 would be red while the rest is white
plz dont just say.....setconsoletextattribute or w/e cuz i got no clue wat to do with that....i need an example,,,,,PLZ AND TY (im so desperate for help :S)
edit: without using system color because i dont want to get bitched at in the future for using it and stuff _________________
Learning C++, trying, failing, never gonna give up tho  |
|
| Back to top |
|
 |
|