Creating a Simple timer in C (only)

Creating a Simple timer in C (only)

Page 1 of 1

5 Replies - 11019 Views - Last Post: 12 December 2009 - 10:15 AM Rate Topic: ***** 1 Votes

#1 Fisek  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-December 09

Creating a Simple timer in C (only)

Posted 12 December 2009 - 05:17 AM

I have to write a simple timer code in C, however, I am not familiar with C therefore I tried to write a simple timer on C++. I tried to convert it but somehow there exists some problems about the headers. (I guess). SO the question is, in the code below, there exists the C++ code that I have written, could someone help me to convert it to C in the same logical structure. (btw. (while1==1) is used just for timer to work continuously without interruption)

#include <iostream>

using namespace std;

#include <stdio.h>
#include <time.h>
#include <stdlib.h>

void Wait(int seconds)
{
 clock_t endwait;
 endwait = clock () + seconds * CLK_TCK;
 while (clock() < endwait) {}
}



int main(void)
{
int i;
int m;
i=0;
m=0;
while (1==1)
{
  Wait( 1 );
	i++;
	if (i==15)
	{
		i=0;
		m=m+1;
		if(m==2)
		{
			m=0;
		}

	}

	system("cls");
cout<<m<<":"<<i<<endl;


}
 

   return 0;
} 






Thank you,

Fisek

Is This A Good Question/Topic? 0
  • +

Replies To: Creating a Simple timer in C (only)

#2 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: Creating a Simple timer in C (only)

Posted 12 December 2009 - 05:35 AM

Make your best effort and show it to us.

At the moment it could well be that what you have given us is exactly the starting point your teacher provided with the challenge of turning it into C.

Show us you have done the work that must have been set you before this challenge was assigned.

Here's a place to start
If we compile your code in a C compiler we get these errors reported.
dic.c:1:20: error: iostream: No such file or directory
dic.c:3: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘namespace’
dic.c: In function ‘Wait’:
dic.c:12: error: ‘CLK_TCK’ undeclared (first use in this function)
dic.c:12: error: (Each undeclared identifier is reported only once
dic.c:12: error: for each function it appears in.)
dic.c: In function ‘main’:
dic.c:40: error: ‘cout’ undeclared (first use in this function)
dic.c:40: error: ‘endl’ undeclared (first use in this function)

What do they suggest needs changing?
Was This Post Helpful? 0
  • +
  • -

#3 Fisek  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-December 09

Re: Creating a Simple timer in C (only)

Posted 12 December 2009 - 05:50 AM

View Postjanotte, on 12 Dec, 2009 - 04:35 AM, said:

Make your best effort and show it to us.

At the moment it could well be that what you have given us is exactly the starting point your teacher provided with the challenge of turning it into C.

Show us you have done the work that must have been set you before this challenge was assigned.

Here's a place to start
If we compile your code in a C compiler we get these errors reported.
dic.c:1:20: error: iostream: No such file or directory
dic.c:3: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘namespace’
dic.c: In function ‘Wait’:
dic.c:12: error: ‘CLK_TCK’ undeclared (first use in this function)
dic.c:12: error: (Each undeclared identifier is reported only once
dic.c:12: error: for each function it appears in.)
dic.c: In function ‘main’:
dic.c:40: error: ‘cout’ undeclared (first use in this function)
dic.c:40: error: ‘endl’ undeclared (first use in this function)

What do they suggest needs changing?






First of all this is not an assignment that I have been working on. I am trying to implement a timer on a board's LCD. And I am using MPLAB to do this. Secondly, I tried to use functions to stand for "wait()" function of C++, and could not find an exact result. That's why you may help me by at least refering to use another function that may stand for wait(). (In my surf on internet I come across functions like delay() and sleep(), however they acquire some header files which needs extra header files if been added on the workspace.)

BTW thanks for your rapid answer.
Was This Post Helpful? 0
  • +
  • -

#4 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 988
  • View blog
  • Posts: 5,135
  • Joined: 28-September 06

Re: Creating a Simple timer in C (only)

Posted 12 December 2009 - 06:05 AM

Ahhh I see you'd like someone to check the Google for you

OK
http://cc.byexamples...create-a-timer/
http://www.cs.cf.ac....e/C/node21.html
http://www.cs.cf.ac.uk/Dave/C/
http://gd.tuwien.ac....ion2_22_28.html
Was This Post Helpful? 0
  • +
  • -

#5 Fisek  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 12-December 09

Re: Creating a Simple timer in C (only)

Posted 12 December 2009 - 06:17 AM

View Postjanotte, on 12 Dec, 2009 - 05:05 AM, said:





No Janotte I wouldn't want that. I really don't understand how can you be this much of aggressive towards this? And don't worry I already have checked the Google for myself. Anyway, I guess I would say thank you.
Was This Post Helpful? 0
  • +
  • -

#6 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2247
  • View blog
  • Posts: 9,237
  • Joined: 18-February 07

Re: Creating a Simple timer in C (only)

Posted 12 December 2009 - 10:15 AM

C++ has been nice enough to put much of the standard C library into the standard C++ library. So if you can do it in C then you can probably do it in C++ without too much effort.

C++ import C's "time.h" as "ctime"


SO this works just fine:
#include <iostream>
#include <ctime>


void Wait(int seconds)
{
	std::clock_t endwait;
	endwait = std::clock () + seconds * CLK_TCK;
	while (std::clock() < endwait) {}
}


int main() {
	std::cout << "wait for it..." << std::endl;
	Wait(1);
	std::cout << "wait for it..." << std::endl;
	Wait(2);
	std::cout << "!!! HELLO WORLD !!!" << std::endl;
	return 0;
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1