<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[do() while variabeln überprüfung]]></title><description><![CDATA[<p>hi</p>
<p>ich habe einen input per cin. diesen will ich überprüfen ob er eine positive zahl ist, und keine negative, null oder alphabetisch.</p>
<p>dies mache ich so:</p>
<pre><code class="language-cpp">cout &lt;&lt; &quot;Enter T, the temperture for which Cv is to be calculated\n&quot;;
do{cin &gt;&gt; T;}while(!checkinput(&quot;neg&quot;,T));

/////////

int checkinput(string type, float value){
	//check if value is negative (if a string eg 'hello' is entered, the value becoms negative too)
	if(type==&quot;neg&quot; &amp;&amp; value&lt;=0){
	    cout &lt;&lt; &quot;ERROR: invalid input. Try again\n&quot;;
        return 0;
	}else if(type==&quot;gas&quot; &amp;&amp; (value &lt; 8.31 || value &gt; 8.32)){
	    cout &lt;&lt; &quot;ERROR: invalid input. Try again\n&quot;;
	    return 0;
	}else{
	   return 1;
	}

}
</code></pre>
<p>das funktioniert auch super solange man zahlen eingibt. auch bei eingabe 0 (=null) geht es gut. sobald ich aber einen buchstaben eingebe, kommt er in eine schlaufe rein. vielleicht noch zu sagen, T ist ein float.<br />
an was kann das liegen?</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/200029/do-while-variabeln-überprüfung</link><generator>RSS for Node</generator><lastBuildDate>Sat, 03 Oct 2026 22:08:15 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/200029.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 09 Dec 2007 21:16:02 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to do() while variabeln überprüfung on Sun, 09 Dec 2007 21:16:02 GMT]]></title><description><![CDATA[<p>hi</p>
<p>ich habe einen input per cin. diesen will ich überprüfen ob er eine positive zahl ist, und keine negative, null oder alphabetisch.</p>
<p>dies mache ich so:</p>
<pre><code class="language-cpp">cout &lt;&lt; &quot;Enter T, the temperture for which Cv is to be calculated\n&quot;;
do{cin &gt;&gt; T;}while(!checkinput(&quot;neg&quot;,T));

/////////

int checkinput(string type, float value){
	//check if value is negative (if a string eg 'hello' is entered, the value becoms negative too)
	if(type==&quot;neg&quot; &amp;&amp; value&lt;=0){
	    cout &lt;&lt; &quot;ERROR: invalid input. Try again\n&quot;;
        return 0;
	}else if(type==&quot;gas&quot; &amp;&amp; (value &lt; 8.31 || value &gt; 8.32)){
	    cout &lt;&lt; &quot;ERROR: invalid input. Try again\n&quot;;
	    return 0;
	}else{
	   return 1;
	}

}
</code></pre>
<p>das funktioniert auch super solange man zahlen eingibt. auch bei eingabe 0 (=null) geht es gut. sobald ich aber einen buchstaben eingebe, kommt er in eine schlaufe rein. vielleicht noch zu sagen, T ist ein float.<br />
an was kann das liegen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1417830</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1417830</guid><dc:creator><![CDATA[fabiopigi]]></dc:creator><pubDate>Sun, 09 Dec 2007 21:16:02 GMT</pubDate></item><item><title><![CDATA[Reply to do() while variabeln überprüfung on Sun, 09 Dec 2007 21:22:23 GMT]]></title><description><![CDATA[<p>Du musst einfach diesen Fall abfangen:</p>
<pre><code class="language-cpp">std::istream&amp; clear_stream(std::istream&amp; in)
{ in.clear(); in.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(), '\n'); return in; }

// ...
do { std::cout &lt;&lt; Enter T, the temperature for which Cv is to be calculated&quot; &lt;&lt; std::endl; } while (!(clear_stream &gt;&gt; T) || valid_input(&quot;neg&quot;, T) == false);

// ...
bool valid_input(std::string const&amp; type, float value)
{ return !((type == &quot;neg&quot; &amp;&amp; value &lt;= 0.0f) || (type == &quot;gas&quot; &amp;&amp; (value &lt; 8.31f || value &gt; 8.32f))); }
</code></pre>
<p>Das wichtige ist, dass du guckst ob nen fail/bad-bit im Eingabestream gesetzt ist und das du gegebenen Falls da wieder aufräumst (clear_stream).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1417834</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1417834</guid><dc:creator><![CDATA[*D*Evil]]></dc:creator><pubDate>Sun, 09 Dec 2007 21:22:23 GMT</pubDate></item><item><title><![CDATA[Reply to do() while variabeln überprüfung on Sun, 09 Dec 2007 21:23:40 GMT]]></title><description><![CDATA[<p><a href="http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.3" rel="nofollow">http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.3</a><br />
Übrigens kannst du hier eine einfache while-Schleife statt do-while benutzen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1417835</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1417835</guid><dc:creator><![CDATA[Freundlich]]></dc:creator><pubDate>Sun, 09 Dec 2007 21:23:40 GMT</pubDate></item></channel></rss>