<?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[cin &amp;gt;&amp;gt; falsche Eingabe verhindern]]></title><description><![CDATA[<p>Mittels cin &gt;&gt; intvar; eine Zahl eingeben, Eingabefehler müssen verhindert werden.<br />
So kann es nicht sein, wenn 44e43 eigegeben wurde, dass in intvar dann 44 steht.<br />
Um solche Eingabefehler abzufangen, habe ich folgendes geschrieben:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt; 
using namespace std;

 int main()
 {
     int myint;
     char c;

     cin &gt;&gt; myint;
     while (cin.fail())
      {
         cin.clear();
         cin &gt;&gt; c;
         cin &gt;&gt; myint;
         cout&lt;&lt;&quot;falsche eingabe - bitte noch einmal&quot; &lt;&lt; endl;
         cin &gt;&gt; myint;
      }

     cin.get(c);  

     while (c != '\n')
     {
           cout&lt;&lt;&quot;falsche eingabe - bitte noch einmal&quot; &lt;&lt; endl;
           while (c != '\n') cin.get(c);  
           cin &gt;&gt; myint;
           while (cin.fail())
           {
                cin.clear();
                cin &gt;&gt; c;
                cin &gt;&gt; myint;
                cout&lt;&lt;&quot;falsche eingabe - bitte noch einmal&quot; &lt;&lt; endl;
                cin &gt;&gt; myint;
           }

           cin.get(c); 
        }
      return 0;
 }
</code></pre>
<p>Lässt sich das nicht kürzer schreiben ? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/topic/166184/cin-gt-gt-falsche-eingabe-verhindern</link><generator>RSS for Node</generator><lastBuildDate>Tue, 15 Sep 2026 05:13:04 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/166184.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 27 Nov 2006 05:13:19 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to cin &amp;gt;&amp;gt; falsche Eingabe verhindern on Mon, 27 Nov 2006 05:13:19 GMT]]></title><description><![CDATA[<p>Mittels cin &gt;&gt; intvar; eine Zahl eingeben, Eingabefehler müssen verhindert werden.<br />
So kann es nicht sein, wenn 44e43 eigegeben wurde, dass in intvar dann 44 steht.<br />
Um solche Eingabefehler abzufangen, habe ich folgendes geschrieben:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt; 
using namespace std;

 int main()
 {
     int myint;
     char c;

     cin &gt;&gt; myint;
     while (cin.fail())
      {
         cin.clear();
         cin &gt;&gt; c;
         cin &gt;&gt; myint;
         cout&lt;&lt;&quot;falsche eingabe - bitte noch einmal&quot; &lt;&lt; endl;
         cin &gt;&gt; myint;
      }

     cin.get(c);  

     while (c != '\n')
     {
           cout&lt;&lt;&quot;falsche eingabe - bitte noch einmal&quot; &lt;&lt; endl;
           while (c != '\n') cin.get(c);  
           cin &gt;&gt; myint;
           while (cin.fail())
           {
                cin.clear();
                cin &gt;&gt; c;
                cin &gt;&gt; myint;
                cout&lt;&lt;&quot;falsche eingabe - bitte noch einmal&quot; &lt;&lt; endl;
                cin &gt;&gt; myint;
           }

           cin.get(c); 
        }
      return 0;
 }
</code></pre>
<p>Lässt sich das nicht kürzer schreiben ? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f615.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--confused_face"
      title=":confused:"
      alt="😕"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1182004</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1182004</guid><dc:creator><![CDATA[cincin]]></dc:creator><pubDate>Mon, 27 Nov 2006 05:13:19 GMT</pubDate></item><item><title><![CDATA[Reply to cin &amp;gt;&amp;gt; falsche Eingabe verhindern on Mon, 27 Nov 2006 07:12:23 GMT]]></title><description><![CDATA[<p>Erstens: Das funktioniert? (wenn du den Rest der Zeile ignorieren willst, nimm lieber con.ignore())</p>
<p>Zweitens: Ein istream interpretiert die Eingabe so weit, wie sie passt, d.h. wenn &quot;44e43&quot; als int eingelesen werden soll, bleibt er am 'e' hängen (und erkennt den Abschnitt davor als Zahl). Wenn du so etwas abfangen willst, kannst du entweder per cin.get() überprüfen, was direkt nach der Eingabe kommt oder du liest eine komplette Zeile per getline() ein und wandelst sie per Hand in eine Zahl um (siehe FAQ).</p>
<p>Drittens: Doppelt auftretender Code schreit förmlich danach, in eine Funktion ausgelagert zu werden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1182013</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1182013</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Mon, 27 Nov 2006 07:12:23 GMT</pubDate></item></channel></rss>