<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Alarm Function on Programmer's Insight</title><link>https://blog.ganapathy.xyz/tags/alarm-function/</link><description>Recent content in Alarm Function on Programmer's Insight</description><generator>Hugo</generator><language>en-US</language><lastBuildDate>Sat, 23 Mar 2013 10:57:13 -0700</lastBuildDate><atom:link href="https://blog.ganapathy.xyz/tags/alarm-function/index.xml" rel="self" type="application/rss+xml"/><item><title>Nitty-Gritty Things - 1</title><link>https://blog.ganapathy.xyz/2013/03/nitty-gritty-things-1.html</link><pubDate>Sat, 23 Mar 2013 10:53:00 -0700</pubDate><guid>https://blog.ganapathy.xyz/2013/03/nitty-gritty-things-1.html</guid><description>&lt;div&gt;&#10;In this blog lets talk about some nitty-gritty implementation stuffs.&lt;br&gt;&#10;&lt;br&gt;&#10;Okay. So assuming the reader knows C/C++&lt;br&gt;&#10;&lt;br&gt;&#10;a) How to add time restrictions to user input?&lt;br&gt;&#10;&lt;br&gt;&#10;To elaborate, we know in C, the user input is read using scanf/getc/getchar and in C++ using cin object.&lt;br&gt;&#10;&lt;br&gt;&#10;So our objective is to add some time limit so that user inputs the value before the timeout.&lt;br&gt;&#10;&lt;br&gt;&#10;a) You can do by calling a script written in bash/python etc from C code and evaluate.&lt;br&gt;&#10;b) May be create a thread and fire the thread when timeout happens.&lt;br&gt;&#10;&lt;br&gt;&#10;Is there any other way? &amp;lt;Think before scrolling down&amp;gt;&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;. &lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;&lt;br&gt;&#10;&lt;a href="http://www.gnu.org/software/libc/manual/html_node/Setting-an-Alarm.html" rel="noopener noreferrer"&gt;http://www.gnu.org/software/libc/manual/html_node/Setting-an-Alarm.html&lt;/a&gt;&lt;br&gt;&#10;&lt;a href="http://pubs.opengroup.org/onlinepubs/009695399/functions/alarm.html" rel="noopener noreferrer"&gt;http://pubs.opengroup.org/onlinepubs/009695399/functions/alarm.html&lt;/a&gt; &lt;br&gt;&#10;&lt;br&gt;&#10;After reading the above links, I came up with this.&lt;br&gt;&#10;&lt;br&gt;&#10;#include&amp;lt;stdio.h&amp;gt;&lt;br&gt;&#10;#include&amp;lt;unistd.h&amp;gt;&lt;br&gt;&#10;&lt;br&gt;&#10;#define TIMEOUT 5&lt;br&gt;&#10;int main()&lt;br&gt;&#10;{&lt;br&gt;&#10;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int t_iTemp;&lt;br&gt;&#10;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; while( alarm(TIMEOUT), ( t_iTemp = getchar() ) != EOF )&lt;br&gt;&#10;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;br&gt;&#10;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; putchar(t_iTemp);&lt;br&gt;&#10;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;br&gt;&#10;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return 0;&lt;br&gt;&#10;&lt;br&gt;&#10;}&lt;br&gt;&#10;&lt;br&gt;&#10;This program waits for 5 secs. If user doesn't input anything, it exits else just echoes the input&lt;br&gt;&#10;and waits for next 5 secs.&lt;br&gt;&#10;&lt;br&gt;&#10;The idea is simple i.e to generate a signal based on timer.&lt;br&gt;&#10;But the important takeaway is about alarm function.&lt;br&gt;&lt;br&gt;Okay lets see one more.&lt;br&gt;&#10;&lt;br&gt;&#10;b) Say you are developing a clock app. Now you will have conditions for hours,seconds,mins etc&lt;br&gt;&#10;&lt;br&gt;&#10;The thing we will talk is how do you display 00 when the clock moves from 59 in case of mins/secs and 23 in case of hour.&lt;br&gt;&#10;&lt;br&gt;&#10;When we think about it, its nothing but printing "00". But remember here we are talking about numbers :-)&lt;br&gt;&#10;&lt;br&gt;&#10;What happens when you assign 00 to a integer and print it. It should be printing 0 instead of 00.&lt;br&gt;&#10;So how do you print an integer as 00. This is what we will be talking.&lt;br&gt;&#10;&lt;br&gt;&#10;&amp;lt;Think before scrolling&amp;gt;&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;..&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;..&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;.&lt;br&gt;&#10;&lt;br&gt;&#10;You can use a format specifier :D&lt;br&gt;&#10;&lt;br&gt;&#10;int a = 00;&lt;br&gt;&#10;printf("%d",a) -&amp;gt; 0&lt;br&gt;&#10;printf("%02d",a) -&amp;gt; 00 &lt;br&gt;&#10;&lt;br&gt;&#10;However there are functions which can replicate a given number/character which is the takeaway.&lt;br&gt;&#10;Check the below link. &lt;br&gt;&#10;&lt;a href="http://www.cplusplus.com/reference/iomanip/setfill/" rel="noopener noreferrer"&gt;http://www.cplusplus.com/reference/iomanip/setfill/&lt;/a&gt;&lt;br&gt;&lt;br&gt;&amp;nbsp;int hrs = 0;&lt;br&gt;&#10;&amp;nbsp;cout&amp;lt;&amp;lt;setw(2)&amp;lt;&amp;lt;setfill('0')&amp;lt;&amp;lt;hrs&amp;lt;&amp;lt;endl; &lt;br&gt;&#10;&lt;br&gt;&#10;Hope you enjoyed reading and got something out of it.&lt;br&gt;&#10;Please post comments/suggestions(if any).&lt;/div&gt;</description></item></channel></rss>