Timer and other problems...

Pages: 12
Hey, i'd like to know how to make timers (if possible).
This is my current code(not done, im still learning)
1
2
3
4
5
6
7
8
9
10
void wait(int seconds);
void wait(int seconds)
{
	seconds=seconds*1000;
	sleep(seconds);
}
int main()
{
too much code to post
}


What it is going to do is:

1: ask for a number
2: ask for seconds, minutes or hours
3: count up up to the amount of seconds,minutes or hours and update every second

Here is the error message...
'sleep': identifier not found

so assuming "sleep" doesn't exist, How do i make timers?
i was learning c++ and trying to make some programs ... to learn ... and realised i did not know ANY kinds of timers and started this project.

so assuming "sleep" doesn't exist, How do i make timers?
Last edited on
1
2
3
4
5
6
7
8
int wait(int TotalTime, int CurrentTime){
if(CurrentTime < TotalTime)
{
std::cout << TotalTime - CurrentTime //Time Left
int wait(TotalTime,CurrentTime++);
}
return 0;
}

You would call on the function like:
wait(1000,0)
Where 1000 would be the time (Not in seconds though) and 0 would be there to be the starting time.

It's a recursive function. (Essentially a function that loops by calling on its self)
Thank you, i will try when i can
Probably worked, lol .. but there is other problems too...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
1
1>  main.cpp
1>c:\users\My Username\desktop\testet\timer\timer\main.cpp(63): error C2678: binary '==' : no operator found which takes a left-hand operand of type 'std::string' (or there is no acceptable conversion)
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(470): could be 'bool std::operator ==(const std::_Exception_ptr &,const std::_Exception_ptr &)'
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(475): or       'bool std::operator ==(std::_Null_type,const std::_Exception_ptr &)'
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(481): or       'bool std::operator ==(const std::_Exception_ptr &,std::_Null_type)'
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\system_error(408): or       'bool std::operator ==(const std::error_code &,const std::error_condition &)'
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\system_error(416): or       'bool std::operator ==(const std::error_condition &,const std::error_code &)'
1>          while trying to match the argument list '(std::string, int)'
1>c:\users\My Username\desktop\testet\timer\timer\main.cpp(65): warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
1>c:\users\My Username\desktop\testet\timer\timer\main.cpp(67): error C2678: binary '==' : no operator found which takes a left-hand operand of type 'std::string' (or there is no acceptable conversion)
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(470): could be 'bool std::operator ==(const std::_Exception_ptr &,const std::_Exception_ptr &)'
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(475): or       'bool std::operator ==(std::_Null_type,const std::_Exception_ptr &)'
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(481): or       'bool std::operator ==(const std::_Exception_ptr &,std::_Null_type)'
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\system_error(408): or       'bool std::operator ==(const std::error_code &,const std::error_condition &)'
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\system_error(416): or       'bool std::operator ==(const std::error_condition &,const std::error_code &)'
1>          while trying to match the argument list '(std::string, int)'
1>c:\users\My Username\desktop\testet\timer\timer\main.cpp(69): warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
1>c:\users\My Username\desktop\testet\timer\timer\main.cpp(71): error C2678: binary '==' : no operator found which takes a left-hand operand of type 'std::string' (or there is no acceptable conversion)
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(470): could be 'bool std::operator ==(const std::_Exception_ptr &,const std::_Exception_ptr &)'
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(475): or       'bool std::operator ==(std::_Null_type,const std::_Exception_ptr &)'
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\exception(481): or       'bool std::operator ==(const std::_Exception_ptr &,std::_Null_type)'
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\system_error(408): or       'bool std::operator ==(const std::error_code &,const std::error_condition &)'
1>          c:\program files (x86)\microsoft visual studio 10.0\vc\include\system_error(416): or       'bool std::operator ==(const std::error_condition &,const std::error_code &)'
1>          while trying to match the argument list '(std::string, int)'
1>c:\users\My Username\desktop\testet\timer\timer\main.cpp(73): warning C4244: '=' : conversion from 'double' to 'int', possible loss of data
1>c:\users\My Username\desktop\testet\timer\timer\main.cpp(94): error C2143: syntax error : missing ';' before 'string'
1>c:\users\My Username\desktop\testet\timer\timer\main.cpp(94): error C2146: syntax error : missing ';' before identifier 'seconds'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


