<?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[Frage zu &amp;quot;else if&amp;quot; und der Codeformatierung]]></title><description><![CDATA[<p>Hi Leute!</p>
<p>Ich hab diesen Code geschrieben. Er berechnet einem zu unterschiedlichen Anzahlen von CD's den zu bewilligen (fiktiven) Rabatt. Meine Frage ist nun, wie ich die Codeformatierung hier noch besser machen kann?</p>
<pre><code class="language-cpp">int main()
{

    cout &lt;&lt; &quot;Anzahl der zu Kaufenden DVD-Rohlinge eingeben: &quot;;
    cin &gt;&gt; anzahl;

    if((anzahl &gt;= 0) &amp;&amp; (anzahl &lt;= 9))
    {
     cout &lt;&lt; &quot;Verkaufspreis der &quot; &lt;&lt; anzahl &lt;&lt; &quot; DVD's: &quot; &lt;&lt; anzahl * 0.8 &lt;&lt; endl; 
    }

    else if((anzahl &gt;= 10) &amp;&amp; (anzahl &lt;= 49))
            {
             cout &lt;&lt; &quot;Verkaufspreis der &quot; &lt;&lt; anzahl &lt;&lt; &quot; DVD's: &quot; &lt;&lt; (float)(((anzahl * 80 * 97)/10+5)/10)/100 &lt;&lt; endl;
            }

    else if((anzahl &gt;= 50) &amp;&amp; (anzahl &lt;= 99))
    {
     cout &lt;&lt; &quot;Verkaufspreis der &quot; &lt;&lt; anzahl &lt;&lt; &quot; DVD's: &quot; &lt;&lt; (float)(((anzahl * 80 * 95)/10+5)/10)/100 &lt;&lt; endl;
    }

     else if(anzahl &gt;= 100)
     {
      cout &lt;&lt; &quot;Verkaufspreis der &quot; &lt;&lt; anzahl &lt;&lt; &quot; DVD's: &quot; &lt;&lt; (float)(((anzahl * 80 * 92)/10+5)/10)/100 &lt;&lt; endl;
     }
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/277256/frage-zu-quot-else-if-quot-und-der-codeformatierung</link><generator>RSS for Node</generator><lastBuildDate>Wed, 26 Aug 2026 01:43:42 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/277256.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 15 Nov 2010 16:23:27 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Frage zu &amp;quot;else if&amp;quot; und der Codeformatierung on Mon, 15 Nov 2010 16:23:27 GMT]]></title><description><![CDATA[<p>Hi Leute!</p>
<p>Ich hab diesen Code geschrieben. Er berechnet einem zu unterschiedlichen Anzahlen von CD's den zu bewilligen (fiktiven) Rabatt. Meine Frage ist nun, wie ich die Codeformatierung hier noch besser machen kann?</p>
<pre><code class="language-cpp">int main()
{

    cout &lt;&lt; &quot;Anzahl der zu Kaufenden DVD-Rohlinge eingeben: &quot;;
    cin &gt;&gt; anzahl;

    if((anzahl &gt;= 0) &amp;&amp; (anzahl &lt;= 9))
    {
     cout &lt;&lt; &quot;Verkaufspreis der &quot; &lt;&lt; anzahl &lt;&lt; &quot; DVD's: &quot; &lt;&lt; anzahl * 0.8 &lt;&lt; endl; 
    }

    else if((anzahl &gt;= 10) &amp;&amp; (anzahl &lt;= 49))
            {
             cout &lt;&lt; &quot;Verkaufspreis der &quot; &lt;&lt; anzahl &lt;&lt; &quot; DVD's: &quot; &lt;&lt; (float)(((anzahl * 80 * 97)/10+5)/10)/100 &lt;&lt; endl;
            }

    else if((anzahl &gt;= 50) &amp;&amp; (anzahl &lt;= 99))
    {
     cout &lt;&lt; &quot;Verkaufspreis der &quot; &lt;&lt; anzahl &lt;&lt; &quot; DVD's: &quot; &lt;&lt; (float)(((anzahl * 80 * 95)/10+5)/10)/100 &lt;&lt; endl;
    }

     else if(anzahl &gt;= 100)
     {
      cout &lt;&lt; &quot;Verkaufspreis der &quot; &lt;&lt; anzahl &lt;&lt; &quot; DVD's: &quot; &lt;&lt; (float)(((anzahl * 80 * 92)/10+5)/10)/100 &lt;&lt; endl;
     }
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1980832</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1980832</guid><dc:creator><![CDATA[bandchef]]></dc:creator><pubDate>Mon, 15 Nov 2010 16:23:27 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;else if&amp;quot; und der Codeformatierung on Mon, 15 Nov 2010 16:37:00 GMT]]></title><description><![CDATA[<p>bandchef schrieb:</p>
<blockquote>
<p>Meine Frage ist nun, wie ich die Codeformatierung hier noch besser machen kann?</p>
</blockquote>
<p>das sieht doch ein blinder mit nem krückstock...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1980835</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1980835</guid><dc:creator><![CDATA[gmoGmo]]></dc:creator><pubDate>Mon, 15 Nov 2010 16:37:00 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;else if&amp;quot; und der Codeformatierung on Mon, 15 Nov 2010 16:39:04 GMT]]></title><description><![CDATA[<p>Sei konsistent, entweder rück den else-Fall immer ein oder nie. IWobei ich ihn einrücken würde, weil dann die logische Struktur des codes besser zum tragen kommt:</p>
<pre><code class="language-cpp">int main()
{

  cout &lt;&lt; &quot;Anzahl der zu Kaufenden DVD-Rohlinge eingeben: &quot;;
  cin &gt;&gt; anzahl;

  if((anzahl &gt;= 0) &amp;&amp; (anzahl &lt;= 9))
    {
      cout &lt;&lt; &quot;Verkaufspreis der &quot; &lt;&lt; anzahl &lt;&lt; &quot; DVD's: &quot; &lt;&lt; anzahl * 0.8 &lt;&lt; endl;
    }

  else
    if((anzahl &gt;= 10) &amp;&amp; (anzahl &lt;= 49))
      {
        cout &lt;&lt; &quot;Verkaufspreis der &quot; &lt;&lt; anzahl &lt;&lt; &quot; DVD's: &quot; &lt;&lt; (float)(((anzahl * 80 * 97)/10+5)/10)/100 &lt;&lt; endl;
      }

    else
      if((anzahl &gt;= 50) &amp;&amp; (anzahl &lt;= 99))
        {
          cout &lt;&lt; &quot;Verkaufspreis der &quot; &lt;&lt; anzahl &lt;&lt; &quot; DVD's: &quot; &lt;&lt; (float)(((anzahl * 80 * 95)/10+5)/10)/100 &lt;&lt; endl;
        }

      else
        if(anzahl &gt;= 100)
          {
            cout &lt;&lt; &quot;Verkaufspreis der &quot; &lt;&lt; anzahl &lt;&lt; &quot; DVD's: &quot; &lt;&lt; (float)(((anzahl * 80 * 92)/10+5)/10)/100 &lt;&lt; endl;
          } 
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1980838</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1980838</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 15 Nov 2010 16:39:04 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;else if&amp;quot; und der Codeformatierung on Mon, 15 Nov 2010 16:41:51 GMT]]></title><description><![CDATA[<p>alter seppj was bringst du für ein beispiel? nicht nur das es total untypisch ist, sondern auch noch den komischen anschein macht als wären die alle verschachtelt..</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1980840</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1980840</guid><dc:creator><![CDATA[_eek_]]></dc:creator><pubDate>Mon, 15 Nov 2010 16:41:51 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;else if&amp;quot; und der Codeformatierung on Mon, 15 Nov 2010 16:42:26 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">int main()
{
    cout &lt;&lt; &quot;Anzahl der zu Kaufenden DVD-Rohlinge eingeben: &quot;;
    cin &gt;&gt; anzahl;

    if (anzahl &gt;= 0 &amp;&amp; anzahl &lt;= 9)
    {
        cout &lt;&lt; &quot;Verkaufspreis der &quot; &lt;&lt; anzahl &lt;&lt; &quot; DVD's: &quot; &lt;&lt; anzahl * 0.8 &lt;&lt; endl; 
    }
    else if (anzahl &gt;= 10 &amp;&amp; anzahl &lt;= 49)
    {
        cout &lt;&lt; &quot;Verkaufspreis der &quot; &lt;&lt; anzahl &lt;&lt; &quot; DVD's: &quot; &lt;&lt; (float)(((anzahl * 80 * 97)/10+5)/10)/100 &lt;&lt; endl;
    }
    else if (anzahl &gt;= 50 &amp;&amp; anzahl &lt;= 99)
    {
        cout &lt;&lt; &quot;Verkaufspreis der &quot; &lt;&lt; anzahl &lt;&lt; &quot; DVD's: &quot; &lt;&lt; (float)(((anzahl * 80 * 95)/10+5)/10)/100 &lt;&lt; endl;
    }
    else if (anzahl &gt;= 100)
    {
        cout &lt;&lt; &quot;Verkaufspreis der &quot; &lt;&lt; anzahl &lt;&lt; &quot; DVD's: &quot; &lt;&lt; (float)(((anzahl * 80 * 92)/10+5)/10)/100 &lt;&lt; endl;
    }
</code></pre>
<p>So vielleicht. if-else-if-Kaskaden würde ich persönlich niemals einrücken.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1980841</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1980841</guid><dc:creator><![CDATA[Bashar]]></dc:creator><pubDate>Mon, 15 Nov 2010 16:42:26 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;else if&amp;quot; und der Codeformatierung on Mon, 15 Nov 2010 16:44:37 GMT]]></title><description><![CDATA[<p>puh, ich bin erleichtert <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1980842</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1980842</guid><dc:creator><![CDATA[schleck]]></dc:creator><pubDate>Mon, 15 Nov 2010 16:44:37 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;else if&amp;quot; und der Codeformatierung on Mon, 15 Nov 2010 17:00:15 GMT]]></title><description><![CDATA[<p>Falls ihr mein programm mal ausführen sollet ihr unter umständen mal eine größere anzahl von dvd wie z.b. 99999 eingebt, dann werdet ihr sehen, dass das programm nicht mehr anständig auf 2 stellen rundet. Ich hab die formel zum runden im internet gefunden. wie könnte man das besser machen, dass er mir für jede mögliche anzahl an dvd's richtig rundet?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1980856</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1980856</guid><dc:creator><![CDATA[bandchef]]></dc:creator><pubDate>Mon, 15 Nov 2010 17:00:15 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;else if&amp;quot; und der Codeformatierung on Mon, 15 Nov 2010 17:05:18 GMT]]></title><description><![CDATA[<p>kann kein c++ würds aber mal <a href="http://www.cplusplus.com/reference/iostream/manipulators/setprecision/" rel="nofollow">damit</a> versuchen</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1980859</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1980859</guid><dc:creator><![CDATA[__--]]></dc:creator><pubDate>Mon, 15 Nov 2010 17:05:18 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;else if&amp;quot; und der Codeformatierung on Mon, 15 Nov 2010 17:08:43 GMT]]></title><description><![CDATA[<p>da die seite gerade nicht geht schau mal <a href="http://www.kev.pulo.com.au/pp/RESOURCES/cplusplus/ref/iostream/iomanip/setprecision.html" rel="nofollow">hier</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1980861</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1980861</guid><dc:creator><![CDATA[__--]]></dc:creator><pubDate>Mon, 15 Nov 2010 17:08:43 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;else if&amp;quot; und der Codeformatierung on Mon, 15 Nov 2010 17:11:13 GMT]]></title><description><![CDATA[<p>oh, das war schwachsinn. vergiss das mal ganz schnell wieder <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1980864</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1980864</guid><dc:creator><![CDATA[__--]]></dc:creator><pubDate>Mon, 15 Nov 2010 17:11:13 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;else if&amp;quot; und der Codeformatierung on Mon, 15 Nov 2010 17:15:21 GMT]]></title><description><![CDATA[<p>hm ok. über antworten freue ich mich immer noch</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1980865</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1980865</guid><dc:creator><![CDATA[bandchef]]></dc:creator><pubDate>Mon, 15 Nov 2010 17:15:21 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;else if&amp;quot; und der Codeformatierung on Mon, 15 Nov 2010 17:18:36 GMT]]></title><description><![CDATA[<p>evtl. gehts doch mit std::fixed oder so <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="😕"
    /> weiß schon wieso mich c++ in den wahnsinn treiben würde :p</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1980868</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1980868</guid><dc:creator><![CDATA[__--]]></dc:creator><pubDate>Mon, 15 Nov 2010 17:18:36 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;else if&amp;quot; und der Codeformatierung on Mon, 15 Nov 2010 17:24:30 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">int main()
{
	cout &lt;&lt; &quot;Anzahl der zu Kaufenden DVD-Rohlinge eingeben: &quot;;
	unsigned int anzahl;
	cin &gt;&gt; anzahl;
    if(anzahl &lt; 10)
	{
		cout &lt;&lt; &quot;Verkaufspreis der &quot; &lt;&lt; anzahl &lt;&lt; &quot; DVDs: &quot; &lt;&lt; anzahl * 0.8 &lt;&lt; endl;		// DVDs ohne Deppenapostroph
	}
	else
	{
		double faktor;		// durch den Typ double kannst du dir später den Cast sparen, der sowieso nicht ganz nach deinem Willen funktioniert hat
		if(anzahl &lt; 50)
			faktor = 97.0;
		else if(anzahl &lt; 100)
			faktor = 95.0;
		else
			faktor = 92.0;

		cout &lt;&lt; &quot;Verkaufspreis der &quot; &lt;&lt; anzahl &lt;&lt; &quot; DVDs: &quot; &lt;&lt; (((anzahl * 80 * faktor)/10+5)/10)/100 &lt;&lt; endl;
	}
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1980875</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1980875</guid><dc:creator><![CDATA[Michael E.]]></dc:creator><pubDate>Mon, 15 Nov 2010 17:24:30 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;else if&amp;quot; und der Codeformatierung on Mon, 15 Nov 2010 18:10:32 GMT]]></title><description><![CDATA[<p>Wenn du das cout aus der eigentlichen Berechnung des Wertes rausnimmst und einsiehst, dass sich für die Bestimmung des Faktors keine if-else-Kaskade lohnt, dann landest du bei dieser gleich- bzw. höherwertigen Implementation deines ursprünglichen Programms:</p>
<pre><code class="language-cpp">std::cout&lt;&lt;&quot;Anzahl der zu kaufenden DVD-Rohlinge eingeben: &quot;;
unsigned anzahl; std::cin&gt;&gt;anzahl;
std::cout&lt;&lt;&quot;Verkaufspreis der &quot;&lt;&lt;anzahl&lt;&lt;&quot; DVDs: &quot;&lt;&lt;(anzahl&lt;10?anzahl*0.8:.005+anzahl*80*(anzahl&lt;50?97:anzahl&lt;100?95:92)/10000.)&lt;&lt;std::endl;
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1980899</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1980899</guid><dc:creator><![CDATA[karl_klammer]]></dc:creator><pubDate>Mon, 15 Nov 2010 18:10:32 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;else if&amp;quot; und der Codeformatierung on Mon, 15 Nov 2010 20:56:33 GMT]]></title><description><![CDATA[<p>karl_klammer schrieb:</p>
<blockquote>
<p>Wenn du das cout aus der eigentlichen Berechnung des Wertes rausnimmst und einsiehst, dass sich für die Bestimmung des Faktors keine if-else-Kaskade lohnt, dann landest du bei dieser gleich- bzw. höherwertigen Implementation deines ursprünglichen Programms:</p>
</blockquote>
<p>ich würde eher sagen du landest über kurz oder lang beim augenarzt <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1981002</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1981002</guid><dc:creator><![CDATA[_--]]></dc:creator><pubDate>Mon, 15 Nov 2010 20:56:33 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;else if&amp;quot; und der Codeformatierung on Mon, 15 Nov 2010 22:42:05 GMT]]></title><description><![CDATA[<p>@karl_klammer<br />
Das mit den höherwertig sollte wohl ein Scherz sein? Ich hoffe doch!</p>
<p>Zwei Codezeilen in eine zu schreiben ist schonmal unschön. Und das du die Eingabe nicht prüfst ist zwar für den Trivialfall in Ordnung, aber zeugt nicht vom höheren Programmierstil. Auch das du den Datentyp nicht vollständig angibst ist keine saubere Sache.<br />
Was deine Berechnung betrifft, provozierst du durch deinen kompakten Code nur unnötig Fehler. Auch wird ein Anfänger mit deinen bedingen Ausdrücken nicht viel anfangen können.<br />
Für mich sieht deine Lösung nicht höherwertig aus, sondern eher wie der Versuch möglichst wenig Codezeilen zu produzieren.<br />
Du solltest beim Programmieren immer nach dem KISS-Prinzip vorgehen. Damit vermeidest du unnötige Fehler. Auch im Nachhinein, weil es einfacher ist, sich in den Code zu lesen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1981040</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1981040</guid><dc:creator><![CDATA[Nick Unbekannt]]></dc:creator><pubDate>Mon, 15 Nov 2010 22:42:05 GMT</pubDate></item><item><title><![CDATA[Reply to Frage zu &amp;quot;else if&amp;quot; und der Codeformatierung on Tue, 16 Nov 2010 17:57:18 GMT]]></title><description><![CDATA[<p>Eigentlich war der ganze Beitrag ein Scherz, bis auf das, dass die entgültige Ausnahme herausgenommen werden sollte.</p>
<pre><code class="language-cpp">unsigned anzahl;
for (;;) {
  std::cout &lt;&lt; &quot;Anzahl der zu kaufenden DVD-Rohlinge eingeben: &quot; &lt;&lt; std::flush;
  if (std::cin &gt;&gt; anzahl) break;
  std::cin.clear();
  std::cin.ignore(std::numeric_limits&lt;std::streamsize&gt;::max(), '\n');
}
static std::pair&lt;unsigned,double&gt; preispaare[] = {
  std::pair&lt;unsigned,double&gt;(9,100.),
  std::pair&lt;unsigned,double&gt;(49,97.),
  std::pair&lt;unsigned,double&gt;(99,95.),
  std::pair&lt;unsigned,double&gt;(std::numeric_limits&lt;unsigned&gt;::max(),92.) };
static std::map&lt;unsigned,double&gt; preise(preispaare+0, preispaare+4);
double faktor = preise.lower_bound(anzahl)-&gt;second;
double verkaufspreis = anzahl*80*faktor/10000.;
if (anzahl &gt;= 10) verkaufspreis += 0.005; // warum eigentlich?
std::cout&lt;&lt;&quot;Verkaufspreis der &quot;&lt;&lt;anzahl&lt;&lt;&quot; DVDs: &quot;&lt;&lt;verkaufspreis&lt;&lt;std::endl;
</code></pre>
<p>Das letzte höherwertig sollte man nicht ernst nehmen, das jetzige schon.<br />
In der Praxis hätte ich es dennoch anders gemacht, etwa die Eingabe ausgelagert und englische Bezeichner genommen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1981331</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1981331</guid><dc:creator><![CDATA[karl_klammer]]></dc:creator><pubDate>Tue, 16 Nov 2010 17:57:18 GMT</pubDate></item></channel></rss>