<?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[printf]]></title><description><![CDATA[<pre><code class="language-cpp">[code]#include &lt;iostream&gt;
using namespace std;
#include &lt;math.h&gt;
#include &lt;time.h&gt;
#include &lt;string&gt;
#include &lt;sstream&gt;

int a;
int kubik;

int main (int argc, char * const argv[]) 
{	
	for(a=0; a&lt;8; a++) 
	{
	printf(&quot;Schleife %d\n&quot;, a+1);
	kubik = a*a*a;
	cout&lt;&lt;&quot;cout kubik= &quot;&lt;&lt;kubik&lt;&lt;&quot;\n&quot;;

	string p;
	ostringstream outStream;
	outStream &lt;&lt; kubik;
	p = outStream.str();
	cout&lt;&lt;&quot;cout p= &quot;&lt;&lt;p&lt;&lt;&quot;\n&quot;;
	printf(&quot;p 1. Zeichen %i\n&quot;, string p [1]);
	}	
    return 0;
}[/code]
</code></pre>
<p>Hallo, Helfer, ich habe einen Integer (kubik) in einen String (p) umgewandelt. Bis cout in Zeile 23 funktioniert alles. Bei Zeile 24 sagte er aber: <strong>&quot;expected primary expression befor &quot;p&quot;&quot;</strong>. Was ist ein Primärausdruck? Was erwartet er? Ich möchte den ersten Buchstaben des Strings dort haben.</p>
<p>Ich probier jetzt schon 1 Stunde....</p>
<p>Danke für den rettenden Tip. (Bin kein Profi und kein Schüler, mach's nur aus Hobbygründen)</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/284699/printf</link><generator>RSS for Node</generator><lastBuildDate>Fri, 21 Aug 2026 20:13:06 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/284699.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 06 Apr 2011 11:03:31 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to printf on Wed, 06 Apr 2011 11:03:31 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">[code]#include &lt;iostream&gt;
using namespace std;
#include &lt;math.h&gt;
#include &lt;time.h&gt;
#include &lt;string&gt;
#include &lt;sstream&gt;

int a;
int kubik;

int main (int argc, char * const argv[]) 
{	
	for(a=0; a&lt;8; a++) 
	{
	printf(&quot;Schleife %d\n&quot;, a+1);
	kubik = a*a*a;
	cout&lt;&lt;&quot;cout kubik= &quot;&lt;&lt;kubik&lt;&lt;&quot;\n&quot;;

	string p;
	ostringstream outStream;
	outStream &lt;&lt; kubik;
	p = outStream.str();
	cout&lt;&lt;&quot;cout p= &quot;&lt;&lt;p&lt;&lt;&quot;\n&quot;;
	printf(&quot;p 1. Zeichen %i\n&quot;, string p [1]);
	}	
    return 0;
}[/code]
</code></pre>
<p>Hallo, Helfer, ich habe einen Integer (kubik) in einen String (p) umgewandelt. Bis cout in Zeile 23 funktioniert alles. Bei Zeile 24 sagte er aber: <strong>&quot;expected primary expression befor &quot;p&quot;&quot;</strong>. Was ist ein Primärausdruck? Was erwartet er? Ich möchte den ersten Buchstaben des Strings dort haben.</p>
<p>Ich probier jetzt schon 1 Stunde....</p>
<p>Danke für den rettenden Tip. (Bin kein Profi und kein Schüler, mach's nur aus Hobbygründen)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2045394</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2045394</guid><dc:creator><![CDATA[Bekell]]></dc:creator><pubDate>Wed, 06 Apr 2011 11:03:31 GMT</pubDate></item><item><title><![CDATA[Reply to printf on Wed, 06 Apr 2011 11:18:46 GMT]]></title><description><![CDATA[<p>Tip 1) benutze [ cpp ] Tags statt [ code ] Tags. Ist besser lesbar (Syntaxhighlighting)<br />
Tip 2) benutze keine using-Direktive in Headern oder vor dem #include von headern<br />
Tip 3) benutze die C++-Header &lt;cmath&gt;, &lt;ctime&gt; an Stelle der C-Header &lt;math.h&gt;, &lt;time.h&gt;<br />
Tip 4) benutze Variablen wenn möglich lieber lokal statt global (kubik, a)<br />
Tip 5) mische nicht printf und cout. Eins von beidem reicht<br />
Tip 6) zu deinem Fehler: printf kann nur mit eingebauten Datentypen. Weil es aus C kommt kennt es die C++-Tpen garnicht. Benutze daher für std::strings cout.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2045406</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2045406</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Wed, 06 Apr 2011 11:18:46 GMT</pubDate></item><item><title><![CDATA[Reply to printf on Wed, 06 Apr 2011 11:41:07 GMT]]></title><description><![CDATA[<p>Was soll das &quot;sting&quot; hier denn bedeuten?</p>
<pre><code class="language-cpp">printf(&quot;p 1. Zeichen %i\n&quot;, string p [1]);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2045419</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2045419</guid><dc:creator><![CDATA[manni66]]></dc:creator><pubDate>Wed, 06 Apr 2011 11:41:07 GMT</pubDate></item><item><title><![CDATA[Reply to printf on Wed, 06 Apr 2011 11:48:23 GMT]]></title><description><![CDATA[<p>Nochwas:</p>
<p>Bekell schrieb:</p>
<blockquote>
<p>Ich probier jetzt schon 1 Stunde....</p>
</blockquote>
<p>probieren kannst du in der Küche oder beim Eismann. Beim Programmieren solltest du wissen, was du tust - und wenn du nicht weißt, wie etwas zu tun ist, solltest du im Buch/Tutorial deiner Wahl nachlesen oder dich anderweitig kundig machen. Ausprobieren führt in 80% der Fälle nur zu Frust, weils garnicht funktioniert, ind 18% zu Code, der compiliert, aber nicht ganz das tut was du willst, und in 2% der Fälle zu Code, der so tut als würde es funktionieren, der aber nicht korrekt ist oder nicht portabel oder nur zufällig &quot;funktioniert&quot;, eigentlich aber undefiniertes Verhalten erzeugt. (&quot;undefiniertes Verhalten&quot; kann mit &quot;extrem böse, Finger weg!&quot; übersetzt werden)</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2045425</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2045425</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Wed, 06 Apr 2011 11:48:23 GMT</pubDate></item><item><title><![CDATA[Reply to printf on Wed, 06 Apr 2011 12:36:26 GMT]]></title><description><![CDATA[<p>pumuckl schrieb:</p>
<blockquote>
<p>Nochwas:</p>
<p>Bekell schrieb:</p>
<blockquote>
<p>Ich probier jetzt schon 1 Stunde....</p>
</blockquote>
<p>probieren kannst du in der Küche oder beim Eismann.</p>
</blockquote>
<p><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="👍"
    /> Bei sowas muss ich mich immer schlapplachen.</p>
<p>Wie manni schon sagte, hat das string da erstmal nichts zu suchen.<br />
Das erste Zeichen erhälst du übrigens mit dem index 0 und nicht mit 1.<br />
Ausserdem würde der Ausgegebene Wert sicherlch nicht dem entsprechen, was du erwartest.</p>
<p>Kleines Beispiel:</p>
<pre><code class="language-cpp">std::string p(&quot;123&quot;);
printf( &quot;Zeichen an 1. Position: %i \n&quot;, p[0]);
printf( &quot;Zeichen an 1. Position: %c \n&quot;, p[0]);
</code></pre>
<p>Ausgabe:</p>
<pre><code>49
1
</code></pre>
<p>Warum da nun 49 rauskommt, darfst du dir nun überlegen <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/2045454</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2045454</guid><dc:creator><![CDATA[inter2k3]]></dc:creator><pubDate>Wed, 06 Apr 2011 12:36:26 GMT</pubDate></item><item><title><![CDATA[Reply to printf on Wed, 06 Apr 2011 18:40:06 GMT]]></title><description><![CDATA[<p>So, das klappte.</p>
<p>Jetzt möchte ich, daß er vom String nur eine Position bei cout ausgibt.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2045648</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2045648</guid><dc:creator><![CDATA[Bekell]]></dc:creator><pubDate>Wed, 06 Apr 2011 18:40:06 GMT</pubDate></item><item><title><![CDATA[Reply to printf on Wed, 06 Apr 2011 18:44:52 GMT]]></title><description><![CDATA[<p>Über Index-Zugriff kannst du die einzelnen Zeichen des Strings erreichen: <code>cout&lt;&lt;p[0];</code></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2045650</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2045650</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Wed, 06 Apr 2011 18:44:52 GMT</pubDate></item><item><title><![CDATA[Reply to printf on Wed, 06 Apr 2011 18:58:14 GMT]]></title><description><![CDATA[<p>Danke, .... jetzt geh ich erst mit den Kindern beten....</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2045659</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2045659</guid><dc:creator><![CDATA[Bekell]]></dc:creator><pubDate>Wed, 06 Apr 2011 18:58:14 GMT</pubDate></item><item><title><![CDATA[Reply to printf on Wed, 06 Apr 2011 21:15:54 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;cmath&gt;
#include &lt;ctime&gt;
#include &lt;string&gt;
#include &lt;sstream&gt;
using namespace std;
int gesamtziffernzaehler;

int main (int argc, char * const argv[]) 
{

int Z0;
int Z1;
int Z2;
int Z3;
int Z4;
int Z5;
int Z6;
int Z7;
int Z8;
int Z9;

	int a;
	for(a=0; a&lt;22; a++) 
	{
	printf(&quot;Schleife %d\n&quot;, a+1);
	int kubik = a*a*a;
	cout&lt;&lt;a&lt;&lt;&quot; Qubik = &quot;&lt;&lt;kubik&lt;&lt;&quot;\n&quot;;

	string p; //Zahl in String umwandeln
	ostringstream outStream;
	outStream &lt;&lt; kubik;
	p = outStream.str();
	//cout&lt;&lt;&quot;cout p= &quot;&lt;&lt;p&lt;&lt;&quot;\n&quot;;
	cout&lt;&lt;a&lt;&lt;&quot; Qubik hat = &quot;&lt;&lt;p.length()&lt;&lt;&quot; Ziffern\n&quot;; //Länge feststellen

	//cout&lt;&lt;&quot;länge p= &quot;&lt;&lt;p(3)&lt;&lt;&quot;\n&quot;;

    string::iterator i;
    for (i=p.begin(); i!=p.end(); i++)
    {
        cout&lt;&lt;&quot;/p= &quot; &lt;&lt; *i ;

	gesamtziffernzaehler = gesamtziffernzaehler+1;
	int meineZahl;
	istringstream inStream(p); //verrückte stelle
inStream &gt;&gt; meineZahl;
	if (meineZahl == 0)
	{
	int Z0=Z0+1;
	}

	if (meineZahl == 1)
	{
	int Z1=Z1+1;
	}

	if (meineZahl == 2)
	{
	int Z2=Z2+1;
	}

	if (meineZahl == 3)
	{
	int Z3=Z3+1;
	}

	if (meineZahl == 4)
	{
	int Z4=Z4+1;
	}

	if (meineZahl == 5)
	{
	int Z5=Z5+1;
	}

	if (meineZahl == 6)
	{
	int Z6=Z6+1;
	}

	if (meineZahl == 7)
	{
	int Z7=Z7+1;
	}

	if (meineZahl == 8)
	{
	int Z8=Z8+1;
	}

	if (meineZahl == 9)
	{
	int Z9=Z9+1;
	}

    {
    cout &lt;&lt; endl;
	cout&lt;&lt;&quot;gzz = &quot;&lt;&lt;gesamtziffernzaehler&lt;&lt;&quot;\n&quot;;
	cout&lt;&lt;&quot;Z0= &quot;&lt;&lt;Z0&lt;&lt;&quot; Z1= &quot;&lt;&lt;Z1&lt;&lt;&quot; Z2= &quot;&lt;&lt;Z2&lt;&lt;&quot; Z3= &quot;&lt;&lt;Z3&lt;&lt;&quot; Z4= &quot;&lt;&lt;Z4&lt;&lt;&quot; Z5= &quot;&lt;&lt;Z5&lt;&lt;&quot; Z6= &quot;&lt;&lt;Z6&lt;&lt;&quot; Z7= &quot;&lt;&lt;Z7&lt;&lt;&quot; Z8= &quot;&lt;&lt;Z8&lt;&lt;&quot; Z9= &quot;&lt;&lt;Z9&lt;&lt;&quot;\n\n&quot;;
	}
	}
	}
		cout&lt;&lt; endl;
    return 0;
}
</code></pre>
<p>@Pumukl Ich seh zwar keinen Unterschied, aber ich habe eben wirklich auf C++ getippt und nicht auf Code</p>
<p>Das Makro funktioniert bis auf die letzte Funktion. Es soll untersuchen, ob die Ziffern in den Qubikzahlen alle gleichmäßig verteilt sind. Dazu zählt es die Ziffern alle, und soll sie dann auch noch sortiert zählen, was *i bei Zeile 42 leistet.</p>
<p>Siehe &quot;verrückte Stelle&quot; Z 42. Ich versuche dort aus dem String p wieder eine Ziffernfolge zu machen. Die brauch ich für die nachfolgenden if. Warum geht *i nicht?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2045748</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2045748</guid><dc:creator><![CDATA[Bekell]]></dc:creator><pubDate>Wed, 06 Apr 2011 21:15:54 GMT</pubDate></item><item><title><![CDATA[Reply to printf on Wed, 06 Apr 2011 21:45:29 GMT]]></title><description><![CDATA[<p>Oh jeh ... Da ist soviel falsch ich weiß nicht wo ich beginnen soll.</p>
<p>1. Arrays verwenden?</p>
<pre><code class="language-cpp">int Z0;
int Z1;
int Z2;
int Z3;
int Z4;
int Z5;
int Z6;
int Z7;
int Z8;
int Z9;
</code></pre>
<p>wird zu</p>
<pre><code class="language-cpp">int Z[10];
</code></pre>
<p>2. Warum deklarierst du Variablen zwei mal?</p>
<pre><code class="language-cpp">int Z0=Z0+1; //falsch muss:
Z0=Z0+1; //sein.
</code></pre>
<p>genau so an anderen Stellen auch...<br />
3. Anstelle des if krams kann man mit einem Array dann einfach:</p>
<pre><code class="language-cpp">++Z[meineZahl];
</code></pre>
<p>schreiben.<br />
4. Du musst Gesamtziffernzähler und die Startwerte auf 0 setzen, da diese nicht initialisiert sind.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt; 
#include &lt;string&gt; 
#include &lt;sstream&gt; 

using namespace std; 

int main () { 
	int gesamtziffernzaehler = 0; 
	int Z[10] = {0}; 

    for(int a = 0 ; a &lt; 5; ++a) { 
		int kubik = a*a*a; 

		ostringstream outStream; 
		outStream &lt;&lt; kubik;
		string p = outStream.str();

		gesamtziffernzaehler += p.length();

		cout &lt;&lt; &quot;\nSchleife: &quot; &lt;&lt; a+1 &lt;&lt; &quot;\n&quot;
		     &lt;&lt; a &lt;&lt; &quot; Qubik = &quot; &lt;&lt; kubik &lt;&lt;&quot;\n&quot; 
		     &lt;&lt; a &lt;&lt;&quot; Qubik hat &quot; &lt;&lt;p.length() &lt;&lt; &quot; Ziffern&quot; 
		     &lt;&lt; &quot;\ngzz = &quot; &lt;&lt; gesamtziffernzaehler &lt;&lt; &quot;\n&quot;;

		for (string::iterator i = p.begin(); i != p.end(); ++i) {
			if ((*i - '0') &lt;= 9 &amp;&amp; (*i - '0') &gt;= 0) 
				++Z[(*i - '0')]; 

			cout &lt;&lt; &quot;\n&quot;;
			for(int i = 0; i &lt; 10; ++i)
				cout&lt;&lt;&quot;Z&quot; &lt;&lt; i &lt;&lt; &quot;=&quot;&lt;&lt; Z[i] &lt;&lt; &quot;\n&quot;; 
		} 
    }
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2045750</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2045750</guid><dc:creator><![CDATA[HighLigerBiMBam]]></dc:creator><pubDate>Wed, 06 Apr 2011 21:45:29 GMT</pubDate></item><item><title><![CDATA[Reply to printf on Wed, 06 Apr 2011 21:27:09 GMT]]></title><description><![CDATA[<p>Ein String-Stream erwartet nunmal einen String als Konstruktor-Parameter, da reicht kein einzelner char. Und an der Stelle ist er auch nicht nötig, wenn du nur die einzelne Zieffer umrechnen willst: <code>meineZahl=*i - '0'</code></p>
<p>PS: Diese if-Serie ab Zeile 48 ist unsinnig - ich hätte die Z*-Variablen zu einem Array zusammengefasst und dann mit <code>++Ziffern[meineZahl];</code> erhöht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2045753</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2045753</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Wed, 06 Apr 2011 21:27:09 GMT</pubDate></item><item><title><![CDATA[Reply to printf on Thu, 07 Apr 2011 00:32:22 GMT]]></title><description><![CDATA[<p>Wo wir schon dabei sind, was soll das ganze Gesumse mit Strings überhaupt? Ich schlage Folgendes vor:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

int main() {
  unsigned Z[10] = {};
  unsigned ziffern = 0;

  for(unsigned a = 1; a &lt;= 1000; ++a) {
    for(unsigned long kubik = a * a * a; kubik != 0; kubik /= 10) {
      ++Z[kubik % 10];
      ++ziffern;
    }
  }

  std::cout &lt;&lt; &quot;ggz = &quot; &lt;&lt; ziffern &lt;&lt; '\n';

  for(unsigned i = 0; i &lt; 10; ++i) {
    std::cout &lt;&lt; 'Z' &lt;&lt; i &lt;&lt; &quot; = &quot; &lt;&lt; Z[i] &lt;&lt; '\n';
  }
}
</code></pre>
<p>Übrigens muss man aufpassen, dass kubik nicht überläuft. Auf 32-Bit-Maschinen kommt man mit unsigned long nicht weiter als 1625<sup>3</sup>, auf 64-Bit-Maschinen bis 2.642.245<sup>3</sup>. Alles, was darüber hinausgeht, braucht komplexere Mathematik oder <a href="http://www.gmplib.org/" rel="nofollow">entsprechende Bibliotheken</a>.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2045782</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2045782</guid><dc:creator><![CDATA[seldon]]></dc:creator><pubDate>Thu, 07 Apr 2011 00:32:22 GMT</pubDate></item><item><title><![CDATA[Reply to printf on Thu, 07 Apr 2011 05:22:16 GMT]]></title><description><![CDATA[<p>Danke, HeighLigerBiMBam, mit Arrays hatte ich nicht nicht gearbeitet. Das andere kann ich nachvollziehen, bis auf den If Ausdruch in Zeile 26/27. Wenn Du den noch mal erläuterst, bete ich heut ein Gesetz für Dich.</p>
<p>Danke</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2045789</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2045789</guid><dc:creator><![CDATA[Bekell]]></dc:creator><pubDate>Thu, 07 Apr 2011 05:22:16 GMT</pubDate></item><item><title><![CDATA[Reply to printf on Thu, 07 Apr 2011 05:39:47 GMT]]></title><description><![CDATA[<p>@HeighLigerBimBam</p>
<p>Irgendwo hast Du einen Fehler drin, denn das Programm schreibt die letzte Zeile p mal.</p>
<p>Entweder man nimmt das raus, oder man sortiert jetzt, indem man noch zählen läßt, wie oft jede Ziffer an der 1.,der 2. usw. Stelle kommt.</p>
<p>Danke</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2045791</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2045791</guid><dc:creator><![CDATA[Bekell]]></dc:creator><pubDate>Thu, 07 Apr 2011 05:39:47 GMT</pubDate></item><item><title><![CDATA[Reply to printf on Thu, 07 Apr 2011 10:55:47 GMT]]></title><description><![CDATA[<p>@Seldon</p>
<p>Danke für das Teil. Bei den 1 - 9-stelligen Qubikzahlen zählt es richtig.</p>
<p>Aber im Bereich der zehnstelligen Qubikzahlen zählt es die Ziffern der Qubikzahle <strong>definitiv</strong> falsch.</p>
<p>Die letzte neunstellige QubikZahl ist 999^3. Der nächste Bereich, der zehnstellige, beginnt mit 1000^3 und endet mit 2154^3. Das sind 1155 Zahlen mit 11550 Ziffern. (Das kann ich grad noch im Kopf rechnen.) Der Tool zählt aber nur 11326. Es fehlen mithin 224 Ziffern. Wo sind die geblieben?</p>
<blockquote>
<p>[Session started at 2011-04-07 12:39:14 +0200.]<br />
ggz = 11326<br />
...Z0 = 1207<br />
...Z1 = 1422<br />
...Z2 = 1358<br />
...Z3 = 1248<br />
...Z4 = 1065<br />
...Z5 = 973<br />
...Z6 = 1006<br />
...Z7 = 1103<br />
...Z8 = 989<br />
...Z9 = 955</p>
<p>BequemerQZz has exited with status 0.</p>
</blockquote>
]]></description><link>https://www.c-plusplus.net/forum/post/2045920</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2045920</guid><dc:creator><![CDATA[Bekell]]></dc:creator><pubDate>Thu, 07 Apr 2011 10:55:47 GMT</pubDate></item><item><title><![CDATA[Reply to printf on Thu, 07 Apr 2011 12:12:52 GMT]]></title><description><![CDATA[<p>Bekell schrieb:</p>
<blockquote>
<p>@HeighLigerBimBam</p>
<p>Irgendwo hast Du einen Fehler drin, denn das Programm schreibt die letzte Zeile p mal.</p>
<p>Entweder man nimmt das raus, oder man sortiert jetzt, indem man noch zählen läßt, wie oft jede Ziffer an der 1.,der 2. usw. Stelle kommt.</p>
<p>Danke</p>
</blockquote>
<p>Dann nimm doch einfach die Ausgabe und schreib sie außerhalb der string::iterator-Schleife. War eh dämlich die selbe Variable in beiden Schleifen zu verwenden.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt; 
#include &lt;string&gt; 
#include &lt;sstream&gt; 

using namespace std; 

int main () { 
    int gesamtziffernzaehler = 0; 
    int Z[10] = {0}; 

    for(int a = 0 ; a &lt; 5; ++a) { 
        int kubik = a*a*a; 

        ostringstream outStream; 
        outStream &lt;&lt; kubik;
        string p = outStream.str();

        gesamtziffernzaehler += p.length();

        cout &lt;&lt; &quot;\nSchleife: &quot; &lt;&lt; a+1 &lt;&lt; &quot;\n&quot;
             &lt;&lt; a &lt;&lt; &quot; Qubik = &quot; &lt;&lt; kubik &lt;&lt;&quot;\n&quot; 
             &lt;&lt; a &lt;&lt;&quot; Qubik hat &quot; &lt;&lt;p.length() &lt;&lt; &quot; Ziffern&quot; 
             &lt;&lt; &quot;\ngzz = &quot; &lt;&lt; gesamtziffernzaehler &lt;&lt; &quot;\n\n&quot;;

        for (string::iterator i = p.begin(); i != p.end(); ++i)
            if ((*i - '0') &lt;= 9 &amp;&amp; (*i - '0') &gt;= 0) 
                ++Z[(*i - '0')];

        for(int i = 0; i &lt; 10; ++i)
            cout&lt;&lt;&quot;Z&quot; &lt;&lt; i &lt;&lt; &quot;=&quot;&lt;&lt; Z[i] &lt;&lt; &quot;\n&quot;; 
    }
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2045976</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2045976</guid><dc:creator><![CDATA[HighLigerBiMBam]]></dc:creator><pubDate>Thu, 07 Apr 2011 12:12:52 GMT</pubDate></item><item><title><![CDATA[Reply to printf on Thu, 07 Apr 2011 13:29:37 GMT]]></title><description><![CDATA[<p>Bekell schrieb:</p>
<blockquote>
<p>Aber im Bereich der zehnstelligen Qubikzahlen zählt es die Ziffern der Qubikzahle <strong>definitiv</strong> falsch.</p>
<p>Die letzte neunstellige QubikZahl ist 999^3. Der nächste Bereich, der zehnstellige, beginnt mit 1000^3 und endet mit 2154^3.</p>
</blockquote>
<p>seldon schrieb:</p>
<blockquote>
<p>Übrigens muss man aufpassen, dass kubik nicht überläuft. <strong>Auf 32-Bit-Maschinen kommt man mit unsigned long nicht weiter als 1625<sup>3</sup></strong>, auf 64-Bit-Maschinen bis 2.642.245<sup>3</sup>. Alles, was darüber hinausgeht, braucht komplexere Mathematik oder entsprechende Bibliotheken.</p>
</blockquote>
<p>Das Problem hast du aber auch, wenn du die ausgerechnete Zahl in einen String stopfst - die ist da nämlich auch schon übergelaufen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2046036</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2046036</guid><dc:creator><![CDATA[seldon]]></dc:creator><pubDate>Thu, 07 Apr 2011 13:29:37 GMT</pubDate></item><item><title><![CDATA[Reply to printf on Thu, 07 Apr 2011 13:44:26 GMT]]></title><description><![CDATA[<blockquote>
<p>Übrigens muss man aufpassen, dass kubik nicht überläuft. <strong>Auf 32-Bit-Maschinen kommt man mit unsigned long nicht weiter als 1625<sup>3</sup></strong>, auf 64-Bit-Maschinen bis 2.642.245<sup>3</sup>. Alles, was darüber hinausgeht, braucht komplexere Mathematik oder entsprechende Bibliotheken.</p>
<p>Das Problem hast du aber auch, wenn du die ausgerechnete Zahl in einen String stopfst - die ist da nämlich auch schon übergelaufen.</p>
</blockquote>
<p>Ich hab ein Imac 2005, wo kann ich sehen, wie breit der Bus ist.</p>
<p>Also auf meinem Programm Magic Number Maschine kann ich bis 30 Stellen rechnen. Aber ich wollte jetzt keine Applescript-Application schreiben oder die Sache über Automator lösen. Ist der neue Imac 64-Bit mäßig construiert?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2046043</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2046043</guid><dc:creator><![CDATA[Bekell]]></dc:creator><pubDate>Thu, 07 Apr 2011 13:44:26 GMT</pubDate></item><item><title><![CDATA[Reply to printf on Thu, 07 Apr 2011 15:14:11 GMT]]></title><description><![CDATA[<p>&quot;Magic Number Machine&quot; wird vermutlich long doubles benutzen. Bei Fließkommazahlen funktioniert die ganze Kiste etwas anders.</p>
<p>Wie breit Typen in Byte sind, kriegst du mit sizeof raus:</p>
<pre><code class="language-cpp">std::cout &lt;&lt; &quot;int : &quot; &lt;&lt; sizeof(int ) &lt;&lt; &quot; Byte\n&quot;
             &quot;long: &quot; &lt;&lt; sizeof(long) &lt;&lt; &quot; Byte\n&quot;;
</code></pre>
<p>Womöglich kennt dein Compiler unsigned long long - der ist zwar im C++-Standard bisher nicht enthalten, wird aber von vielen Compilern unterstützt und ist mindestens 64 Bit (also 8 Byte) breit. Das sähe dann so aus:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;

int main() {
  unsigned Z[10] = {};
  unsigned ziffern = 0;

  // a muss auch 64 Bit breit sein, sonst läuft a * a * a vor der Zuweisung zu
  // kubik über. Das ist auch ein möglicher Fehler in meinem ersten Codestück.
  for(unsigned long long a = 1000; a &lt;= 2154; ++a) {
    for(unsigned long long kubik = a * a * a; kubik != 0; kubik /= 10) {
      ++Z[kubik % 10];
      ++ziffern;
    }
  }

  std::cout &lt;&lt; &quot;ggz = &quot; &lt;&lt; ziffern &lt;&lt; '\n';

  for(unsigned i = 0; i &lt; 10; ++i) {
    std::cout &lt;&lt; 'Z' &lt;&lt; i &lt;&lt; &quot; = &quot; &lt;&lt; Z[i] &lt;&lt; '\n';
  }
}
</code></pre>
<p>Mit der Bauart von Apple-Computern kenne ich mich nicht aus. Wie breit der Bus ist, spielt dabei auch keine Rolle (es geht um die Registerbreite der CPU und ob das installierte Betriebssystem und der installierte Compiler in der Lage sind, diese zu nutzen).</p>
<p>Ansonsten könntest du einen Blick auf die gmplib werfen, die ich oben schon verlinkt hatte, oder Boost zur Hand nehmen und kucken, ob boost/cstdint.hpp deinem Compiler einen uint64_t entlocken kann.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2046077</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2046077</guid><dc:creator><![CDATA[seldon]]></dc:creator><pubDate>Thu, 07 Apr 2011 15:14:11 GMT</pubDate></item></channel></rss>