will post code in another message, to much for 1 post
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include<iostream>
#include"StdAfx.h"
#include<string>
using namespace std;
bool run=true;
int wait(int TotalTime, int CurrentTime){
if(CurrentTime < TotalTime)
{
std::cout << TotalTime - CurrentTime; //Time Left
wait(TotalTime,CurrentTime++);
}
return 0;
}
int main()
{
	int timermaxmin=0;
	int timermaxsec=0;
	int timermaxhour=0;
    int hours;
    int minutes;
    int seconds;
    while (run=true)
    {
	double amount;
	string secminhour;
	cout<<"How many?\n";
	cin>>amount;
	cout<<"\n";
	cout<<"Seconds,Minutes or Hours?\n";
	cin>>secminhour;
	cout<<"\n";
	cout<<"\n";
	cout<<"\n";
	cout<<"\n";
	cout<<"\n";
	if (secminhour=="Seconds"||secminhour=="seconds"||secminhour=="Minutes"||secminhour=="minutes"||secminhour=="Hours"||secminhour=="hours")
	{
		if(secminhour=="Seconds"||secminhour=="seconds")
        {
			secminhour="seconds";
		}
		else if(secminhour=="minutes"||secminhour=="Minutes")
		{
			secminhour="minutes";
		}
		else if(secminhour=="hours"||secminhour=="Hours")
		{
			secminhour="hours";
		}
		else
		{
			cout<<"ERROR!";
			run=false;
		}
		cout<<"Counting up to "<<amount<<" "<<secminhour<<".";
	}
	else
	{
	    cout<<"Please write seconds, minutes or hours!";
	    run=false;
	}
	
	if (secminhour==seconds)
	{
		timermaxsec=amount;
	}
	else if (secminhour==minutes)
	{
		timermaxmin=amount;
	}
	else if (secminhour==hours)
	{
		timermaxhour=amount;
	}
	else
	{
		cout<<"ERROR!";
	}

	while(minutes<=timermaxmin||seconds<=timermaxsec||hours<=timermaxhour)
	{
		wait(1000,0);
		seconds=seconds+1;
		if (seconds==60)
		{
			seconds=0;
			minutes=minutes+1;
		}
		if (minutes==60)
		{
			minutes=0;
			hours=hours+1;
		}
		cout<<hours<<":"<<minutes":"seconds;
	}

		run=false;
	
  }
}

