<?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[Binärcode über ein return zurückgeben]]></title><description><![CDATA[<p>Hi,<br />
ich hoffe ihr könnt mir helfen. Und zwar habe ich eine Funktion geschrieben die alle integer Werte in Binär umrechnet. Die binäre Zahl ist vom Typ String</p>
<p>Das Problem ist ich bekomme immer eine Absturzmeldung und ich weis nicht wo dran es liegt.</p>
<p>Hier der Code</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;stdlib.h&gt;

using namescpade std;
string binary(int x, string y){
	int temp = 0;
	if(x &lt; 0 || x == 0){
		cout &lt;&lt; y;
		return y;
	}else{
		temp = x;
		if(x % 2 == 0){
			y = &quot;0&quot; + y;
			temp = int(temp) / 2;
		}else{
			y = &quot;1&quot; + y;
			temp = float(temp) / 2 - 0.5;
		}
	}
	binary(temp, y);
}

int main(){
        int num = 0;
	string bcd;
	cout &lt;&lt; &quot;Enter a number :&quot;;
	cin &gt;&gt; num;
	cout &lt;&lt; &quot;The binary number is: &quot;;
	cout &lt;&lt; binary(num, bcd)&lt;&lt;endl;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/332810/binärcode-über-ein-return-zurückgeben</link><generator>RSS for Node</generator><lastBuildDate>Mon, 27 Apr 2026 12:15:13 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/332810.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 23 May 2015 13:39:37 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Binärcode über ein return zurückgeben on Sat, 23 May 2015 13:50:42 GMT]]></title><description><![CDATA[<p>Hi,<br />
ich hoffe ihr könnt mir helfen. Und zwar habe ich eine Funktion geschrieben die alle integer Werte in Binär umrechnet. Die binäre Zahl ist vom Typ String</p>
<p>Das Problem ist ich bekomme immer eine Absturzmeldung und ich weis nicht wo dran es liegt.</p>
<p>Hier der Code</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;stdlib.h&gt;

using namescpade std;
string binary(int x, string y){
	int temp = 0;
	if(x &lt; 0 || x == 0){
		cout &lt;&lt; y;
		return y;
	}else{
		temp = x;
		if(x % 2 == 0){
			y = &quot;0&quot; + y;
			temp = int(temp) / 2;
		}else{
			y = &quot;1&quot; + y;
			temp = float(temp) / 2 - 0.5;
		}
	}
	binary(temp, y);
}

int main(){
        int num = 0;
	string bcd;
	cout &lt;&lt; &quot;Enter a number :&quot;;
	cin &gt;&gt; num;
	cout &lt;&lt; &quot;The binary number is: &quot;;
	cout &lt;&lt; binary(num, bcd)&lt;&lt;endl;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2454519</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2454519</guid><dc:creator><![CDATA[isuna25]]></dc:creator><pubDate>Sat, 23 May 2015 13:50:42 GMT</pubDate></item><item><title><![CDATA[Reply to Binärcode über ein return zurückgeben on Sat, 23 May 2015 13:56:22 GMT]]></title><description><![CDATA[<p>Ließt du dir eigentlich gar nicht die Warnungen durch? Ich kriege diese Warnung: warning C4715: 'binary' : not all control paths return a value</p>
<p>Vermutlich wolltest du in der letzten Zeile von der binary Funktion ein return binary(temp, y); schreiben. Dann funktioniert auch alles und es gibt keinen Absturz.</p>
<p>Die Geschichte mit temp = float(temp) / 2 - 0.5; kannst du dir übrigens sparen. Ein einfaches temp = temp/2 reicht, da bei einer ganzzahligen Division sowieso immer abgerundet wird. Wie kommst du überhaupt auf das int(temp)? Deine temp Variable ist doch schon int.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2454520</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2454520</guid><dc:creator><![CDATA[sebi707]]></dc:creator><pubDate>Sat, 23 May 2015 13:56:22 GMT</pubDate></item><item><title><![CDATA[Reply to Binärcode über ein return zurückgeben on Sat, 23 May 2015 13:58:16 GMT]]></title><description><![CDATA[<p>bisschen wenig returns in binary und main - oder?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2454521</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2454521</guid><dc:creator><![CDATA[Gast3]]></dc:creator><pubDate>Sat, 23 May 2015 13:58:16 GMT</pubDate></item><item><title><![CDATA[Reply to Binärcode über ein return zurückgeben on Sat, 23 May 2015 14:03:08 GMT]]></title><description><![CDATA[<p>Ok das hatte ich ehrlich gesagt nicht vorgehabt aber du hast recht es klappt jetzt.</p>
<p>Das mit dem int(temp) ist die, das ich den Code von Python zu C++ konvertieren wollte.</p>
<p>Trotzdem danke</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2454522</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2454522</guid><dc:creator><![CDATA[isuna25]]></dc:creator><pubDate>Sat, 23 May 2015 14:03:08 GMT</pubDate></item><item><title><![CDATA[Reply to Binärcode über ein return zurückgeben on Sat, 23 May 2015 14:04:24 GMT]]></title><description><![CDATA[<p>Gast3 es ging sich ja auch mher um die Funktion binary. Das problem hat sich jetzt geklärt <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="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2454523</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2454523</guid><dc:creator><![CDATA[isuna25]]></dc:creator><pubDate>Sat, 23 May 2015 14:04:24 GMT</pubDate></item><item><title><![CDATA[Reply to Binärcode über ein return zurückgeben on Sat, 23 May 2015 14:09:48 GMT]]></title><description><![CDATA[<p>Oder vermutlich wolltest du eher sowas schreiben:</p>
<pre><code>string binary(int x, string&amp; y)
{
  int temp = 0;
  if(x &lt; 0 || x == 0)
  {
    //cout &lt;&lt; y;
    return y;
  }
  else
  {
    temp = x;
    if(x % 2 == 0)
    {
      y = &quot;0&quot; + y;
      temp = int(temp) / 2;
    } else {
      y = &quot;1&quot; + y;
      temp = float(temp) / 2 - 0.5;
    }
  }
  binary(temp, y);
  return y;
}
</code></pre>
<p>Man beachte das dein string Parameter jetzt eine Referenz ist und daher von der Funktion verändert werden kann. Bei deiner Variante war der string Parameter total nutzlos weil die bcd Variable in der main Funktion gar nicht geändert wurde.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2454525</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2454525</guid><dc:creator><![CDATA[sebi707]]></dc:creator><pubDate>Sat, 23 May 2015 14:09:48 GMT</pubDate></item><item><title><![CDATA[Reply to Binärcode über ein return zurückgeben on Sat, 23 May 2015 14:11:25 GMT]]></title><description><![CDATA[<p>Nachtrag: Der Rückgabewert sollte dann auch eine Referenz sein.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2454526</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2454526</guid><dc:creator><![CDATA[sebi707]]></dc:creator><pubDate>Sat, 23 May 2015 14:11:25 GMT</pubDate></item></channel></rss>