<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: How-To: Get valid integer input in C++ (a stupidly long solution to a stupidly simple problem)</title>
	<atom:link href="http://r3dux.org/2011/11/how-to-get-valid-integer-input-in-c-a-stupidly-long-solution-to-a-stupidly-simple-problem/feed/" rel="self" type="application/rss+xml" />
	<link>http://r3dux.org/2011/11/how-to-get-valid-integer-input-in-c-a-stupidly-long-solution-to-a-stupidly-simple-problem/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=how-to-get-valid-integer-input-in-c-a-stupidly-long-solution-to-a-stupidly-simple-problem</link>
	<description>A number-pimping side project from the valleys in *NEW* upside-down flavour.</description>
	<lastBuildDate>Tue, 15 May 2012 23:01:55 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
	<item>
		<title>By: r3dux</title>
		<link>http://r3dux.org/2011/11/how-to-get-valid-integer-input-in-c-a-stupidly-long-solution-to-a-stupidly-simple-problem/#comment-7181</link>
		<dc:creator>r3dux</dc:creator>
		<pubDate>Thu, 17 Nov 2011 07:23:46 +0000</pubDate>
		<guid isPermaLink="false">http://r3dux.org/?p=5707#comment-7181</guid>
		<description>Great advice - which I&#039;ve tried to take on board - but I feel like I&#039;m losing the plot with all this =/

- &lt;a href=&quot;http://r3dux.org/files/code/GetInt/48e-Get-Valid-Int-(Reusable-Function-Version).7z&quot; rel=&quot;nofollow&quot;&gt;48e-Get-Valid-Int-(Reusable-Function-Version).7z&lt;/a&gt;
- &lt;a href=&quot;http://r3dux.org/files/code/GetInt/48f-Get-Valid-Int-(Unit-Test-Version).7z&quot; rel=&quot;nofollow&quot;&gt;48f-Get-Valid-Int-(Unit-Test-Version).7z&lt;/a&gt;

I know I should really have multiple projects in the same workspace to perform unit tests but I&#039;m getting towards the end of it...

