<?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[Array erzeugt Programmabsturz]]></title><description><![CDATA[<p>Hallo</p>
<p>Ich habe mal vor langem C++ &quot;gelernt&quot; und wollte nun ein kleines Poker-Programm schreiben. Aber irgendwie fehlts bei mir noch an den Basics...</p>
<p>Ich kann bei folgendem Programm, welches für eine beliebige anzahl Spieler Karten austeilen sollte, keinen Fehler mehr finden.<br />
Das Programm stürzt beim Ausführen mit einer Windows-Fehlermeldung ab, compiliert wird es einwandfre. Anscheinend verträgt es die Ausgabe über ein Array nicht, aber keinen Plan wiso:</p>
<pre><code class="language-cpp">//cout &lt;&lt; conflicts &lt;&lt; &quot;Konflikte&quot; &lt;&lt; endl;
    for (int i=0; i&lt;players; i++) {
        cout &lt;&lt; &quot;Spieler &quot; &lt;&lt; i+1 &lt;&lt; &quot;: &quot; &lt;&lt; cards[ hand[i][0][0] ][ hand[i][0][1] ];
        cout &lt;&lt; &quot; &quot; &lt;&lt; cards[ hand[i][1][0] ][ hand[i][1][1] ] &lt;&lt; endl;
    }
</code></pre>
<p>Da ich nicht weis, ob das ein Logikfehler ist oder nicht gebe ich mal den ganzen Code an. Zuerst definiere ich die möglichen Karten, danach wird per Zufall eine Karte gezogen und kontrolliert, ob diese nicht schon vergeben ist. Danach werden in einem anderen Array mit allen Spieler-Händen die beiden Karten eingetragen und am Schluss ausgegeben. Der Code könnte auch logisch nicht funktionieren, aber mir geht es darum das Ganze mal compilieren zu können.</p>
<pre><code class="language-cpp">#include &lt;cstdlib&gt;
#include &lt;iostream&gt;

using namespace std;

// Kartendeck besteht aus 4x13 = 52 Karten
const char cards[13][4][3] = {  { &quot;Ah&quot;, &quot;As&quot;, &quot;Ap&quot;, &quot;Ak&quot; },
                                { &quot;Kh&quot;, &quot;Ks&quot;, &quot;Kp&quot;, &quot;Kk&quot; },
                                { &quot;Qh&quot;, &quot;Qs&quot;, &quot;Qp&quot;, &quot;Qk&quot; },
                                { &quot;Jh&quot;, &quot;Js&quot;, &quot;Jp&quot;, &quot;Jk&quot; },
                                { &quot;1h&quot;, &quot;1s&quot;, &quot;1p&quot;, &quot;1k&quot; },
                                { &quot;9h&quot;, &quot;9s&quot;, &quot;9p&quot;, &quot;9k&quot; },
                                { &quot;8h&quot;, &quot;8s&quot;, &quot;8p&quot;, &quot;8k&quot; },
                                { &quot;7h&quot;, &quot;7s&quot;, &quot;7p&quot;, &quot;7k&quot; },
                                { &quot;6h&quot;, &quot;6s&quot;, &quot;6p&quot;, &quot;6k&quot; },
                                { &quot;5h&quot;, &quot;5s&quot;, &quot;5p&quot;, &quot;5k&quot; },
                                { &quot;4h&quot;, &quot;4s&quot;, &quot;4p&quot;, &quot;4k&quot; },
                                { &quot;3h&quot;, &quot;3s&quot;, &quot;3p&quot;, &quot;3k&quot; },
                                { &quot;2h&quot;, &quot;2s&quot;, &quot;2p&quot;, &quot;2k&quot; }};

int main(int argc, char *argv[])
{
    int players, conflicts;                // Anzahl Spieler
    bool carddeck[13][4];                  // Ausgeteilte Karten
    bool unique = false;                   // Verifizierungsvariable

    // Programm
    cout &lt;&lt; &quot;Anzahl Spieler: &quot;;
    cin &gt;&gt; players;
    int hand[players-1][2][2];               // [Spieler 0 bis x][Karte 0 oder 1][Wert oder Art]

    srand((unsigned)time(NULL));

    // carddeck reseten
    for (int i=0; i&lt;13; i++) {
        for (int j=0; j&lt;4; j++) {
            carddeck[i][j] = false;
        }
    }    

    conflicts = 0;

    //Karten austeilen
    for (int i=0; i&lt;players; i++) {                         // Spieler durchlaufen
        for (int j=0; j&lt;2; j++ ) {                          // 2 Karen ziehen
            while (!unique) {                                     // Solange bis eine gültige Karte gezogen wurde
                  unique = true;
                  hand[i][j][0] = rand()%13;                      // Kartenwert ziehen
                  hand[i][j][1] = rand()%4;                       // Kartenart ziehen
                  if (carddeck[ hand[i][j][0] ][ hand[i][j][1] ]) {            // Kontrolle ob Karte bereits vergeben ist
                     unique = false;                                           // Ja
                     conflicts++; }                                         
                  else { carddeck[ hand[i][j][0] ][ hand[i][j][1] ] = true; }   // Nein =&gt; eintragen
            }
        }
    }

    //cout &lt;&lt; conflicts &lt;&lt; &quot;Konflikte&quot; &lt;&lt; endl;
    for (int i=0; i&lt;players; i++) {
        cout &lt;&lt; &quot;Spieler &quot; &lt;&lt; i+1 &lt;&lt; &quot;: &quot; &lt;&lt; cards[ hand[i][0][0] ][ hand[i][0][1] ];
        cout &lt;&lt; &quot; &quot; &lt;&lt; cards[ hand[i][1][0] ][ hand[i][1][1] ] &lt;&lt; endl;
    }

    system(&quot;PAUSE&quot;);
    return EXIT_SUCCESS;
}
</code></pre>
<p>Ich lasse mich auch gerne belehren, wie man solche fehler selber analysieren kann (mit Dev-C++)...</p>
<p>Besten Dank für deine Aufmerksamkeit <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>
<p>greez gieraffe</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/140071/array-erzeugt-programmabsturz</link><generator>RSS for Node</generator><lastBuildDate>Sun, 30 Aug 2026 21:16:40 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/140071.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 11 Mar 2006 17:57:01 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Array erzeugt Programmabsturz on Sat, 11 Mar 2006 18:04:26 GMT]]></title><description><![CDATA[<p>Hallo</p>
<p>Ich habe mal vor langem C++ &quot;gelernt&quot; und wollte nun ein kleines Poker-Programm schreiben. Aber irgendwie fehlts bei mir noch an den Basics...</p>
<p>Ich kann bei folgendem Programm, welches für eine beliebige anzahl Spieler Karten austeilen sollte, keinen Fehler mehr finden.<br />
Das Programm stürzt beim Ausführen mit einer Windows-Fehlermeldung ab, compiliert wird es einwandfre. Anscheinend verträgt es die Ausgabe über ein Array nicht, aber keinen Plan wiso:</p>
<pre><code class="language-cpp">//cout &lt;&lt; conflicts &lt;&lt; &quot;Konflikte&quot; &lt;&lt; endl;
    for (int i=0; i&lt;players; i++) {
        cout &lt;&lt; &quot;Spieler &quot; &lt;&lt; i+1 &lt;&lt; &quot;: &quot; &lt;&lt; cards[ hand[i][0][0] ][ hand[i][0][1] ];
        cout &lt;&lt; &quot; &quot; &lt;&lt; cards[ hand[i][1][0] ][ hand[i][1][1] ] &lt;&lt; endl;
    }
</code></pre>
<p>Da ich nicht weis, ob das ein Logikfehler ist oder nicht gebe ich mal den ganzen Code an. Zuerst definiere ich die möglichen Karten, danach wird per Zufall eine Karte gezogen und kontrolliert, ob diese nicht schon vergeben ist. Danach werden in einem anderen Array mit allen Spieler-Händen die beiden Karten eingetragen und am Schluss ausgegeben. Der Code könnte auch logisch nicht funktionieren, aber mir geht es darum das Ganze mal compilieren zu können.</p>
<pre><code class="language-cpp">#include &lt;cstdlib&gt;
#include &lt;iostream&gt;

using namespace std;

// Kartendeck besteht aus 4x13 = 52 Karten
const char cards[13][4][3] = {  { &quot;Ah&quot;, &quot;As&quot;, &quot;Ap&quot;, &quot;Ak&quot; },
                                { &quot;Kh&quot;, &quot;Ks&quot;, &quot;Kp&quot;, &quot;Kk&quot; },
                                { &quot;Qh&quot;, &quot;Qs&quot;, &quot;Qp&quot;, &quot;Qk&quot; },
                                { &quot;Jh&quot;, &quot;Js&quot;, &quot;Jp&quot;, &quot;Jk&quot; },
                                { &quot;1h&quot;, &quot;1s&quot;, &quot;1p&quot;, &quot;1k&quot; },
                                { &quot;9h&quot;, &quot;9s&quot;, &quot;9p&quot;, &quot;9k&quot; },
                                { &quot;8h&quot;, &quot;8s&quot;, &quot;8p&quot;, &quot;8k&quot; },
                                { &quot;7h&quot;, &quot;7s&quot;, &quot;7p&quot;, &quot;7k&quot; },
                                { &quot;6h&quot;, &quot;6s&quot;, &quot;6p&quot;, &quot;6k&quot; },
                                { &quot;5h&quot;, &quot;5s&quot;, &quot;5p&quot;, &quot;5k&quot; },
                                { &quot;4h&quot;, &quot;4s&quot;, &quot;4p&quot;, &quot;4k&quot; },
                                { &quot;3h&quot;, &quot;3s&quot;, &quot;3p&quot;, &quot;3k&quot; },
                                { &quot;2h&quot;, &quot;2s&quot;, &quot;2p&quot;, &quot;2k&quot; }};

int main(int argc, char *argv[])
{
    int players, conflicts;                // Anzahl Spieler
    bool carddeck[13][4];                  // Ausgeteilte Karten
    bool unique = false;                   // Verifizierungsvariable

    // Programm
    cout &lt;&lt; &quot;Anzahl Spieler: &quot;;
    cin &gt;&gt; players;
    int hand[players-1][2][2];               // [Spieler 0 bis x][Karte 0 oder 1][Wert oder Art]

    srand((unsigned)time(NULL));

    // carddeck reseten
    for (int i=0; i&lt;13; i++) {
        for (int j=0; j&lt;4; j++) {
            carddeck[i][j] = false;
        }
    }    

    conflicts = 0;

    //Karten austeilen
    for (int i=0; i&lt;players; i++) {                         // Spieler durchlaufen
        for (int j=0; j&lt;2; j++ ) {                          // 2 Karen ziehen
            while (!unique) {                                     // Solange bis eine gültige Karte gezogen wurde
                  unique = true;
                  hand[i][j][0] = rand()%13;                      // Kartenwert ziehen
                  hand[i][j][1] = rand()%4;                       // Kartenart ziehen
                  if (carddeck[ hand[i][j][0] ][ hand[i][j][1] ]) {            // Kontrolle ob Karte bereits vergeben ist
                     unique = false;                                           // Ja
                     conflicts++; }                                         
                  else { carddeck[ hand[i][j][0] ][ hand[i][j][1] ] = true; }   // Nein =&gt; eintragen
            }
        }
    }

    //cout &lt;&lt; conflicts &lt;&lt; &quot;Konflikte&quot; &lt;&lt; endl;
    for (int i=0; i&lt;players; i++) {
        cout &lt;&lt; &quot;Spieler &quot; &lt;&lt; i+1 &lt;&lt; &quot;: &quot; &lt;&lt; cards[ hand[i][0][0] ][ hand[i][0][1] ];
        cout &lt;&lt; &quot; &quot; &lt;&lt; cards[ hand[i][1][0] ][ hand[i][1][1] ] &lt;&lt; endl;
    }

    system(&quot;PAUSE&quot;);
    return EXIT_SUCCESS;
}
</code></pre>
<p>Ich lasse mich auch gerne belehren, wie man solche fehler selber analysieren kann (mit Dev-C++)...</p>
<p>Besten Dank für deine Aufmerksamkeit <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>
<p>greez gieraffe</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1014162</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1014162</guid><dc:creator><![CDATA[Gieraffe]]></dc:creator><pubDate>Sat, 11 Mar 2006 18:04:26 GMT</pubDate></item><item><title><![CDATA[Reply to Array erzeugt Programmabsturz on Sat, 11 Mar 2006 18:10:10 GMT]]></title><description><![CDATA[<p>Hi,</p>
<p>ein Array mit z. B. 5 Elementen benutzt Index 0 bis Index 4 (=5 Elemente).</p>
<p>mfg theosoft</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1014173</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1014173</guid><dc:creator><![CDATA[theosoft]]></dc:creator><pubDate>Sat, 11 Mar 2006 18:10:10 GMT</pubDate></item><item><title><![CDATA[Reply to Array erzeugt Programmabsturz on Sat, 11 Mar 2006 18:20:03 GMT]]></title><description><![CDATA[<p>ahhrg, jo, genau!!</p>
<p>Anscheinend ist die erste Karte in Ordnung, die restlichen haben viel zu hohe Werte... Keinen Plan wieso, aber das dürfte man rausfinden.</p>
<p>danke und gruss</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1014181</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1014181</guid><dc:creator><![CDATA[Gieraffe]]></dc:creator><pubDate>Sat, 11 Mar 2006 18:20:03 GMT</pubDate></item></channel></rss>