<?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[Zahl eingeben und diese in einzelne Ziffern zerlegen]]></title><description><![CDATA[<p>Hallo an alle die mir helfen könnten.</p>
<p>Ich habe gerade ein kleines Problemchen, undzwar: Ich möchte in meinem Programm das so machen dass mich dieses nach einer Zahl frägt und ich diese eingeben und das Programm mir diese Zahl in einzelne Ziffern zerlegt und abspeichert.</p>
<p>Bisher war mein Ansatz so:</p>
<pre><code class="language-cpp">int zahl;
	int feld[5];

	cin&gt;&gt;zahl;
	int i=0;
	while(zahl){
	//for(int i=0; i&lt;=5; i++){

		feld[i]=zahl%10;

		zahl/=10;
		//cout&lt;&lt;feld[i] &lt;&lt;endl;
		i++;}

	//Ausgabe der umgekehrten Zahlen
	cout&lt;&lt;feld[4] &lt;&lt;endl;
	cout&lt;&lt;feld[3] &lt;&lt;endl;
	cout&lt;&lt;feld[2] &lt;&lt;endl;
	cout&lt;&lt;feld[1] &lt;&lt;endl;
	cout&lt;&lt;feld[0] &lt;&lt;endl;
</code></pre>
<p>Das funktioniert zwar, aber nur wenn diese Zahlen nicht mit einer &quot;0&quot; beginnen, ich brauche das aber so dass es auf jeden Fall 5 stellen sind, deswegen auch ein Feld der Länge 5...</p>
<p>oder diese Lösung, aber hier ist das Problem dass die Werte in einem char abgespeichert werden, und ich nicht mit denen weiterrechnen kann:</p>
<pre><code class="language-cpp">int main()
{
	string str;
	cin&gt;&gt;str;
//string str = &quot;hello world&quot;;
for( int i = 0; i &lt; str.size(); i++ ){
char c = str[i];
cout&lt;&lt;str[i]&lt;&lt;endl;
}
</code></pre>
<p>Gibt es vielleicht eine Lösung die funktioniert wenn auch nuller am anfang stehen?</p>
<p>Vielen Dank im Voraus.</p>
<p>NexXxuS</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/276119/zahl-eingeben-und-diese-in-einzelne-ziffern-zerlegen</link><generator>RSS for Node</generator><lastBuildDate>Wed, 26 Aug 2026 13:30:42 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/276119.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 27 Oct 2010 11:00:12 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Zahl eingeben und diese in einzelne Ziffern zerlegen on Wed, 27 Oct 2010 11:22:59 GMT]]></title><description><![CDATA[<p>Hallo an alle die mir helfen könnten.</p>
<p>Ich habe gerade ein kleines Problemchen, undzwar: Ich möchte in meinem Programm das so machen dass mich dieses nach einer Zahl frägt und ich diese eingeben und das Programm mir diese Zahl in einzelne Ziffern zerlegt und abspeichert.</p>
<p>Bisher war mein Ansatz so:</p>
<pre><code class="language-cpp">int zahl;
	int feld[5];

	cin&gt;&gt;zahl;
	int i=0;
	while(zahl){
	//for(int i=0; i&lt;=5; i++){

		feld[i]=zahl%10;

		zahl/=10;
		//cout&lt;&lt;feld[i] &lt;&lt;endl;
		i++;}

	//Ausgabe der umgekehrten Zahlen
	cout&lt;&lt;feld[4] &lt;&lt;endl;
	cout&lt;&lt;feld[3] &lt;&lt;endl;
	cout&lt;&lt;feld[2] &lt;&lt;endl;
	cout&lt;&lt;feld[1] &lt;&lt;endl;
	cout&lt;&lt;feld[0] &lt;&lt;endl;
</code></pre>
<p>Das funktioniert zwar, aber nur wenn diese Zahlen nicht mit einer &quot;0&quot; beginnen, ich brauche das aber so dass es auf jeden Fall 5 stellen sind, deswegen auch ein Feld der Länge 5...</p>
<p>oder diese Lösung, aber hier ist das Problem dass die Werte in einem char abgespeichert werden, und ich nicht mit denen weiterrechnen kann:</p>
<pre><code class="language-cpp">int main()
{
	string str;
	cin&gt;&gt;str;
//string str = &quot;hello world&quot;;
for( int i = 0; i &lt; str.size(); i++ ){
char c = str[i];
cout&lt;&lt;str[i]&lt;&lt;endl;
}
</code></pre>
<p>Gibt es vielleicht eine Lösung die funktioniert wenn auch nuller am anfang stehen?</p>
<p>Vielen Dank im Voraus.</p>
<p>NexXxuS</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1971357</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1971357</guid><dc:creator><![CDATA[NexXxuS]]></dc:creator><pubDate>Wed, 27 Oct 2010 11:22:59 GMT</pubDate></item><item><title><![CDATA[Reply to Zahl eingeben und diese in einzelne Ziffern zerlegen on Wed, 27 Oct 2010 11:15:37 GMT]]></title><description><![CDATA[<p>Nach der Deklaration von feld:</p>
<pre><code class="language-cpp">memset(feld,0,sizeof(feld));
</code></pre>
<p>Durch das <code>cin&gt;&gt;zahl;</code> werden ja schon führende Nullen &quot;gestrichen&quot;.<br />
Oder einfach</p>
<pre><code class="language-cpp">int feld[5] = {0};
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1971362</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1971362</guid><dc:creator><![CDATA[Vicious Falcon]]></dc:creator><pubDate>Wed, 27 Oct 2010 11:15:37 GMT</pubDate></item><item><title><![CDATA[Reply to Zahl eingeben und diese in einzelne Ziffern zerlegen on Wed, 27 Oct 2010 11:20:04 GMT]]></title><description><![CDATA[<p>bestimmt nicht gut aber es tut das, das du willst.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
using namespace std;

int main ()
{
    int feld[5] = {};

    int zahl;
    cin &gt;&gt; zahl;

    if(zahl &gt; 99999)
    {
        //misst
    }

    int plus = 0;
    if(zahl &lt;= 9999)
    {
        ++plus;
    }

    int i = 0 + plus;
    while(zahl)
    {
        feld[i]=zahl%10;
        zahl/=10;
        i++;
    }
    cout&lt;&lt;feld[4] &lt;&lt;endl;
    cout&lt;&lt;feld[3] &lt;&lt;endl;
    cout&lt;&lt;feld[2] &lt;&lt;endl;
    cout&lt;&lt;feld[1] &lt;&lt;endl;
    cout&lt;&lt;feld[0] &lt;&lt;endl;

    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1971365</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1971365</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Wed, 27 Oct 2010 11:20:04 GMT</pubDate></item><item><title><![CDATA[Reply to Zahl eingeben und diese in einzelne Ziffern zerlegen on Wed, 27 Oct 2010 11:39:07 GMT]]></title><description><![CDATA[<p>@ Dweb</p>
<p>danke für deine Antwort, genau das was ich gesucht habe, nur verstehe ich nicht ganz wieso wenn ich eine führende 0 habe diese dann am Ende ausgegeben wird, habe ich aber 00 am anfang wird eine oben und die andere am ende ausgegeben also feld[4] und feld[0]...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1971379</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1971379</guid><dc:creator><![CDATA[NexXxuS]]></dc:creator><pubDate>Wed, 27 Oct 2010 11:39:07 GMT</pubDate></item><item><title><![CDATA[Reply to Zahl eingeben und diese in einzelne Ziffern zerlegen on Wed, 27 Oct 2010 11:55:40 GMT]]></title><description><![CDATA[<p>also erstmal das hier:</p>
<pre><code class="language-cpp">/*
        Wenn du in {} zu wenige Elemente angibst, werden die restlichen auto.
        mit 0 initialisiert, d.h.:
        {0} und {0,0} und {} etc. --&gt; alles das Selbe.
    */
    int feld[5] = {}; // alle Elemente sind also nun 0.

    int zahl;
    cin &gt;&gt; zahl; // wenn du 00000123 eingbist, werden alle führenden 0 nicht beachtet.
    cout &lt;&lt; zahl; // 123
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1971391</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1971391</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Wed, 27 Oct 2010 11:55:40 GMT</pubDate></item><item><title><![CDATA[Reply to Zahl eingeben und diese in einzelne Ziffern zerlegen on Wed, 27 Oct 2010 12:08:55 GMT]]></title><description><![CDATA[<p>wolltest du genau das hier erreichen ?</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
using namespace std;

int main()
{
    //========================
    int feld[5] = {};
    int i = 0;
    int zahl = 0;
    cin &gt;&gt; zahl;
    //========================

    int laenge = 0;
    int zahl_save = zahl;
    do
    {
        ++laenge;
    }
    while(zahl_save /= 10);	
    //========================

    if(zahl &gt; 99999)
    {
        // misst
    }
    else
    {
        i = 5-laenge;
    }
    //========================

    while(zahl)
    {
        feld[i]=zahl%10;
        zahl/=10;
        i++;
    }
    //========================

    cout&lt;&lt;feld[4] &lt;&lt;endl;
    cout&lt;&lt;feld[3] &lt;&lt;endl;
    cout&lt;&lt;feld[2] &lt;&lt;endl;
    cout&lt;&lt;feld[1] &lt;&lt;endl;
    cout&lt;&lt;feld[0] &lt;&lt;endl;
    //========================

    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1971406</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1971406</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Wed, 27 Oct 2010 12:08:55 GMT</pubDate></item><item><title><![CDATA[Reply to Zahl eingeben und diese in einzelne Ziffern zerlegen on Wed, 27 Oct 2010 12:32:41 GMT]]></title><description><![CDATA[<p>Vielen Dank jetzt funktioniert es... Die Funktion mit ++plus war irgenwie überflüssig.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1971416</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1971416</guid><dc:creator><![CDATA[NexXxuS]]></dc:creator><pubDate>Wed, 27 Oct 2010 12:32:41 GMT</pubDate></item><item><title><![CDATA[Reply to Zahl eingeben und diese in einzelne Ziffern zerlegen on Wed, 27 Oct 2010 12:39:03 GMT]]></title><description><![CDATA[<p>Wie wärs damit:</p>
<pre><code class="language-cpp">bool testeObZahl(std::string vielleichtZahl)
{
  //testen obs ne Zahl ist
  std::stringstream sstr(vielleichtZahl);
  int testZahl;
  sstr &gt;&gt; testZahl;
  return sstr;
}

void zerlegeRueckwaerts(std::string str)
{
  for (std::string::reverse_iterator it = str.rbegin(); it != str.rend(); ++it)
  {
    std::cout &lt;&lt; *it &lt;&lt; std::endl;
  }
}

void zerlegeZahl()
{  
  std::string vielleichtNeZahl;
  do
  {
    std::cin &gt;&gt; vielleichtNeZahl;
  } while (!testeObZahl(vielleichtNeZahl));

  zerlegeRueckwaerts(vielleichtNeZahl);
}
</code></pre>
<p>Wenn was zeichenweise zerlegt werden soll, sollte man es auch zeichenweise bearbeiten - als String nämlich.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1971428</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1971428</guid><dc:creator><![CDATA[pumuckl]]></dc:creator><pubDate>Wed, 27 Oct 2010 12:39:03 GMT</pubDate></item><item><title><![CDATA[Reply to Zahl eingeben und diese in einzelne Ziffern zerlegen on Wed, 27 Oct 2010 12:49:26 GMT]]></title><description><![CDATA[<p>Man kann es mit deinem zweiten Ansatz machen. Nur finde ich es mit static_cast und new/del fraglich. Sicher kann man mit dem Int-Array nun auch rechnen. Die Benutzereingabe solltest du aber vorher streng kontrollieren.</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;string&gt;

using namespace std;

int main()
{
    string str; 
    cin &gt;&gt; str; 

	int *zahlen = new int [str.size()];

	for( int i = 0; i &lt; str.size(); i++ ){ 
		char c = str[i];
		zahlen[i] = static_cast&lt;int&gt; (c) - 48;
		cout &lt;&lt; zahlen[i] &lt;&lt; endl;
		}

	delete[] zahlen;
	cin.get();
	cin.get();

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1971436</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1971436</guid><dc:creator><![CDATA[HighLigerBiMBam]]></dc:creator><pubDate>Wed, 27 Oct 2010 12:49:26 GMT</pubDate></item><item><title><![CDATA[Reply to Zahl eingeben und diese in einzelne Ziffern zerlegen on Wed, 27 Oct 2010 15:54:51 GMT]]></title><description><![CDATA[<p>HighLigerBiMBam schrieb:</p>
<blockquote>
<pre><code class="language-cpp">char c = str[i];
zahlen[i] = static_cast&lt;int&gt; (c) - 48;
</code></pre>
</blockquote>
<p>Überlaufen kann das nicht (Grund für int-cast) und was 48 bedeutet weiss nicht jeder (ist auch nicht immer gleich). Deshalb gleich:</p>
<pre><code class="language-cpp">zahlen[i] = str[i] - '0';
</code></pre>
<p>Klarer und kürzer.</p>
<p>warum nicht so?</p>
<pre><code class="language-cpp">int char_to_int(char c) { return c - '0'; }
std::vector&lt;int&gt; zerlege_rueckwaerts(int i)
{
  std::stringstream s;
  s &lt;&lt; i;
  std::string str;
  s &gt;&gt; str;
  std::vector&lt;int&gt; vec(str.size());
  std::transform(str.begin()+str.find_first_not_of('0'), str.end(), vec.begin(), char_to_int);
  return vec;
}

void zerlege_zahl()
{  
  int ganz_sicher_ne_zahl;
  while (!(std::cin &gt;&gt; ganz_sicher_ne_zahl));
  std::vector&lt;int&gt; feld = zerlege_rueckwaerts(ganz_sicher_ne_zahl);

  std::cout &lt;&lt; feld[0] &lt;&lt; std::endl  // das
            &lt;&lt; feld[1] &lt;&lt; std::endl  // ist
            &lt;&lt; feld[2] &lt;&lt; std::endl  // natürlich
            &lt;&lt; feld[3] &lt;&lt; std::endl  // vollkommener
            &lt;&lt; feld[4] &lt;&lt; std::endl; // Unsinn
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1971570</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1971570</guid><dc:creator><![CDATA[nocast]]></dc:creator><pubDate>Wed, 27 Oct 2010 15:54:51 GMT</pubDate></item><item><title><![CDATA[Reply to Zahl eingeben und diese in einzelne Ziffern zerlegen on Wed, 27 Oct 2010 15:56:46 GMT]]></title><description><![CDATA[<p>Ups</p>
<pre><code class="language-cpp">std::transform(str.begin()+str.find_first_not_of('0'), str.end(), vec.begin(), char_to_int);
</code></pre>
<p>Das komische find_first_of_not kann man sich sparen, lieber so:</p>
<pre><code class="language-cpp">std::transform(str.begin(), str.end(), vec.begin(), char_to_int);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1971571</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1971571</guid><dc:creator><![CDATA[nocast]]></dc:creator><pubDate>Wed, 27 Oct 2010 15:56:46 GMT</pubDate></item></channel></rss>