<?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[Warum geht else nicht?]]></title><description><![CDATA[<p>Hi, wenn man nen buchstaben eingibt, muesste doch else stattfinden tut es aber nicht. Weiss jemand wieso?</p>
<p>#include &lt;iostream&gt;</p>
<p>using namespace std;</p>
<p>int main ()</p>
<p>{<br />
const int tempolimit = 100;<br />
int Geschwindigkeit = 0;</p>
<p>cout &lt;&lt; &quot;Hier sind &quot; &lt;&lt; tempolimit &lt;&lt; &quot;km/h erlaubt\n&quot;;<br />
cout &lt;&lt; &quot;Wie schnell sind Sie gefahren?\n&quot;;<br />
cin &gt;&gt; Geschwindigkeit;</p>
<p>if (Geschwindigkeit == tempolimit)<br />
{ cout &lt;&lt; &quot;Nochmal Glueck gehabt, Junge..&quot;;<br />
}<br />
else if (Geschwindigkeit &lt;= tempolimit)<br />
{ cout &lt;&lt; &quot;Du bist &quot; &lt;&lt; tempolimit - Geschwindigkeit&lt;&lt; &quot; km/h unter dem Tempolimit gefahren. Beruhig dich Junge..&quot; &lt;&lt; endl;<br />
}</p>
<p>else if (Geschwindigkeit &gt;= tempolimit)<br />
{<br />
cout &lt;&lt; &quot;Du bist &quot; &lt;&lt; Geschwindigkeit - tempolimit&lt;&lt; &quot; km/h zu schnell gefahren. Das gibt maechtig Probleme, Junge..&quot;;<br />
}</p>
<p>else<br />
{<br />
cout &lt;&lt; &quot;Ungueltige Eingabe&quot;;<br />
}</p>
<p>return 0;</p>
<p>}</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/319664/warum-geht-else-nicht</link><generator>RSS for Node</generator><lastBuildDate>Sat, 25 Jul 2026 06:34:33 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/319664.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 27 Aug 2013 21:59:42 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Warum geht else nicht? on Tue, 27 Aug 2013 21:59:42 GMT]]></title><description><![CDATA[<p>Hi, wenn man nen buchstaben eingibt, muesste doch else stattfinden tut es aber nicht. Weiss jemand wieso?</p>
<p>#include &lt;iostream&gt;</p>
<p>using namespace std;</p>
<p>int main ()</p>
<p>{<br />
const int tempolimit = 100;<br />
int Geschwindigkeit = 0;</p>
<p>cout &lt;&lt; &quot;Hier sind &quot; &lt;&lt; tempolimit &lt;&lt; &quot;km/h erlaubt\n&quot;;<br />
cout &lt;&lt; &quot;Wie schnell sind Sie gefahren?\n&quot;;<br />
cin &gt;&gt; Geschwindigkeit;</p>
<p>if (Geschwindigkeit == tempolimit)<br />
{ cout &lt;&lt; &quot;Nochmal Glueck gehabt, Junge..&quot;;<br />
}<br />
else if (Geschwindigkeit &lt;= tempolimit)<br />
{ cout &lt;&lt; &quot;Du bist &quot; &lt;&lt; tempolimit - Geschwindigkeit&lt;&lt; &quot; km/h unter dem Tempolimit gefahren. Beruhig dich Junge..&quot; &lt;&lt; endl;<br />
}</p>
<p>else if (Geschwindigkeit &gt;= tempolimit)<br />
{<br />
cout &lt;&lt; &quot;Du bist &quot; &lt;&lt; Geschwindigkeit - tempolimit&lt;&lt; &quot; km/h zu schnell gefahren. Das gibt maechtig Probleme, Junge..&quot;;<br />
}</p>
<p>else<br />
{<br />
cout &lt;&lt; &quot;Ungueltige Eingabe&quot;;<br />
}</p>
<p>return 0;</p>
<p>}</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2348918</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2348918</guid><dc:creator><![CDATA[kamelkacke]]></dc:creator><pubDate>Tue, 27 Aug 2013 21:59:42 GMT</pubDate></item><item><title><![CDATA[Reply to Warum geht else nicht? on Tue, 27 Aug 2013 22:02:46 GMT]]></title><description><![CDATA[<p>Buchstaben, also chars, können implizit nach int konvertiert werden. Jeder Buchstabe hat einen bestimmten Wert, den du aus der ASCII-Tabelle entnehmen kannst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2348920</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2348920</guid><dc:creator><![CDATA[sdfgfds]]></dc:creator><pubDate>Tue, 27 Aug 2013 22:02:46 GMT</pubDate></item><item><title><![CDATA[Reply to Warum geht else nicht? on Tue, 27 Aug 2013 22:21:03 GMT]]></title><description><![CDATA[<p>sdfgfds schrieb:</p>
<blockquote>
<p>Buchstaben, also chars, können implizit nach int konvertiert werden. Jeder Buchstabe hat einen bestimmten Wert, den du aus der ASCII-Tabelle entnehmen kannst.</p>
</blockquote>
<p>Nein, das Problem ist ein anderes:</p>
<pre><code>cin &gt;&gt; Geschwindigkeit;
</code></pre>
<p>Hier wird versucht einen int einzulesen. Das kann klappen oder nicht, das wird hier nicht geprüft. Wenn es nicht klappt, dann ist Geschwindigkeit aber immer noch irgendein int mit irgendeinem Wert. Da die ersten drei if-Bedingungen die Fälle Gleichheit, kleiner und größer abdecken<sup>*</sup>, muss ja irgendetwas davon eintreffen. Daher kommt der letzte else-Fall niemals dran.<br />
Richtig wäre, den Erfolg der Eingabe zu prüfen, zum Beispiel:</p>
<pre><code>if (cin &gt;&gt; Geschwindigkeit)
{
  // .. restlicher Code
}
else
{
  cout &lt;&lt; &quot;Ungueltige Eingabe&quot;;
}
</code></pre>
<p>An den Threadersteller: In Zukunft bitte Codetags benutzen (unter dem Editorfenster, bei den Smileys) und nur ein Thema pro Frage erstellen.</p>
<p><sup>*</sup>: Unnötigerweise sogar kleier<strong>gleich</strong> und größer<strong>gleich</strong>, was gar nicht mehr sein kann.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2348924</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2348924</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Tue, 27 Aug 2013 22:21:03 GMT</pubDate></item><item><title><![CDATA[Reply to Warum geht else nicht? on Tue, 27 Aug 2013 22:21:06 GMT]]></title><description><![CDATA[<p>Wenn du einen Buchstaben eingibst, wird</p>
<pre><code class="language-cpp">cin &gt;&gt; Geschwindigkeit;
</code></pre>
<p>fehlschlagen (und einen Fehlerzustand in cin setzen; darauf kommen wir gleich zurück). Die Variable Geschwindigkeit bleibt dabei unverändert (hat also nachher immer noch den Wert 0).</p>
<p>Den Fehlercode in cin kannst du abfragen; das sieht dann so aus:</p>
<pre><code class="language-cpp">cin &gt;&gt; Geschwindigkeit;

if(!cin) {
  cout &lt;&lt; &quot;Ungueltige Eingabe&quot;;
} else if (Geschwindigkeit == tempolimit) {
  cout &lt;&lt; &quot;Nochmal Glueck gehabt, Junge..&quot;;
} else if (Geschwindigkeit &lt;= tempolimit) {
  cout &lt;&lt; &quot;Du bist &quot; &lt;&lt; tempolimit - Geschwindigkeit&lt;&lt; &quot; km/h unter dem Tempolimit gefahren. Beruhig dich Junge..&quot; &lt;&lt; endl;
} else if (Geschwindigkeit &gt;= tempolimit) {
  cout &lt;&lt; &quot;Du bist &quot; &lt;&lt; Geschwindigkeit - tempolimit&lt;&lt; &quot; km/h zu schnell gefahren. Das gibt maechtig Probleme, Junge..&quot;;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2348925</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2348925</guid><dc:creator><![CDATA[seldon]]></dc:creator><pubDate>Tue, 27 Aug 2013 22:21:06 GMT</pubDate></item><item><title><![CDATA[Reply to Warum geht else nicht? on Wed, 28 Aug 2013 00:10:08 GMT]]></title><description><![CDATA[<p>Danke fuer die Antworten.<br />
Gibt es einen befehl fuer buchstaben und zahlen generell? Nach dem motto if (zifferneingabe..) if a-z geht schonmal nicht .. :p</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2348926</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2348926</guid><dc:creator><![CDATA[kamelkacke]]></dc:creator><pubDate>Wed, 28 Aug 2013 00:10:08 GMT</pubDate></item><item><title><![CDATA[Reply to Warum geht else nicht? on Wed, 28 Aug 2013 00:14:58 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<p>Wenn es nicht klappt, dann ist Geschwindigkeit aber immer noch irgendein int mit irgendeinem Wert.</p>
</blockquote>
<p>Wenn ich mich recht entsinne, garantiert der Standard doch, dass der Wert der Variablen bei einem Fehler unverändert bleibt. Dann hat Geschwindigkeit also nicht irgendeinen Wert, sondern 0.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2348936</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2348936</guid><dc:creator><![CDATA[out]]></dc:creator><pubDate>Wed, 28 Aug 2013 00:14:58 GMT</pubDate></item><item><title><![CDATA[Reply to Warum geht else nicht? on Wed, 28 Aug 2013 05:45:48 GMT]]></title><description><![CDATA[<p>out schrieb:</p>
<blockquote>
<p>SeppJ schrieb:</p>
<blockquote>
<p>Wenn es nicht klappt, dann ist Geschwindigkeit aber immer noch irgendein int mit irgendeinem Wert.</p>
</blockquote>
<p>Wenn ich mich recht entsinne, garantiert der Standard doch, dass der Wert der Variablen bei einem Fehler unverändert bleibt. Dann hat Geschwindigkeit also nicht irgendeinen Wert, sondern 0.</p>
</blockquote>
<p>Ja, seit <strong>C++11</strong> schon. Vorher war der Wert beliebig.</p>
<p>§22.4.2.1.2/3 schrieb:</p>
<blockquote>
<p>The numeric value to be stored can be one of:<br />
— zero, if the conversion function fails to convert the entire field. ios_base::failbit is assigned to err.<br />
— the most positive representable value, if the field represents a value too large positive to be represented in val. ios_base::failbit is assigned to err.<br />
— the most negative representable value or zero for an unsigned integer type, if the field represents a value too large negative to be represented in val. ios_base::failbit is assigned to err.<br />
— the converted value, otherwise.<br />
The resultant numeric value is stored in val.</p>
</blockquote>
<p>Das finde ich ganz nett.</p>
<blockquote>
<p>Gibt es einen befehl fuer buchstaben und zahlen generell?</p>
</blockquote>
<p><a href="http://en.cppreference.com/w/cpp/io/basic_istream/get" rel="nofollow"> <code>get</code> </a>extrahiert ein einzelnes Zeichen aus dem Stream.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2348941</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2348941</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Wed, 28 Aug 2013 05:45:48 GMT</pubDate></item><item><title><![CDATA[Reply to Warum geht else nicht? on Wed, 28 Aug 2013 06:26:28 GMT]]></title><description><![CDATA[<p>kamelkacke schrieb:</p>
<blockquote>
<p>Danke fuer die Antworten.<br />
Gibt es einen befehl fuer buchstaben und zahlen generell? Nach dem motto if (zifferneingabe..) if a-z geht schonmal nicht .. :p</p>
</blockquote>
<p>Du kannst prüfen ob ein Zeichen eine Zahl oder ein Buchstabe oder ... ist. Dafür gibst isalpha, isalnum, isdigit,...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2348944</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2348944</guid><dc:creator><![CDATA[Nathan]]></dc:creator><pubDate>Wed, 28 Aug 2013 06:26:28 GMT</pubDate></item><item><title><![CDATA[Reply to Warum geht else nicht? on Wed, 28 Aug 2013 15:02:31 GMT]]></title><description><![CDATA[<p>Sone schrieb:</p>
<blockquote>
<p>out schrieb:</p>
<blockquote>
<p>SeppJ schrieb:</p>
<blockquote>
<p>Wenn es nicht klappt, dann ist Geschwindigkeit aber immer noch irgendein int mit irgendeinem Wert.</p>
</blockquote>
<p>Wenn ich mich recht entsinne, garantiert der Standard doch, dass der Wert der Variablen bei einem Fehler unverändert bleibt. Dann hat Geschwindigkeit also nicht irgendeinen Wert, sondern 0.</p>
</blockquote>
<p>Ja, seit <strong>C++11</strong> schon. Vorher war der Wert beliebig.</p>
<p>§22.4.2.1.2/3 schrieb:</p>
<blockquote>
<p>The numeric value to be stored can be one of:<br />
— zero, if the conversion function fails to convert the entire field. ios_base::failbit is assigned to err.<br />
— the most positive representable value, if the field represents a value too large positive to be represented in val. ios_base::failbit is assigned to err.<br />
— the most negative representable value or zero for an unsigned integer type, if the field represents a value too large negative to be represented in val. ios_base::failbit is assigned to err.<br />
— the converted value, otherwise.<br />
The resultant numeric value is stored in val.</p>
</blockquote>
<p>Das finde ich ganz nett.</p>
</blockquote>
<p>Wo liest du da raus dass der Wert unverändert bleiben soll? Da steht explizit <strong>zero</strong>.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2349039</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2349039</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Wed, 28 Aug 2013 15:02:31 GMT</pubDate></item><item><title><![CDATA[Reply to Warum geht else nicht? on Wed, 28 Aug 2013 15:20:50 GMT]]></title><description><![CDATA[<blockquote>
<p>Wo liest du da raus dass der Wert unverändert bleiben soll? Da steht explizit zero.</p>
</blockquote>
<p><strong>Verdammt, da habe ich outs Post nicht richtig gelesen.</strong> Ich habe eigentlich nur bejahen wollen, dass (seit C++11) der Wert 0 ist, falls nichts extrahiert werden konnte. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f623.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--persevering_face"
      title="&gt;.&lt;"
      alt="😣"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2349045</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2349045</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Wed, 28 Aug 2013 15:20:50 GMT</pubDate></item></channel></rss>