If you have the time to take a quick swiz (they&#039;re both Code::Blocks projects) and let me know if I&#039;ve got the right end of the stick or if I&#039;ve got the wrong stick entirely (which is the feeling I&#039;ve got) then it would be greatly appreciated - no worries if not tho, I know you&#039;re a busy bloke!

/me hangs up his coding hat for the day...</description>
		<content:encoded><![CDATA[<p>Great advice &#8211; which I&#8217;ve tried to take on board &#8211; but I feel like I&#8217;m losing the plot with all this =/</p>
<p>- <a href="http://r3dux.org/files/code/GetInt/48e-Get-Valid-Int-(Reusable-Function-Version).7z">48e-Get-Valid-Int-(Reusable-Function-Version).7z</a><br />
- <a href="http://r3dux.org/files/code/GetInt/48f-Get-Valid-Int-(Unit-Test-Version).7z">48f-Get-Valid-Int-(Unit-Test-Version).7z</a></p>
<p>I know I should really have multiple projects in the same workspace to perform unit tests but I&#8217;m getting towards the end of it&#8230;</p>
<p>If you have the time to take a quick swiz (they&#8217;re both Code::Blocks projects) and let me know if I&#8217;ve got the right end of the stick or if I&#8217;ve got the wrong stick entirely (which is the feeling I&#8217;ve got) then it would be greatly appreciated &#8211; no worries if not tho, I know you&#8217;re a busy bloke!</p>
<p>/me hangs up his coding hat for the day&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: shetboy</title>
		<link>http://r3dux.org/2011/11/how-to-get-valid-integer-input-in-c-a-stupidly-long-solution-to-a-stupidly-simple-problem/#comment-7177</link>
		<dc:creator>shetboy</dc:creator>
		<pubDate>Wed, 16 Nov 2011 13:49:19 +0000</pubDate>
		<guid isPermaLink="false">http://r3dux.org/?p=5707#comment-7177</guid>
		<description>PS Love the name of the achievement</description>
		<content:encoded><![CDATA[<p>PS Love the name of the achievement</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: shetboy</title>
		<link>http://r3dux.org/2011/11/how-to-get-valid-integer-input-in-c-a-stupidly-long-solution-to-a-stupidly-simple-problem/#comment-7176</link>
		<dc:creator>shetboy</dc:creator>
		<pubDate>Wed, 16 Nov 2011 11:26:27 +0000</pubDate>
		<guid isPermaLink="false">http://r3dux.org/?p=5707#comment-7176</guid>
		<description>That&#039;s good  :)

However it is preferable when developing functions like this to not output anything, they should be like submarines... silent running.

The CALLING function should deal with IO, the LIBRARY function should deal with parsing the integer or whatever needs to be done.  &lt;a href=&quot;http://www.cplusplus.com/doc/tutorial/exceptions/&quot; rel=&quot;nofollow&quot;&gt;Exceptions&lt;/a&gt; should be used when things go wrong, then the calling function can do what is required.

After all, the calling function may need to do different things depending on its context.  The library function will have no idea what that could possibly be as it will be impossible to predict all scenarios.

You will then be able to use the function for command line utilities, Windows Framework applications, QT, etc, etc.</description>
		<content:encoded><![CDATA[<p>That&#8217;s good  <img src='http://r3dux.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>However it is preferable when developing functions like this to not output anything, they should be like submarines&#8230; silent running.</p>
<p>The CALLING function should deal with IO, the LIBRARY function should deal with parsing the integer or whatever needs to be done.  <a href="http://www.cplusplus.com/doc/tutorial/exceptions/">Exceptions</a> should be used when things go wrong, then the calling function can do what is required.</p>
<p>After all, the calling function may need to do different things depending on its context.  The library function will have no idea what that could possibly be as it will be impossible to predict all scenarios.</p>
<p>You will then be able to use the function for command line utilities, Windows Framework applications, QT, etc, etc.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: r3dux</title>
		<link>http://r3dux.org/2011/11/how-to-get-valid-integer-input-in-c-a-stupidly-long-solution-to-a-stupidly-simple-problem/#comment-7175</link>
		<dc:creator>r3dux</dc:creator>
		<pubDate>Wed, 16 Nov 2011 05:20:10 +0000</pubDate>
		<guid isPermaLink="false">http://r3dux.org/?p=5707#comment-7175</guid>
		<description>May as well refactor into a function:

&lt;pre lang=&quot;cpp&quot;&gt;
#include &lt;iostream&gt;
#include &lt;limits&gt;
using namespace std;
 
int getValidInt(int min, int max)
{
	int number;

	while ( (cout &lt;&lt; &quot;Enter a valid number: &quot; &amp;&amp; !(cin &gt;&gt; number) )  &#124;&#124; (number &lt; min) &#124;&#124; (number &gt; max) &#124;&#124; cin.peek() != &#039;\n&#039;)
	{
		cout &lt;&lt; &quot;You must input a whole number between &quot; &lt;&lt; min &lt;&lt; &quot; and &quot; &lt;&lt; max &lt;&lt; endl;
		cin.clear();
		cin.ignore(numeric_limits&lt;streamsize&gt;::max(), &#039;\n&#039;);
	}

	return number;
}

int main()
{
	int x = getValidInt(1, 100);
 
	cout &lt;&lt; &quot;Successfully got the valid integer value: &quot; &lt;&lt; x &lt;&lt; endl;
 
	return 0;
}&lt;/pre&gt;

Still lovin&#039; it =D</description>
		<content:encoded><![CDATA[<p>May as well refactor into a function:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #339900;">#include &lt;limits&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> getValidInt<span style="color: #008000;">&#40;</span><span style="color: #0000ff;">int</span> min, <span style="color: #0000ff;">int</span> max<span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">int</span> number<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Enter a valid number: &quot;</span> <span style="color: #000040;">&amp;&amp;</span> <span style="color: #000040;">!</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> number<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span>  <span style="color: #000040;">||</span> <span style="color: #008000;">&#40;</span>number <span style="color: #000080;">&lt;</span> min<span style="color: #008000;">&#41;</span> <span style="color: #000040;">||</span> <span style="color: #008000;">&#40;</span>number <span style="color: #000080;">&gt;</span> max<span style="color: #008000;">&#41;</span> <span style="color: #000040;">||</span> <span style="color: #0000dd;">cin</span>.<span style="color: #007788;">peek</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #FF0000;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #008000;">&#41;</span>
	<span style="color: #008000;">&#123;</span>
		<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;You must input a whole number between &quot;</span> <span style="color: #000080;">&lt;&lt;</span> min <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot; and &quot;</span> <span style="color: #000080;">&lt;&lt;</span> max <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
		<span style="color: #0000dd;">cin</span>.<span style="color: #007788;">clear</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
		<span style="color: #0000dd;">cin</span>.<span style="color: #007788;">ignore</span><span style="color: #008000;">&#40;</span>numeric_limits<span style="color: #000080;">&lt;</span>streamsize<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">max</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #FF0000;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #008000;">&#125;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> number<span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
	<span style="color: #0000ff;">int</span> x <span style="color: #000080;">=</span> getValidInt<span style="color: #008000;">&#40;</span><span style="color: #0000dd;">1</span>, <span style="color: #0000dd;">100</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Successfully got the valid integer value: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> x <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
	<span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Still lovin&#8217; it =D</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: r3dux</title>
		<link>http://r3dux.org/2011/11/how-to-get-valid-integer-input-in-c-a-stupidly-long-solution-to-a-stupidly-simple-problem/#comment-7173</link>
		<dc:creator>r3dux</dc:creator>
		<pubDate>Tue, 15 Nov 2011 21:03:22 +0000</pubDate>
		<guid isPermaLink="false">http://r3dux.org/?p=5707#comment-7173</guid>
		<description>That is absolutely perfect! You legend!!!

With the code above tho the guy messed up and added two cin.ignore() statements so you need to push enter after each failed entry, I just did a quick mod to fix and added a top validation limit, which gives us this:

&lt;pre lang=&quot;cpp&quot;&gt;
#include &lt;iostream&gt;
#include &lt;limits&gt;
using namespace std;

int main()
{
    const int MIN = 1;
    const int MAX = 100;
    int number;

    while ( (cout &lt;&lt; &quot;Enter a valid number: &quot; &amp;&amp; !(cin &gt;&gt; number) )  &#124;&#124; (number &lt; MIN) &#124;&#124; (number &gt; MAX) &#124;&#124; cin.peek() != &#039;\n&#039;)
    {
        cout &lt;&lt; &quot;You must input a whole number between 1 and 100!&quot; &lt;&lt; endl;
        cin.clear();
        cin.ignore(numeric_limits&lt;streamsize&gt;::max(), &#039;\n&#039;);
    }

    cout &lt;&lt; &quot;Successfully got the valid integer value: &quot; &lt;&lt; number &lt;&lt; endl;

    return 0;
}&lt;/pre&gt;

That works beautifully, and saves 130 lines of clumsiness! I think you&#039;ve unlocked an achievement!

&lt;img src=&quot;http://r3dux.org/wp-content/uploads/2011/11/King-of-the-Code.png&quot; alt=&quot;King of the Code achievement&quot; /&gt;

=D</description>
		<content:encoded><![CDATA[<p>That is absolutely perfect! You legend!!!</p>
<p>With the code above tho the guy messed up and added two cin.ignore() statements so you need to push enter after each failed entry, I just did a quick mod to fix and added a top validation limit, which gives us this:</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #339900;">#include &lt;iostream&gt;</span>
<span style="color: #339900;">#include &lt;limits&gt;</span>
<span style="color: #0000ff;">using</span> <span style="color: #0000ff;">namespace</span> std<span style="color: #008080;">;</span>
&nbsp;
<span style="color: #0000ff;">int</span> main<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> MIN <span style="color: #000080;">=</span> <span style="color: #0000dd;">1</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">const</span> <span style="color: #0000ff;">int</span> MAX <span style="color: #000080;">=</span> <span style="color: #0000dd;">100</span><span style="color: #008080;">;</span>
    <span style="color: #0000ff;">int</span> number<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Enter a valid number: &quot;</span> <span style="color: #000040;">&amp;&amp;</span> <span style="color: #000040;">!</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> number<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#41;</span>  <span style="color: #000040;">||</span> <span style="color: #008000;">&#40;</span>number <span style="color: #000080;">&lt;</span> MIN<span style="color: #008000;">&#41;</span> <span style="color: #000040;">||</span> <span style="color: #008000;">&#40;</span>number <span style="color: #000080;">&gt;</span> MAX<span style="color: #008000;">&#41;</span> <span style="color: #000040;">||</span> <span style="color: #0000dd;">cin</span>.<span style="color: #007788;">peek</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #FF0000;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">&#123;</span>
        <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;You must input a whole number between 1 and 100!&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
        <span style="color: #0000dd;">cin</span>.<span style="color: #007788;">clear</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
        <span style="color: #0000dd;">cin</span>.<span style="color: #007788;">ignore</span><span style="color: #008000;">&#40;</span>numeric_limits<span style="color: #000080;">&lt;</span>streamsize<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">max</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #FF0000;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
    <span style="color: #008000;">&#125;</span>
&nbsp;
    <span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Successfully got the valid integer value: &quot;</span> <span style="color: #000080;">&lt;&lt;</span> number <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>
&nbsp;
    <span style="color: #0000ff;">return</span> <span style="color: #0000dd;">0</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>That works beautifully, and saves 130 lines of clumsiness! I think you&#8217;ve unlocked an achievement!</p>
<p><img src="http://r3dux.org/wp-content/uploads/2011/11/King-of-the-Code.png" alt="King of the Code achievement" /></p>
<p>=D</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: shetboy</title>
		<link>http://r3dux.org/2011/11/how-to-get-valid-integer-input-in-c-a-stupidly-long-solution-to-a-stupidly-simple-problem/#comment-7170</link>
		<dc:creator>shetboy</dc:creator>
		<pubDate>Tue, 15 Nov 2011 16:23:26 +0000</pubDate>
		<guid isPermaLink="false">http://r3dux.org/?p=5707#comment-7170</guid>
		<description>Just spotted on &lt;a href=&quot;http://www.gamedev.net/topic/213066-c-verify-user-inputs-a-number/&quot; rel=&quot;nofollow&quot;&gt;this page&lt;/a&gt;...

&lt;pre lang=&quot;cpp&quot;&gt;
while (cout &lt;&lt; &quot;Enter Quantity: &quot; &amp;&amp; !(cin &gt;&gt; Quantity)  &#124;&#124; (Quantity &lt; 1) &#124;&#124; cin.peek() != &#039;\n&#039;)
{    
	cout &lt;&lt; &quot;Quantity must be  whole positive number!&quot; &lt;&lt; endl;    
	cin.clear();
	cin.ignore();
	cin.ignore(numeric_limits&lt;streamsize&gt;::max(), &#039;\n&#039;);
}
&lt;/pre&gt;

Apologies if I messed up the markup code  :)

(&lt;strong&gt;r3dux&lt;/strong&gt;: All good! It&#039;s &quot;cpp&quot; for C++, list of valid languages for highlighting plus examples can be found &lt;a href=&quot;http://wordpress.org/extend/plugins/wp-syntax/other_notes/&quot; rel=&quot;nofollow&quot;&gt;here&lt;/a&gt;)</description>
		<content:encoded><![CDATA[<p>Just spotted on <a href="http://www.gamedev.net/topic/213066-c-verify-user-inputs-a-number/">this page</a>&#8230;</p>

<div class="wp_syntax"><div class="code"><pre class="cpp" style="font-family:monospace;"><span style="color: #0000ff;">while</span> <span style="color: #008000;">&#40;</span><span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Enter Quantity: &quot;</span> <span style="color: #000040;">&amp;&amp;</span> <span style="color: #000040;">!</span><span style="color: #008000;">&#40;</span><span style="color: #0000dd;">cin</span> <span style="color: #000080;">&gt;&gt;</span> Quantity<span style="color: #008000;">&#41;</span>  <span style="color: #000040;">||</span> <span style="color: #008000;">&#40;</span>Quantity <span style="color: #000080;">&lt;</span> <span style="color: #0000dd;">1</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">||</span> <span style="color: #0000dd;">cin</span>.<span style="color: #007788;">peek</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #000040;">!</span><span style="color: #000080;">=</span> <span style="color: #FF0000;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #008000;">&#41;</span>
<span style="color: #008000;">&#123;</span>    
	<span style="color: #0000dd;">cout</span> <span style="color: #000080;">&lt;&lt;</span> <span style="color: #FF0000;">&quot;Quantity must be  whole positive number!&quot;</span> <span style="color: #000080;">&lt;&lt;</span> endl<span style="color: #008080;">;</span>    
	<span style="color: #0000dd;">cin</span>.<span style="color: #007788;">clear</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cin</span>.<span style="color: #007788;">ignore</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
	<span style="color: #0000dd;">cin</span>.<span style="color: #007788;">ignore</span><span style="color: #008000;">&#40;</span>numeric_limits<span style="color: #000080;">&lt;</span>streamsize<span style="color: #000080;">&gt;</span><span style="color: #008080;">::</span><span style="color: #007788;">max</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>, <span style="color: #FF0000;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: #008000;">&#41;</span><span style="color: #008080;">;</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Apologies if I messed up the markup code  <img src='http://r3dux.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>(<strong>r3dux</strong>: All good! It&#8217;s &#8220;cpp&#8221; for C++, list of valid languages for highlighting plus examples can be found <a href="http://wordpress.org/extend/plugins/wp-syntax/other_notes/">here</a>)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: shetboy</title>
		<link>http://r3dux.org/2011/11/how-to-get-valid-integer-input-in-c-a-stupidly-long-solution-to-a-stupidly-simple-problem/#comment-7166</link>
		<dc:creator>shetboy</dc:creator>
		<pubDate>Tue, 15 Nov 2011 09:42:44 +0000</pubDate>
		<guid isPermaLink="false">http://r3dux.org/?p=5707#comment-7166</guid>
		<description>Gosh, that is indeed clumsy.  Can you use pre-made objects like in Java and then catch the exception?  This will also work for -ve numbers, etc.

&lt;pre lang=&quot;java&quot;&gt;
public int stringToInt(String enteredValue) throws NumberFormatException {
	return Integer.parseInt(enteredValue);
}
	
public float stringToFloat(String enteredValue) throws NumberFormatException {
	return Float.parseFloat(enteredValue);
}
&lt;/pre&gt;</description>
		<content:encoded><![CDATA[<p>Gosh, that is indeed clumsy.  Can you use pre-made objects like in Java and then catch the exception?  This will also work for -ve numbers, etc.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> stringToInt<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> enteredValue<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NumberFormatException</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #003399;">Integer</span>.<span style="color: #006633;">parseInt</span><span style="color: #009900;">&#40;</span>enteredValue<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">float</span> stringToFloat<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> enteredValue<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> <span style="color: #003399;">NumberFormatException</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">return</span> <span style="color: #003399;">Float</span>.<span style="color: #006633;">parseFloat</span><span style="color: #009900;">&#40;</span>enteredValue<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
	</item>
</channel>
</rss>

