<?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[Schleifenproblem cin.fail()]]></title><description><![CDATA[<p>Hi,<br />
ich wollte eine Ueberpruefung mit cin.fail machen um zu testen ob der Benutzer eine Zahl oder Zeichen eingegeben hat und falls er keine Zahl eingegeben hat, soll dass Menue noch einmal angezeigt werden mit der Nachricht, dass er eine Zahl eingeben soll.</p>
<p>Mein Problem ist allerdings, dass wenn ich diesen Code ausfuehre und einen Buchstaben eingebe, die while-Schleife sich endlos wiederholt ohne dass ich darauf Einfluss nehmen kann.</p>
<pre><code class="language-cpp">do {
		do {
			system(&quot;clear&quot;);

			if( cin.fail( ) ) {
				cout &lt;&lt; &quot;Bitte geben Sie eine Zahl ein!&quot;;
				cin.clear( );
				cin.ignore( cin.rdbuf( )-&gt;in_avail( ) );
            		} 

			cout &lt;&lt; &quot;  \n\t\t\tSchul-Datenbank\n&quot;;
			cout &lt;&lt; &quot;  \t\t===============================\n\n&quot;;
			cout &lt;&lt; &quot;  Menue:\n\n&quot;;
			cout &lt;&lt; &quot;  1 - Schueler\n&quot;;
			cout &lt;&lt; &quot;  2 - Lehrer\n&quot;;
			cout &lt;&lt; &quot;  3 - Unterricht\n&quot;;
			cout &lt;&lt; &quot;  0 - Exit\n\n&quot;;

			cout &lt;&lt; &quot;  Bitte Waehlen Sie einen Menuepunkt: &quot;;
			cin &gt;&gt; menue;
		}
		while( cin.fail() );

	}
	while( menue != 0 &amp;&amp; menue != 1 &amp;&amp; menue != 2 &amp;&amp; menue != 3);
</code></pre>
<p>Rgds<br />
Coda</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/199398/schleifenproblem-cin-fail</link><generator>RSS for Node</generator><lastBuildDate>Sat, 03 Oct 2026 16:26:22 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/199398.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 02 Dec 2007 17:03:34 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Schleifenproblem cin.fail() on Sun, 02 Dec 2007 17:03:34 GMT]]></title><description><![CDATA[<p>Hi,<br />
ich wollte eine Ueberpruefung mit cin.fail machen um zu testen ob der Benutzer eine Zahl oder Zeichen eingegeben hat und falls er keine Zahl eingegeben hat, soll dass Menue noch einmal angezeigt werden mit der Nachricht, dass er eine Zahl eingeben soll.</p>
<p>Mein Problem ist allerdings, dass wenn ich diesen Code ausfuehre und einen Buchstaben eingebe, die while-Schleife sich endlos wiederholt ohne dass ich darauf Einfluss nehmen kann.</p>
<pre><code class="language-cpp">do {
		do {
			system(&quot;clear&quot;);

			if( cin.fail( ) ) {
				cout &lt;&lt; &quot;Bitte geben Sie eine Zahl ein!&quot;;
				cin.clear( );
				cin.ignore( cin.rdbuf( )-&gt;in_avail( ) );
            		} 

			cout &lt;&lt; &quot;  \n\t\t\tSchul-Datenbank\n&quot;;
			cout &lt;&lt; &quot;  \t\t===============================\n\n&quot;;
			cout &lt;&lt; &quot;  Menue:\n\n&quot;;
			cout &lt;&lt; &quot;  1 - Schueler\n&quot;;
			cout &lt;&lt; &quot;  2 - Lehrer\n&quot;;
			cout &lt;&lt; &quot;  3 - Unterricht\n&quot;;
			cout &lt;&lt; &quot;  0 - Exit\n\n&quot;;

			cout &lt;&lt; &quot;  Bitte Waehlen Sie einen Menuepunkt: &quot;;
			cin &gt;&gt; menue;
		}
		while( cin.fail() );

	}
	while( menue != 0 &amp;&amp; menue != 1 &amp;&amp; menue != 2 &amp;&amp; menue != 3);
</code></pre>
<p>Rgds<br />
Coda</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1413875</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1413875</guid><dc:creator><![CDATA[Coda*linux.PrayLoad]]></dc:creator><pubDate>Sun, 02 Dec 2007 17:03:34 GMT</pubDate></item><item><title><![CDATA[Reply to Schleifenproblem cin.fail() on Sun, 02 Dec 2007 17:45:34 GMT]]></title><description><![CDATA[<p>Hmm nochmal damit es nun alle kapieren:</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; }
</code></pre>
<pre><code class="language-cpp">do {
    std::system(&quot;cls&quot;);

    std::cout &lt;&lt; &quot;  \n\t\t\tSchul-Datenbank\n&quot;
              &lt;&lt; &quot;  \t\t===============================\n\n&quot;
              &lt;&lt; &quot;  Menue:\n\n&quot;
              &lt;&lt; &quot;  1 - Schueler\n&quot;
              &lt;&lt; &quot;  2 - Lehrer\n&quot;
              &lt;&lt; &quot;  3 - Unterricht\n&quot;
              &lt;&lt; &quot;  0 - Exit\n&quot; &lt;&lt; std::endl;

    std::cout &lt;&lt; &quot;Auswahl: &quot;;
} while(!(clear_stream(std::cin) &gt;&gt; menu) || menu &gt; 3 || menu &lt; 0);
</code></pre>
<p>Und fertig.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1413891</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1413891</guid><dc:creator><![CDATA[*D*Evil]]></dc:creator><pubDate>Sun, 02 Dec 2007 17:45:34 GMT</pubDate></item><item><title><![CDATA[Reply to Schleifenproblem cin.fail() on Mon, 03 Dec 2007 13:42:39 GMT]]></title><description><![CDATA[<p>Eigetl. sehr gut, nur wird die Erste Eingaben die getaetigt wird immer ignoriert.</p>
<p>Also, wenn ich in das Menue komme und z.B. 1 eingebe, geht er in die nachste Zeile und wartet auf eine erneute Eingabe, obwhol 1 ja korrekt ist. Danach funktioniert es perfekt, nur halt beim 1. mal ignoriert er alles was ich eingebe.</p>
<p>Rgds<br />
Coda</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1414231</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1414231</guid><dc:creator><![CDATA[Coda*linux.PrayLoad]]></dc:creator><pubDate>Mon, 03 Dec 2007 13:42:39 GMT</pubDate></item></channel></rss>