<?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[bool]]></title><description><![CDATA[<p>Hallooo</p>
<p>Ich bin jetzt seit einer Woche C++ am programmieren und mir selber Aufgaben gemacht und zum Teil aus Büchern und Internet.<br />
Mein Programm die ihr hier unten sieht ist eigentlich nichts besonderes.<br />
ich habe eine untere und eine obere Grenze (Zahlen bereich) wenn die Zahl die man eingibt im oberen oder unteren zahlenbereich ist dann geht es weiter und dann wird noch überprüft ob die Zahl gerade oder ungerade ist. mehr nicht. wie ihr sieht etwas ganz simples abr nicht einfach für einer der erst seit 1 woche programmiert.</p>
<p>zu meiner Frage:</p>
<p>Ich möchte eine Funktion implementieren, die sowohl den negativen als auch den positiven bereich überprüfen kann.</p>
<p>Habe mir vorgenommen das man maximal ein mal &quot;if - else&quot; verwendet kann.<br />
if (...)<br />
{ ...<br />
}<br />
else<br />
{ ...<br />
}</p>
<p>prototyp:<br />
bool isInRange(int zahl, int untereGrenze, int obereGrenze);</p>
<p>ist das überhaupt möglich? bin schon seit einer Zeit am hirnen und im Internet am schauen (bücher sollten kommen habe ich bestellt) aber komme nicht drauf.<br />
Ich sehe es schon vor mir muss sicher nur eine kleine Änderung machen aber schaffe es nicht.</p>
<p>kann mir jemand dabei helfen?</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;cmath&gt;

#define ERR_UNGERADE &quot;Die Zahl ist ungerade&quot;
#define ERR_GRENZE  &quot;Die Zahl ist ausserhalb der Grenze&quot;

#define OBEREGRENZE (100)
#define UNTEREGRENZE (50)

#define TEST 1

#define DEBUG 1
using namespace std;

//Funktionprototypen
//bool insideRange (int zahl,int unten, int oben);
bool isInRange(int zahl, int untereGrenze, int obereGrenze);
bool isEven (int zahl);

int main()
{	//Check bounds.
	if(UNTEREGRENZE&gt;OBEREGRENZE)
	{
		cout&lt;&lt; &quot;Falsche Konfiguration&quot;&lt;&lt; endl;
		return EXIT_FAILURE;
	}
#if TEST==1
	//cout &lt;&lt; &quot;Testfunktion: &quot;&lt;&lt;boolalpha &lt;&lt;  insideRange(50, 0, 100)  &lt;&lt; endl;
	//cout &lt;&lt; &quot;Testfunktion: &quot;&lt;&lt;boolalpha &lt;&lt;  insideRange(5, 90, 100)  &lt;&lt; endl;
	cout &lt;&lt; &quot;Testfunktion: &quot;&lt;&lt;boolalpha &lt;&lt; isEven(80)&lt;&lt; endl;
	cout &lt;&lt; &quot;Testfunktion: &quot;&lt;&lt;boolalpha &lt;&lt; isEven(85)&lt;&lt; endl;

#endif

	int zahl = 1;
#if DEBUG == 1
	cout &lt;&lt; &quot;zahl: &quot;&lt;&lt; zahl &lt;&lt; endl;
#endif

	//Zahl eingeben
	cout &lt;&lt; &quot;bitte eingeben: &quot; &lt;&lt; endl;
	cin &gt;&gt; zahl; // zahl von der konsole einlesen mittels std::cin
	cout &lt;&lt; &quot;zahl: &quot;&lt;&lt; zahl &lt;&lt; endl;

	// Zahl darf nicht groesser sein als +/- GRENZE
	// Zahl&gt;= -100 und  Zahl&lt;= 100
	// Vergleiche die Zahl mit der Oberegrenze
	// Ausgabe
	// Vergleich mit der Unteregrenze
	// Ausgabe
	// Fehlermeldung

	int error = 0;
	//if ((zahl&gt;= -OBEREGRENZE &amp;&amp; zahl &lt;= -UNTEREGRENZE) || (zahl&gt;= UNTEREGRENZE &amp;&amp; zahl&lt;= OBEREGRENZE))
	//Überprüfung war erfolgreich wir befinden uns entweder im Oberer oder Unterer Bereich
		if (isInRange(zahl, UNTEREGRENZE, OBEREGRENZE) || isInRange(zahl, -OBEREGRENZE, -UNTEREGRENZE))
		{
			cout &lt;&lt; &quot;Oberegrenze: &quot;  &lt;&lt; endl;

			cout &lt;&lt; &quot;Unteregrenze: &quot;  &lt;&lt; endl;

		}
		else
		{   // error==0
			error+=1; //error=error+1; Fall 2: Zahl ausserhalb der Grenze
		}

		// erfolgreich=keine Fehler=true
		if (!error)
		{
			if(isEven(zahl))
			{
				cout &lt;&lt; &quot;Zahl ist gerade&quot; &lt;&lt; endl;
			}
			else
			{
				error+=1; //Fall 1: Zahl ist ungerade
			}
		}
		switch (error)
		{
		case 0:
			//cout &lt;&lt; &quot;Zahl ist gerade&quot; &lt;&lt; endl;
			break;
		case 1:
			cout &lt;&lt; &quot;Ist ein Fehler aufgetreten&quot; &lt;&lt; endl;
			// Fall 1; Fall 2
			break;
		default:
			break;
		}

		return (error&gt;0 ? EXIT_FAILURE : EXIT_SUCCESS);
}

//Funktionsimplementation
/*bool insideRange (int zahl,int unten, int oben)
{
	if (zahl&gt;= unten &amp;&amp; zahl&lt;= oben)
	{
		return true;
	}
	else
	{
		return false;
	}
}
*/
bool isInRange(int zahl,int untereGrenze,int obereGrenze)
{
	if (zahl&gt;=untereGrenze &amp;&amp; zahl&lt;=obereGrenze ||zahl&lt;=-untereGrenze &amp;&amp; zahl&gt;=-obereGrenze)
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool isEven(int zahl)
{	// Resultat invertieren
	if (!(zahl%2))
	{
		return true;
	}
	else
	{
		return false;
	}

}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/247704/bool</link><generator>RSS for Node</generator><lastBuildDate>Thu, 17 Sep 2026 11:29:51 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/247704.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 14 Aug 2009 11:54:52 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to bool on Fri, 14 Aug 2009 11:54:52 GMT]]></title><description><![CDATA[<p>Hallooo</p>
<p>Ich bin jetzt seit einer Woche C++ am programmieren und mir selber Aufgaben gemacht und zum Teil aus Büchern und Internet.<br />
Mein Programm die ihr hier unten sieht ist eigentlich nichts besonderes.<br />
ich habe eine untere und eine obere Grenze (Zahlen bereich) wenn die Zahl die man eingibt im oberen oder unteren zahlenbereich ist dann geht es weiter und dann wird noch überprüft ob die Zahl gerade oder ungerade ist. mehr nicht. wie ihr sieht etwas ganz simples abr nicht einfach für einer der erst seit 1 woche programmiert.</p>
<p>zu meiner Frage:</p>
<p>Ich möchte eine Funktion implementieren, die sowohl den negativen als auch den positiven bereich überprüfen kann.</p>
<p>Habe mir vorgenommen das man maximal ein mal &quot;if - else&quot; verwendet kann.<br />
if (...)<br />
{ ...<br />
}<br />
else<br />
{ ...<br />
}</p>
<p>prototyp:<br />
bool isInRange(int zahl, int untereGrenze, int obereGrenze);</p>
<p>ist das überhaupt möglich? bin schon seit einer Zeit am hirnen und im Internet am schauen (bücher sollten kommen habe ich bestellt) aber komme nicht drauf.<br />
Ich sehe es schon vor mir muss sicher nur eine kleine Änderung machen aber schaffe es nicht.</p>
<p>kann mir jemand dabei helfen?</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;cmath&gt;

#define ERR_UNGERADE &quot;Die Zahl ist ungerade&quot;
#define ERR_GRENZE  &quot;Die Zahl ist ausserhalb der Grenze&quot;

#define OBEREGRENZE (100)
#define UNTEREGRENZE (50)

#define TEST 1

#define DEBUG 1
using namespace std;

//Funktionprototypen
//bool insideRange (int zahl,int unten, int oben);
bool isInRange(int zahl, int untereGrenze, int obereGrenze);
bool isEven (int zahl);

int main()
{	//Check bounds.
	if(UNTEREGRENZE&gt;OBEREGRENZE)
	{
		cout&lt;&lt; &quot;Falsche Konfiguration&quot;&lt;&lt; endl;
		return EXIT_FAILURE;
	}
#if TEST==1
	//cout &lt;&lt; &quot;Testfunktion: &quot;&lt;&lt;boolalpha &lt;&lt;  insideRange(50, 0, 100)  &lt;&lt; endl;
	//cout &lt;&lt; &quot;Testfunktion: &quot;&lt;&lt;boolalpha &lt;&lt;  insideRange(5, 90, 100)  &lt;&lt; endl;
	cout &lt;&lt; &quot;Testfunktion: &quot;&lt;&lt;boolalpha &lt;&lt; isEven(80)&lt;&lt; endl;
	cout &lt;&lt; &quot;Testfunktion: &quot;&lt;&lt;boolalpha &lt;&lt; isEven(85)&lt;&lt; endl;

#endif

	int zahl = 1;
#if DEBUG == 1
	cout &lt;&lt; &quot;zahl: &quot;&lt;&lt; zahl &lt;&lt; endl;
#endif

	//Zahl eingeben
	cout &lt;&lt; &quot;bitte eingeben: &quot; &lt;&lt; endl;
	cin &gt;&gt; zahl; // zahl von der konsole einlesen mittels std::cin
	cout &lt;&lt; &quot;zahl: &quot;&lt;&lt; zahl &lt;&lt; endl;

	// Zahl darf nicht groesser sein als +/- GRENZE
	// Zahl&gt;= -100 und  Zahl&lt;= 100
	// Vergleiche die Zahl mit der Oberegrenze
	// Ausgabe
	// Vergleich mit der Unteregrenze
	// Ausgabe
	// Fehlermeldung

	int error = 0;
	//if ((zahl&gt;= -OBEREGRENZE &amp;&amp; zahl &lt;= -UNTEREGRENZE) || (zahl&gt;= UNTEREGRENZE &amp;&amp; zahl&lt;= OBEREGRENZE))
	//Überprüfung war erfolgreich wir befinden uns entweder im Oberer oder Unterer Bereich
		if (isInRange(zahl, UNTEREGRENZE, OBEREGRENZE) || isInRange(zahl, -OBEREGRENZE, -UNTEREGRENZE))
		{
			cout &lt;&lt; &quot;Oberegrenze: &quot;  &lt;&lt; endl;

			cout &lt;&lt; &quot;Unteregrenze: &quot;  &lt;&lt; endl;

		}
		else
		{   // error==0
			error+=1; //error=error+1; Fall 2: Zahl ausserhalb der Grenze
		}

		// erfolgreich=keine Fehler=true
		if (!error)
		{
			if(isEven(zahl))
			{
				cout &lt;&lt; &quot;Zahl ist gerade&quot; &lt;&lt; endl;
			}
			else
			{
				error+=1; //Fall 1: Zahl ist ungerade
			}
		}
		switch (error)
		{
		case 0:
			//cout &lt;&lt; &quot;Zahl ist gerade&quot; &lt;&lt; endl;
			break;
		case 1:
			cout &lt;&lt; &quot;Ist ein Fehler aufgetreten&quot; &lt;&lt; endl;
			// Fall 1; Fall 2
			break;
		default:
			break;
		}

		return (error&gt;0 ? EXIT_FAILURE : EXIT_SUCCESS);
}

//Funktionsimplementation
/*bool insideRange (int zahl,int unten, int oben)
{
	if (zahl&gt;= unten &amp;&amp; zahl&lt;= oben)
	{
		return true;
	}
	else
	{
		return false;
	}
}
*/
bool isInRange(int zahl,int untereGrenze,int obereGrenze)
{
	if (zahl&gt;=untereGrenze &amp;&amp; zahl&lt;=obereGrenze ||zahl&lt;=-untereGrenze &amp;&amp; zahl&gt;=-obereGrenze)
	{
		return true;
	}
	else
	{
		return false;
	}
}
bool isEven(int zahl)
{	// Resultat invertieren
	if (!(zahl%2))
	{
		return true;
	}
	else
	{
		return false;
	}

}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1760723</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1760723</guid><dc:creator><![CDATA[whaaatz]]></dc:creator><pubDate>Fri, 14 Aug 2009 11:54:52 GMT</pubDate></item><item><title><![CDATA[Reply to bool on Fri, 14 Aug 2009 12:00:30 GMT]]></title><description><![CDATA[<p>Ohne allzu lange drüber nachzudenken würde ich vermuten, Du musst lediglich die Bedingungen (&amp;&amp; || &amp;&amp;) noch ordentlich klammern. Denn entweder müssen (die beiden linken Bedingungen) erfüllt sein oder (die beiden rechten).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1760726</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1760726</guid><dc:creator><![CDATA[LordJaxom]]></dc:creator><pubDate>Fri, 14 Aug 2009 12:00:30 GMT</pubDate></item><item><title><![CDATA[Reply to bool on Fri, 14 Aug 2009 12:00:53 GMT]]></title><description><![CDATA[<ul>
<li>Verwende [cpp]- und <strong>nicht</strong> [code]-Tags.</li>
<li>Hör endlich mit diesen Makros auf und verwende C++-Alternativen (Funktionen, <code>const</code> -Variablen). Das gilt natürlich nicht für diejenigen, die für bedingte Kompilierung zuständig sind.</li>
<li>Sobald du mehrere logische Bedingungen hast, brauchst du grundsätzlich mehrere Abfragen. Du kannst vielleicht mit AND, OR und NOT etwas zusammenfassen, aber oft wird der Code dadurch schlechter.</li>
<li>Es gibt auch noch <code>operator?:</code> - das wäre dann wohl die Cheaterlösung. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></li>
</ul>
]]></description><link>https://www.c-plusplus.net/forum/post/1760727</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1760727</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Fri, 14 Aug 2009 12:00:53 GMT</pubDate></item><item><title><![CDATA[Reply to bool on Fri, 14 Aug 2009 12:07:04 GMT]]></title><description><![CDATA[<p>Du könntest dann das hier auch noch abkürzen:</p>
<pre><code class="language-cpp">if ( Bedingung )
 return true;
else
 return false;
</code></pre>
<p>Ist ersetzbar mit:</p>
<pre><code class="language-cpp">return ( Bedingung )
</code></pre>
<p>Die Bedinung ist schlussentlich auch ein boolscher Ausdruck.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1760732</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1760732</guid><dc:creator><![CDATA[drakon]]></dc:creator><pubDate>Fri, 14 Aug 2009 12:07:04 GMT</pubDate></item><item><title><![CDATA[Reply to bool on Fri, 14 Aug 2009 12:13:30 GMT]]></title><description><![CDATA[<p>Nexus schrieb:</p>
<blockquote>
<p>...</p>
</blockquote>
<p>Ich möchte noch</p>
<ul>
<li>Verwende aussagekräftige Titel für deine Threads</li>
</ul>
<p>hinzufügen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1760736</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1760736</guid><dc:creator><![CDATA[Registrierter Troll]]></dc:creator><pubDate>Fri, 14 Aug 2009 12:13:30 GMT</pubDate></item><item><title><![CDATA[Reply to bool on Fri, 14 Aug 2009 12:28:13 GMT]]></title><description><![CDATA[<p>whaaatz schrieb:</p>
<blockquote>
<p>bool</p>
</blockquote>
<p><code>int</code></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1760744</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1760744</guid><dc:creator><![CDATA[Tachyon]]></dc:creator><pubDate>Fri, 14 Aug 2009 12:28:13 GMT</pubDate></item><item><title><![CDATA[Reply to bool on Fri, 14 Aug 2009 12:40:04 GMT]]></title><description><![CDATA[<p>whaaatz schrieb:</p>
<blockquote>
<p>prototyp:<br />
bool isInRange(int zahl, int untereGrenze, int obereGrenze);</p>
</blockquote>
<pre><code class="language-cpp">inline bool isInRange(int zahl, int untereGrenze, int obereGrenze)
{
  const unsigned u = untereGrenze;
  return (zahl-u) &lt;= (obereGrenze-u);
}
</code></pre>
<p>So?</p>
<p>Gruß,<br />
SP</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1760758</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1760758</guid><dc:creator><![CDATA[Sebastian Pizer]]></dc:creator><pubDate>Fri, 14 Aug 2009 12:40:04 GMT</pubDate></item><item><title><![CDATA[Reply to bool on Fri, 14 Aug 2009 12:44:38 GMT]]></title><description><![CDATA[<p>Sebastian Pizer schrieb:</p>
<blockquote>
<p>whaaatz schrieb:</p>
<blockquote>
<p>prototyp:<br />
bool isInRange(int zahl, int untereGrenze, int obereGrenze);</p>
</blockquote>
<pre><code class="language-cpp">inline bool isInRange(int zahl, int untereGrenze, int obereGrenze)
{
  const unsigned u = untereGrenze;
  return (zahl-u) &lt;= (obereGrenze-u);
}
</code></pre>
<p>So?</p>
<p>Gruß,<br />
SP</p>
</blockquote>
<p>Mein Mathe-Verständnis sagt dazu<br />
zahl-u &lt;= oberGrenze-u | auf beiden Seiten +u<br />
zahl &lt;= obergrenze<br />
Das verwirtrt mich.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1760763</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1760763</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Fri, 14 Aug 2009 12:44:38 GMT</pubDate></item><item><title><![CDATA[Reply to bool on Fri, 14 Aug 2009 13:08:35 GMT]]></title><description><![CDATA[<p>volkard schrieb:</p>
<blockquote>
<p>Mein Mathe-Verständnis sagt dazu</p>
<pre><code>zahl-u  &lt;=  oberGrenze-u  | auf beiden Seiten +u
&lt;=&gt;   zahl    &lt;=  obergrenze
</code></pre>
<p>Das verwirtrt mich.</p>
</blockquote>
<p>Es ist wegen der Modulo-Arithmetik nicht ganz korrekt. u ist ein <code>unsigned</code> int. =&gt; zahl-u und obereGrenze-u sind auch <code>unsigned</code> ints (nie negativ) =&gt; Die Äquivalenz (&lt;=&gt;) ist an der Stelle falsch. Es muss heißen</p>
<pre><code>untergrenze &lt;= zahl   &lt;= obergrenze     | überall u abziehen
      ~~~~~~~~~~~    ~~~~      ~~~~~~~~~~
          int        int           int

&lt;=&gt;             0 &lt;= zahl-u &lt;= obergrenze-u
                     ~~~~~~    ~~~~~~~~~~~~
                    unsigned     unsigned
</code></pre>
<p>...und da 0&lt;=zahl-u immer gilt (ist ja unsigned), kann man sich diesen Test sparen.</p>
<p>Gruß,<br />
SP</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1760783</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1760783</guid><dc:creator><![CDATA[Sebastian Pizer]]></dc:creator><pubDate>Fri, 14 Aug 2009 13:08:35 GMT</pubDate></item><item><title><![CDATA[Reply to bool on Fri, 14 Aug 2009 13:16:33 GMT]]></title><description><![CDATA[<p>Netter Trick. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44d.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_up"
      title=":+1:"
      alt="👍"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1760793</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1760793</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Fri, 14 Aug 2009 13:16:33 GMT</pubDate></item><item><title><![CDATA[Reply to bool on Fri, 14 Aug 2009 14:45:37 GMT]]></title><description><![CDATA[<p>Ist trotzdem nicht ganz richtig. So würde die Funktion nämlich bei z.B. isInRange(2, 1, 0) true zurückgeben..</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1760850</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1760850</guid><dc:creator><![CDATA[life]]></dc:creator><pubDate>Fri, 14 Aug 2009 14:45:37 GMT</pubDate></item><item><title><![CDATA[Reply to bool on Fri, 14 Aug 2009 15:17:24 GMT]]></title><description><![CDATA[<p>Du hast recht. Ich habe stillschweigend untereGrenze &lt;= obereGrenze vorausgesetzt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1760876</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1760876</guid><dc:creator><![CDATA[Sebastian Pizer]]></dc:creator><pubDate>Fri, 14 Aug 2009 15:17:24 GMT</pubDate></item></channel></rss>