<?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[C++ - Probleme mit Dezimal to Binaer Programm]]></title><description><![CDATA[<p>Hallo allerseits,</p>
<p>ich habe ein Programm geschrieben welches eine Dezimal Zahl einliest und dann in HEX-, Oktal- und Binaerzahlen ausgiebt. Hier der Code:</p>
<pre><code>#include &lt;iostream&gt;
using namespace std;

void ausgabe(int zahl);
int ausgabeBin(int i);

int main(){
	int zahl = 100;

	ausgabe(zahl);
	return(0);
}

void ausgabe(int zahl){
	while (zahl != 0){
		cout &lt;&lt; &quot;Geben Sie eine Zahl ein die in HEX und Oktal-Zahl ausgegeben wird&quot; &lt;&lt; endl;
		cout &lt;&lt; &quot;Dezimal Zahl: &quot;;
		cin &gt;&gt; zahl;
		cout &lt;&lt; &quot;HEX: &quot; &lt;&lt; hex &lt;&lt; zahl &lt;&lt; endl; // HEX Konvertierung
		cout &lt;&lt; &quot;Oktal: &quot; &lt;&lt; oct &lt;&lt; zahl &lt;&lt; endl; // Oktal Konvertierung
		cout &lt;&lt; &quot;Binaer: &quot;;
		cout &lt;&lt; ausgabeBin(zahl) &lt;&lt; endl;
	}
}
// Binaere Konvertierung
int ausgabeBin(int i)
{
	if (i!=0) {
		ausgabeBin(i/2);
		if ((i % 2) == 0) {
			cout &lt;&lt; &quot;0&quot;;
		}
		else {
			cout &lt;&lt; &quot;1&quot;;
		}
	}
	return(0);
}
</code></pre>
<p>Das Problem ist das es immer eine Null drangehaengt wird, wegen dem return. Ich habe versucht aus der ausgabeBin-Funktion, eine Funktion mit Datentyp void zu machen. Aber da bekomme ich eine Fehlermeldung, das dies nicht moeglich ist.</p>
<p>Und noch etwas ist mir aufgefallen:</p>
<pre><code>cout &lt;&lt; &quot;Binaer: &quot; &lt;&lt; ausgabeBin(zahl) &lt;&lt; endl;
</code></pre>
<p>und</p>
<pre><code>cout &lt;&lt; &quot;Binaer: &quot;;
cout &lt;&lt; ausgabeBin(zahl) &lt;&lt; endl;
</code></pre>
<p>ist anscheinend nicht das gleiche da die Binaerzahlen der Funktion als erstes ausgegeben wird. Und erst spaeter &quot;Binaer: &quot; mit dem Rueckgabewert &quot;0&quot;. Kann mir jemand erklaeren wieso ? Binaer steht ja(von links gesehen) vor der Funktion <code>ausgabe</code> .</p>
<p>Gruss</p>
<p>THE-E</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/268319/c-probleme-mit-dezimal-to-binaer-programm</link><generator>RSS for Node</generator><lastBuildDate>Tue, 01 Sep 2026 21:14:08 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/268319.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 07 Jun 2010 14:18:39 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to C++ - Probleme mit Dezimal to Binaer Programm on Mon, 07 Jun 2010 14:18:39 GMT]]></title><description><![CDATA[<p>Hallo allerseits,</p>
<p>ich habe ein Programm geschrieben welches eine Dezimal Zahl einliest und dann in HEX-, Oktal- und Binaerzahlen ausgiebt. Hier der Code:</p>
<pre><code>#include &lt;iostream&gt;
using namespace std;

void ausgabe(int zahl);
int ausgabeBin(int i);

int main(){
	int zahl = 100;

	ausgabe(zahl);
	return(0);
}

void ausgabe(int zahl){
	while (zahl != 0){
		cout &lt;&lt; &quot;Geben Sie eine Zahl ein die in HEX und Oktal-Zahl ausgegeben wird&quot; &lt;&lt; endl;
		cout &lt;&lt; &quot;Dezimal Zahl: &quot;;
		cin &gt;&gt; zahl;
		cout &lt;&lt; &quot;HEX: &quot; &lt;&lt; hex &lt;&lt; zahl &lt;&lt; endl; // HEX Konvertierung
		cout &lt;&lt; &quot;Oktal: &quot; &lt;&lt; oct &lt;&lt; zahl &lt;&lt; endl; // Oktal Konvertierung
		cout &lt;&lt; &quot;Binaer: &quot;;
		cout &lt;&lt; ausgabeBin(zahl) &lt;&lt; endl;
	}
}
// Binaere Konvertierung
int ausgabeBin(int i)
{
	if (i!=0) {
		ausgabeBin(i/2);
		if ((i % 2) == 0) {
			cout &lt;&lt; &quot;0&quot;;
		}
		else {
			cout &lt;&lt; &quot;1&quot;;
		}
	}
	return(0);
}
</code></pre>
<p>Das Problem ist das es immer eine Null drangehaengt wird, wegen dem return. Ich habe versucht aus der ausgabeBin-Funktion, eine Funktion mit Datentyp void zu machen. Aber da bekomme ich eine Fehlermeldung, das dies nicht moeglich ist.</p>
<p>Und noch etwas ist mir aufgefallen:</p>
<pre><code>cout &lt;&lt; &quot;Binaer: &quot; &lt;&lt; ausgabeBin(zahl) &lt;&lt; endl;
</code></pre>
<p>und</p>
<pre><code>cout &lt;&lt; &quot;Binaer: &quot;;
cout &lt;&lt; ausgabeBin(zahl) &lt;&lt; endl;
</code></pre>
<p>ist anscheinend nicht das gleiche da die Binaerzahlen der Funktion als erstes ausgegeben wird. Und erst spaeter &quot;Binaer: &quot; mit dem Rueckgabewert &quot;0&quot;. Kann mir jemand erklaeren wieso ? Binaer steht ja(von links gesehen) vor der Funktion <code>ausgabe</code> .</p>
<p>Gruss</p>
<p>THE-E</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1908366</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1908366</guid><dc:creator><![CDATA[THE-E]]></dc:creator><pubDate>Mon, 07 Jun 2010 14:18:39 GMT</pubDate></item><item><title><![CDATA[Reply to C++ - Probleme mit Dezimal to Binaer Programm on Mon, 07 Jun 2010 14:32:39 GMT]]></title><description><![CDATA[<p>Du hast das ja auch reichlich komisch gemacht. Warum gibst du die 0 zurück? die hat doch gar keine Bedeutung. Und wieso gibst du sie danach auch noch aus? Es reicht doch, folgendes zu machen:</p>
<pre><code class="language-cpp">cout &lt;&lt; &quot;Binaer: &quot;;
ausgabeBin(zahl);
cout &lt;&lt; endl;
</code></pre>
<p>Und ausgabeBin gibst du den Typ void.</p>
<p>Deine andere Frage: Tja, so ist das halt mit den Nebeneffekten. da ist die reihenfolge nicht definiert, denn &lt;&lt; ist kein Sequenzpunkt. Die Reihenfolge ist undefiniert, die meisten Compiler machen daraus aber Code von rechts nach links. Wenn du das nicht willst, dann schreib Funktionen ohne Nebeneffekte ( <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/26a0.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--warning"
      title=":warning:"
      alt="⚠"
    /> Das ist auch allgemein eine sehr gute Idee, nicht nur hier!). ausgabeBin könnte beispielsweise einen String mit der richtigen Ausgabe zurückgeben und selber nichts ausgeben. Dann kannst du auch <code>cout &lt;&lt; &quot;Binaer: &quot; &lt;&lt; ausgabeBin(zahl) &lt;&lt; endl;</code> schreiben.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1908372</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1908372</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 07 Jun 2010 14:32:39 GMT</pubDate></item><item><title><![CDATA[Reply to C++ - Probleme mit Dezimal to Binaer Programm on Mon, 07 Jun 2010 14:33:58 GMT]]></title><description><![CDATA[<p>Mach aus &quot;cout &lt;&lt; ausgabeBin(zahl) &lt;&lt; endl; &quot; einfach &quot;ausgabeBin(zahl);&quot; Zum Rest sage ich mal nichts, hab jetzt Feierabend.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1908373</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1908373</guid><dc:creator><![CDATA[Fellhuhn]]></dc:creator><pubDate>Mon, 07 Jun 2010 14:33:58 GMT</pubDate></item><item><title><![CDATA[Reply to C++ - Probleme mit Dezimal to Binaer Programm on Mon, 07 Jun 2010 14:39:31 GMT]]></title><description><![CDATA[<p>Es gibt übrigens auch eine Möglichkeit der Standardbibliothek, Zahlen binär auszugeben, mittels bitset:<br />
<a href="http://www.cplusplus.com/reference/stl/bitset/operators/" rel="nofollow">http://www.cplusplus.com/reference/stl/bitset/operators/</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1908377</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1908377</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 07 Jun 2010 14:39:31 GMT</pubDate></item><item><title><![CDATA[Reply to C++ - Probleme mit Dezimal to Binaer Programm on Mon, 07 Jun 2010 15:50:04 GMT]]></title><description><![CDATA[<p>Ihr habt mir sehr geholfen, das sind meistens kleine Fehler die einen manchmal zum verzweifeln bringen. <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="🙂"
    /> Danke schoen <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="👍"
    /> <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="👍"
    /> Ihr seid echt gut !</p>
<p>SeppJ schrieb:</p>
<blockquote>
<p>Es reicht doch, folgendes zu machen:</p>
<pre><code class="language-cpp">cout &lt;&lt; &quot;Binaer: &quot;;
ausgabeBin(zahl);
cout &lt;&lt; endl;
</code></pre>
</blockquote>
<p>Ist es moeglich dies in einer Zeile zu schreiben ?</p>
<pre><code class="language-cpp">cout &lt;&lt; &quot;Binaer: &quot;; ausgabeBin(zahl); cout &lt;&lt; endl;
</code></pre>
<p>...geht ja nicht</p>
<p><strong>EDIT:</strong></p>
<p>Sorry das obengeschriebene geht doch. Ich hatte nur vergessen die Konsole zu schliessen dadurch war ein erneutes debuggen nicht moeglich.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1908421</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1908421</guid><dc:creator><![CDATA[THE-E]]></dc:creator><pubDate>Mon, 07 Jun 2010 15:50:04 GMT</pubDate></item><item><title><![CDATA[Reply to C++ - Probleme mit Dezimal to Binaer Programm on Mon, 07 Jun 2010 17:28:21 GMT]]></title><description><![CDATA[<p>und die boost libraries haben auch ein dynamisches bitset</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1908462</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1908462</guid><dc:creator><![CDATA[muli]]></dc:creator><pubDate>Mon, 07 Jun 2010 17:28:21 GMT</pubDate></item><item><title><![CDATA[Reply to C++ - Probleme mit Dezimal to Binaer Programm on Mon, 07 Jun 2010 19:06:35 GMT]]></title><description><![CDATA[<p>Heißer Tipp: Schreib nicht alles in eine Zeile. Zeilenumbrüche sind dein Freund. Das macht Debuggen um so vieles einfacher. Man erfährt nämlich meistens nur die Zeile in der ein Fehler auftritt. Und wenn du mehrere Befehle in einer Zeile hast (du kannst auch das gesamte Programm in einer Zeile schreiben), dann nützt dies nicht viel.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1908533</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1908533</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Mon, 07 Jun 2010 19:06:35 GMT</pubDate></item><item><title><![CDATA[Reply to C++ - Probleme mit Dezimal to Binaer Programm on Mon, 07 Jun 2010 19:09:10 GMT]]></title><description><![CDATA[<p>muli schrieb:</p>
<blockquote>
<p>und die boost libraries haben auch ein dynamisches bitset</p>
</blockquote>
<p>Es gibt auch noch <code>std::vector&lt;bool&gt;</code> , falls einem nach Wohltätigkeit ist. :p</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1908538</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1908538</guid><dc:creator><![CDATA[Nexus]]></dc:creator><pubDate>Mon, 07 Jun 2010 19:09:10 GMT</pubDate></item><item><title><![CDATA[Reply to C++ - Probleme mit Dezimal to Binaer Programm on Tue, 08 Jun 2010 07:00:12 GMT]]></title><description><![CDATA[<p>SeppJ schrieb:</p>
<blockquote>
<p>Heißer Tipp: Schreib nicht alles in eine Zeile. Zeilenumbrüche sind dein Freund. Das macht Debuggen um so vieles einfacher. Man erfährt nämlich meistens nur die Zeile in der ein Fehler auftritt. Und wenn du mehrere Befehle in einer Zeile hast (du kannst auch das gesamte Programm in einer Zeile schreiben), dann nützt dies nicht viel.</p>
</blockquote>
<p>So ein heisser Tipp, ich glaub ich verbrenne :). Nein, mal ehrlich, danke fuer den Tipp, aber ich wollte das nur so, da die Zeilen ueber mir eine aehnlich Formatierung haben und der Code ohnehin nicht allzu lang ist. <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="😉"
    /> <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/1908732</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1908732</guid><dc:creator><![CDATA[THE-E]]></dc:creator><pubDate>Tue, 08 Jun 2010 07:00:12 GMT</pubDate></item><item><title><![CDATA[Reply to C++ - Probleme mit Dezimal to Binaer Programm on Tue, 08 Jun 2010 09:20:52 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">#include &lt;iostream&gt;

struct bin
{
	int const&amp; fremderInt;
	bin(int const&amp; fi):
			fremderInt(fi)
	{
	}
	friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; out,bin const&amp; aB)
	{
		rekursivesGefrickel(aB.fremderInt,out);
		return out;
	}
	static void rekursivesGefrickel(int i,std::ostream&amp; out)
	{
		if (i!=0) {
			rekursivesGefrickel(i/2,out);
			if ((i % 2) == 0) {
				out &lt;&lt; &quot;0&quot;;
			}
			else {
				out &lt;&lt; &quot;1&quot;;
			}
		}
	}
};

using namespace std;

void ausgabe(int zahl) {
	while (zahl != 0) {
		cout &lt;&lt; &quot;Geben Sie eine Zahl ein die in HEX und Oktal-Zahl ausgegeben wird&quot; &lt;&lt; endl;
		cout &lt;&lt; &quot;Dezimal Zahl: &quot;;
		cin &gt;&gt; zahl;
		cout &lt;&lt; &quot;HEX: &quot; &lt;&lt; hex &lt;&lt; zahl &lt;&lt; endl; // HEX Konvertierung
		cout &lt;&lt; &quot;Oktal: &quot; &lt;&lt; oct &lt;&lt; zahl &lt;&lt; endl; // Oktal Konvertierung
		cout &lt;&lt; &quot;Binaer: &quot;&lt;&lt; bin(zahl) &lt;&lt; endl; // Binär Konvertierung
	}
}

int main() {
	int zahl = 100;
	ausgabe(zahl);
	return 0;
}
</code></pre>
<p>Von so Ausgabeobjekten halte ich viel mehr als von Stream-Flags.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1908779</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1908779</guid><dc:creator><![CDATA[volkard]]></dc:creator><pubDate>Tue, 08 Jun 2010 09:20:52 GMT</pubDate></item></channel></rss>