^ ^
: :
the code(ps: not done AND ima beginner)
Last edited on
You have lots of problem... but i have no anyone Idea... :(
What are you trying to do?

on line 63: if (secminhour=="seconds")
do the same for line 67/71 (the quotes)

I don't know if Pickle Gunner was trolling, but wait function is nonsense. What you're looking for is Sleep() (note the capital S)

http://msdn.microsoft.com/en-us/library/windows/desktop/ms686298%28v=vs.85%29.aspx
Thanks sir, but it did not work i am afraid... linking new code and the 3 error messages left(still learning, dont be mad if i am missing something basic)

Errors:
1
2
3
4
5
1
1>  main.cpp
1>c:\users\My Username\desktop\testet\klocka\klocka\main.cpp(74): error C3861: 'Sleep': identifier not found
1>c:\users\My Username\desktop\testet\klocka\klocka\main.cpp(86): error C2143: syntax error : missing ';' before 'string'
1>c:\users\My Username\desktop\testet\klocka\klocka\main.cpp(86): error C2146: syntax error : missing ';' before identifier 'seconds'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include<iostream>
#include"StdAfx.h"
#include<string>
using namespace std;
bool run=true;
int main()
{
	int timermaxmin=0;
	int timermaxsec=0;
	int timermaxhour=0;
    int hours;
    int minutes;
    int seconds;
    while (run=true)
    {
	int amount;
	string secminhour;
	cout<<"How many?\n";
	cin>>amount;
	cout<<"\n";
	cout<<"Seconds,Minutes or Hours?\n";
	cin>>secminhour;
	cout<<"\n";
	cout<<"\n";
	cout<<"\n";
	cout<<"\n";
	cout<<"\n";
	if (secminhour=="Seconds"||secminhour=="seconds"||secminhour=="Minutes"||secminhour=="minutes"||secminhour=="Hours"||secminhour=="hours")
	{
		if(secminhour=="Seconds"||secminhour=="seconds")
        {
			secminhour="seconds";
		}
		else if(secminhour=="minutes"||secminhour=="Minutes")
		{
			secminhour="minutes";
		}
		else if(secminhour=="hours"||secminhour=="Hours")
		{
			secminhour="hours";
		}
		else
		{
			cout<<"ERROR!";
			run=false;
		}
		cout<<"Counting up to "<<amount<<" "<<secminhour<<".";
	}
	else
	{
	    cout<<"Please write seconds, minutes or hours!";
	    run=false;
	}
	
	if (secminhour=="seconds")
	{
		timermaxsec=amount;
	}
	else if (secminhour=="minutes")
	{
		timermaxmin=amount;
	}
	else if (secminhour=="hours")
	{
		timermaxhour=amount;
	}
	else
	{
		cout<<"ERROR!";
	}

	while(minutes<=timermaxmin||seconds<=timermaxsec||hours<=timermaxhour)
	{
		Sleep(1000);
		seconds=seconds+1;
		if (seconds==60)
		{
			seconds=0;
			minutes=minutes+1;
		}
		if (minutes==60)
		{
			minutes=0;
			hours=hours+1;
		}
		cout<<hours<<":"<<minutes":"seconds;
	}

		run=false;
	
  }
}
for Sleep() you need to #include <windows.h> (look at the link).

line 86: cout<<hours<<":"<<minutes<<":"<<seconds;
You, sir, are a gentleman!
NEW FRIGGIN ERRORS?!?!?!?!?
1
2
3
1>c:\users\My Username\desktop\testet\klocka\klocka\main.cpp(73): warning C4700: uninitialized local variable 'minutes' used
1>c:\users\My Username\desktop\testet\klocka\klocka\main.cpp(76): warning C4700: uninitialized local variable 'seconds' used
1>c:\users\My Username\desktop\testet\klocka\klocka\main.cpp(85): warning C4700: uninitialized local variable 'hours' used


did i fail at defining minutes, seconds and hours?
EDIT: Solved :D i just didnt give ethem any numbers
Last edited on
you need to initialize minutes/seconds/hours to 0 on line 11/12/13
NOT SOLVED!!!

new problem: the program keeps on counting after limit, ex

Console:how many?
Me:1
Console:minutes, seconds or hours
me:minutes



console: counting up to 1 minutes.(yes! it sounds weird, and it is weird!)
0:0:1
0:0:2
0:0:3
0:0:4
0:0:5
0:0:6
0:0:7
0:0:8
0:0:9
0:0:10
...

until

...
0:1:0
0:1:1
0:1:2
and it just goes on... why?

Code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include<iostream>
#include"StdAfx.h"
#include<string>
#include<Windows.h>
using namespace std;
bool run=true;
int main()
{
	int timermaxmin=0;
	int timermaxsec=0;
	int timermaxhour=0;
    int hours=0;
    int minutes=0;
    int seconds=0;
    while (run=true)
    {
	int amount;
	string secminhour;
	cout<<"How many?\n";
	cin>>amount;
	cout<<"\n";
	cout<<"Seconds,Minutes or Hours?\n";
	cin>>secminhour;
	cout<<"\n";
	cout<<"\n";
	cout<<"\n";
	cout<<"\n";
	cout<<"\n";
	if (secminhour=="Seconds"||secminhour=="seconds"||secminhour=="Minutes"||secminhour=="minutes"||secminhour=="Hours"||secminhour=="hours")
	{
		if(secminhour=="Seconds"||secminhour=="seconds")
        {
			secminhour="seconds";
		}
		else if(secminhour=="minutes"||secminhour=="Minutes")
		{
			secminhour="minutes";
		}
		else if(secminhour=="hours"||secminhour=="Hours")
		{
			secminhour="hours";
		}
		else
		{
			cout<<"ERROR!";
			run=false;
		}
		cout<<"Counting up to "<<amount<<" "<<secminhour<<"."<<endl;
	}
	else
	{
	    cout<<"Please write seconds, minutes or hours!";
	    run=false;
	}
	
	if (secminhour=="seconds")
	{
		timermaxsec=amount;
	}
	else if (secminhour=="minutes")
	{
		timermaxmin=amount;
	}
	else if (secminhour=="hours")
	{
		timermaxhour=amount;
	}
	else
	{
		cout<<"ERROR!";
	}

	while(minutes<=timermaxmin||seconds<=timermaxsec||hours<=timermaxhour)
	{
		Sleep(1000);
		seconds=seconds+1;
		if (seconds==60)
		{
			seconds=0;
			minutes=minutes+1;
		}
		if (minutes==60)
		{
			minutes=0;
			hours=hours+1;
		}
		cout<<hours<<":"<<minutes<<":"<<seconds<<endl;
	}

		run=false;
	
  }
}
Last edited on
change the or in your while to and
|| --> && line 73
Last edited on
actually solved that one by myself... but now ... a small problem

My conversation with the xonsole:
1
2
3
4
5
6
7
8
Console:how many?
Me:1
Console:minutes, seconds or hours
me:seconds
console: counting up to 1 seconds
console:0:0:1
console:0:0:2
console:how many?

it should stop at 0:0:1 and close the program
Last edited on
does it start at 0 or at 1?

because the output tells me it counts from 1 to 2 , which is excactly 1 second
calculate the amount of seconds from the entered amount
replace the while loop on line 73 with a for loop that simply runs to the amount of seconds.

Btw: line 15: this while (run=true) assigns true to run (the loop will never end). you want while (run==true) or simply while (run)
starts at 0:0:1
BUT i tried int seconds=-1
it was like
0:0:0
0:0:1
0:0:2
How many?
EDIT: solved!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include<iostream>
#include"StdAfx.h"
#include<string>
#include<Windows.h>
using namespace std;
bool run=true;
int main()
{
	int timermaxmin=0;
	int timermaxsec=0;
	int timermaxhour=0;
    int hours=0;
    int minutes=0;
    int seconds=0;
	bool run=true;
    while (run==true)
	{
	int amount;
	string secminhour;
	cout<<"How many?\n";
	cin>>amount;
	cout<<"Seconds,Minutes or Hours?\n";
	cin>>secminhour;
	cout<<"\n";
	cout<<"\n";
	cout<<"\n";
	cout<<"\n";
	cout<<"\n";
	if (secminhour=="Seconds"||secminhour=="seconds"||secminhour=="Minutes"||secminhour=="minutes"||secminhour=="Hours"||secminhour=="hours")
	{
		if(secminhour=="Seconds"||secminhour=="seconds")
        {
			secminhour="seconds";
		}
		else if(secminhour=="minutes"||secminhour=="Minutes")
		{
			secminhour="minutes";
		}
		else if(secminhour=="hours"||secminhour=="Hours")
		{
			secminhour="hours";
		}
		else
		{
			cout<<"ERROR!";
			run=false;
		}
		cout<<"Counting up to "<<amount<<" "<<secminhour<<"."<<endl;
	}
	else
	{
	    cout<<"Please write seconds, minutes or hours!";
	    run=false;
	}
	
	if (secminhour=="seconds")
	{
		timermaxsec=amount;
	}
	else if (secminhour=="minutes")
	{
		timermaxmin=amount;
	}
	else if (secminhour=="hours")
	{
		timermaxhour=amount;
	}
	else
	{
		cout<<"ERROR!";
	}

	while(minutes<=timermaxmin&&seconds<=timermaxsec&&hours<=timermaxhour)
	{
		cout<<hours<<":"<<minutes<<":"<<seconds<<endl;
		seconds=seconds+1;
		if (seconds==60)
		{
			seconds=0;
			minutes=minutes+1;
		}
		if (minutes==60)
		{
			minutes=0;
			hours=hours+1;
		}
		Sleep(1000);
	}
	cout<<"The timer has now waited for "<<amount<<" "<<secminhour<<".";
	cin.ignore();
	cin.get();
	run=false;
	
  }
}


Thanks, coder77 and darkmaster for helping me :D
Last edited on
EDIT: solved!
Nope: try enter 1 minute.

Consider the for loop
coder777 is right just handle 1minute as 60seconds and 1hours as 3600seconds.
this way you only need to check for for seconds.

also use a for loop where you simply run from 0 to the stoptime and inside the for loop you do nothing besides waiting a second and printing the output
Last edited on
lol this is the 1293642183764891723689172349871234978123465712530412341234601236491237641982346128734561287936897126478512436512783452716345t12098381253487652346127936412653421736479821341287359th time i ask for your help on this post...


Console:how many?
me:1
Console:seconds, minutes or hours?
me:minutes
console:counting up to 1 minutes
0:0:0
The timer has counted up to 1 minutes

What the fuck?
Pages: 12