<?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[(Gelöst) Zahl soll in Schleife von 2 zu 0 nach Tastendruck werden...]]></title><description><![CDATA[<p><strong>Edit: Problem gelöst.</strong></p>
<p>Hi,</p>
<p>bin Anfänger in C++ und wollte ein bisschen coden lernen und komm jetzt nicht weiter. Das Problem ist, dass ich die Zahl 2, die als einzigstes ausgegeben wird, nach Drücken der Taste &quot;w&quot; bzw. &quot;s&quot; zu 0 werden soll und die obere oder untere Zahl davon zu einer 2 werden soll. Leider bleibt die frühere 2 noch stehen. Und die soll zu einer 0 umgewandelt werden, was nicht gelingt.</p>
<p>Hier mal ein Ausschnitt des Quellcodes:</p>
<pre><code class="language-cli">#include &lt;iostream&gt;
#include &lt;windows.h&gt;
#include &lt;cstdlib&gt;
using namespace std;

enum
{
HEIGHT = 10,
WIDTH = 20
};

int gameboard[100][100];
int substraction(0);
int addition(0);

void game_board(int x, int y);
void making_board(int x, int y);
void making_new_board(int x, int y);

int main()
{
	game_board(HEIGHT, WIDTH);
	making_board(HEIGHT, WIDTH);

	return 0;
}

// Interne Berechnung der Spielfeldgröße

void game_board(int x, int y)
{
	for (int i = 0; i &lt; x; i++)
	{
		for (int j = 0; j &lt; y; j++)
		{

			if ((i != 0) &amp;&amp; (j != 0))
			{
				gameboard[i][j] = 0;
			}
			else
			{
				gameboard[i][j] = 1;
			}

			if ((i == (x - 1)) || (j == (y - 1)))
			{
				gameboard[i][j] = 1;
			}

		}
	}
}

// Erstellen des Spielfeldes auf der Konsole

void making_board(int x, int y)
{
	for (int i = 0; i &lt; x; i++)
	{
		for (int j = 0; j &lt; y; j++)
		{
			if ((i == (4 - addition)) &amp;&amp; (j == (3 - substraction)))
			{
				gameboard[i][j] = gameboard[i][j] = 2;
				cout &lt;&lt; gameboard[i][j];
				continue;
			}
			cout &lt;&lt; gameboard[i][j];
		}
		cout &lt;&lt; &quot;\n&quot;;
	}
	making_new_board(HEIGHT, WIDTH);
}

// Neuberechnung des Spielfeldes

void making_new_board(int x, int y)
{
	char tastatur;
	cin &gt;&gt; tastatur;

	if (tastatur != 'y')
	{
		for (int i = 0; i &lt; x; i++)
		{
			for (int j = 0; j &lt; y; j++)
			{
				if ((i == (4 - addition)) &amp;&amp; (j == (3 - substraction)))
				{

					if (tastatur == 'w')
					{
						addition++;
						gameboard[i][j] = 0;
						making_board(HEIGHT, WIDTH);
					}
					if (tastatur == 's')
					{
						addition--;
						gameboard[i][j] = 0;
						making_board(HEIGHT, WIDTH);
					}
					if (tastatur == 'a')
					{
						substraction++;
						gameboard[i][j] = 0;
						making_board(HEIGHT, WIDTH);
					}
					if (tastatur == 'd')
					{
						substraction--;
						gameboard[i][j] = 0;
						making_board(HEIGHT, WIDTH);
					}
				}
			}
		}
	}
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/251353/gelöst-zahl-soll-in-schleife-von-2-zu-0-nach-tastendruck-werden</link><generator>RSS for Node</generator><lastBuildDate>Tue, 15 Sep 2026 14:31:01 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/251353.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 03 Oct 2009 16:12:56 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to (Gelöst) Zahl soll in Schleife von 2 zu 0 nach Tastendruck werden... on Sun, 04 Oct 2009 10:04:36 GMT]]></title><description><![CDATA[<p><strong>Edit: Problem gelöst.</strong></p>
<p>Hi,</p>
<p>bin Anfänger in C++ und wollte ein bisschen coden lernen und komm jetzt nicht weiter. Das Problem ist, dass ich die Zahl 2, die als einzigstes ausgegeben wird, nach Drücken der Taste &quot;w&quot; bzw. &quot;s&quot; zu 0 werden soll und die obere oder untere Zahl davon zu einer 2 werden soll. Leider bleibt die frühere 2 noch stehen. Und die soll zu einer 0 umgewandelt werden, was nicht gelingt.</p>
<p>Hier mal ein Ausschnitt des Quellcodes:</p>
<pre><code class="language-cli">#include &lt;iostream&gt;
#include &lt;windows.h&gt;
#include &lt;cstdlib&gt;
using namespace std;

enum
{
HEIGHT = 10,
WIDTH = 20
};

int gameboard[100][100];
int substraction(0);
int addition(0);

void game_board(int x, int y);
void making_board(int x, int y);
void making_new_board(int x, int y);

int main()
{
	game_board(HEIGHT, WIDTH);
	making_board(HEIGHT, WIDTH);

	return 0;
}

// Interne Berechnung der Spielfeldgröße

void game_board(int x, int y)
{
	for (int i = 0; i &lt; x; i++)
	{
		for (int j = 0; j &lt; y; j++)
		{

			if ((i != 0) &amp;&amp; (j != 0))
			{
				gameboard[i][j] = 0;
			}
			else
			{
				gameboard[i][j] = 1;
			}

			if ((i == (x - 1)) || (j == (y - 1)))
			{
				gameboard[i][j] = 1;
			}

		}
	}
}

// Erstellen des Spielfeldes auf der Konsole

void making_board(int x, int y)
{
	for (int i = 0; i &lt; x; i++)
	{
		for (int j = 0; j &lt; y; j++)
		{
			if ((i == (4 - addition)) &amp;&amp; (j == (3 - substraction)))
			{
				gameboard[i][j] = gameboard[i][j] = 2;
				cout &lt;&lt; gameboard[i][j];
				continue;
			}
			cout &lt;&lt; gameboard[i][j];
		}
		cout &lt;&lt; &quot;\n&quot;;
	}
	making_new_board(HEIGHT, WIDTH);
}

// Neuberechnung des Spielfeldes

void making_new_board(int x, int y)
{
	char tastatur;
	cin &gt;&gt; tastatur;

	if (tastatur != 'y')
	{
		for (int i = 0; i &lt; x; i++)
		{
			for (int j = 0; j &lt; y; j++)
			{
				if ((i == (4 - addition)) &amp;&amp; (j == (3 - substraction)))
				{

					if (tastatur == 'w')
					{
						addition++;
						gameboard[i][j] = 0;
						making_board(HEIGHT, WIDTH);
					}
					if (tastatur == 's')
					{
						addition--;
						gameboard[i][j] = 0;
						making_board(HEIGHT, WIDTH);
					}
					if (tastatur == 'a')
					{
						substraction++;
						gameboard[i][j] = 0;
						making_board(HEIGHT, WIDTH);
					}
					if (tastatur == 'd')
					{
						substraction--;
						gameboard[i][j] = 0;
						making_board(HEIGHT, WIDTH);
					}
				}
			}
		}
	}
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1787708</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1787708</guid><dc:creator><![CDATA[Shen]]></dc:creator><pubDate>Sun, 04 Oct 2009 10:04:36 GMT</pubDate></item><item><title><![CDATA[Reply to (Gelöst) Zahl soll in Schleife von 2 zu 0 nach Tastendruck werden... on Sat, 03 Oct 2009 22:48:04 GMT]]></title><description><![CDATA[<p>der code sieht für mich eher nach C als C++ aus^^</p>
<pre><code class="language-cpp">#define HEIGHT        10
#define WEIGHT        20
</code></pre>
<p>hat extrem viele nachteile, nimm stattdessen</p>
<pre><code class="language-cpp">enum {
 HEIGHT = 10,
 WEIGHT = 20
};

//oder

const std::size_t HEIGHT = 10;
const std::size_t WEIGHT = 20;
</code></pre>
<p>Benutzung bleibt wie gehabt...</p>
<p>btw:<br />
<a href="http://dict.leo.org/ende?lp=ende&amp;lang=de&amp;searchLoc=0&amp;cmpType=relaxed&amp;sectHdr=on&amp;spellToler=on&amp;chinese=both&amp;pinyin=diacritic&amp;search=WEIGHT+&amp;relink=on" rel="nofollow">WEIGHT-Übersetzungen</a></p>
<p>meintest du evtl WIDTH?!</p>
<p>zu den Fkt an sich:<br />
Wieso ruft making_board making_new_board auf?<br />
Wieso muss man beide nacheinander aufrufen?</p>
<p>zu globalen Variablen:<br />
Das Array Global zu machen, ist vll gerade noch so ok - aber addition und erst recht tastatur ist... bäh xP</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1787796</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1787796</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Sat, 03 Oct 2009 22:48:04 GMT</pubDate></item><item><title><![CDATA[Reply to (Gelöst) Zahl soll in Schleife von 2 zu 0 nach Tastendruck werden... on Sun, 04 Oct 2009 10:05:36 GMT]]></title><description><![CDATA[<p>Wenn ich es richtig mit Google Suche gelesen habe, ist enum sicherer als define, und deshalb sollte ich es lieber so schreiben, oder?</p>
<p>unskilled schrieb:</p>
<blockquote>
<p>meintest du evtl WIDTH?!</p>
</blockquote>
<p>Klar meinte ich WIDTH und nicht WEIGHT, aber das war mir auch jetzt nicht so wichtig, genauso wie addition, etc. Da gibt es bestimmt bessere variablennamen... <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f644.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--face_with_rolling_eyes"
      title=":rolling_eyes:"
      alt="🙄"
    /></p>
<p>unskilled schrieb:</p>
<blockquote>
<p>zu den Fkt an sich:<br />
Wieso ruft making_board making_new_board auf?<br />
Wieso muss man beide nacheinander aufrufen?</p>
</blockquote>
<p>Ja, das ist ein Fehler von mir. Am Anfang brauchte ich es noch, um den Code auszuführen und hab es nicht bemerkt und dann einfach stehen gelassen.</p>
<p>unskilled schrieb:</p>
<blockquote>
<p>zu globalen Variablen:<br />
Das Array Global zu machen, ist vll gerade noch so ok - aber addition und erst recht tastatur ist... bäh xP</p>
</blockquote>
<p>das mit tastatur kann ich ins making_new_board machen, aber wenn ich addition lokal in eine Funktion schreibe, funktioniert es nicht, in der anderen funktion.<br />
Und wieso ist es schlimm, wenn ich es global deklariere? Welche Nachteile entstehen dadurch?</p>
<p>// Edit: Hab mal den Code verbessert...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1787853</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1787853</guid><dc:creator><![CDATA[Shen]]></dc:creator><pubDate>Sun, 04 Oct 2009 10:05:36 GMT</pubDate></item><item><title><![CDATA[Reply to (Gelöst) Zahl soll in Schleife von 2 zu 0 nach Tastendruck werden... on Sun, 04 Oct 2009 13:58:53 GMT]]></title><description><![CDATA[<p>Shen schrieb:</p>
<blockquote>
<p>Wenn ich es richtig mit Google Suche gelesen habe, ist enum sicherer als define, und deshalb sollte ich es lieber so schreiben, oder?</p>
</blockquote>
<p>Jopp - define ist eben einfach eine (stumpfe) Ersetzung, die der Präprozessor durchführt. Für so etwas nimmt man Konstanten. Mit enum kann man sich sparen, den richtigen Typen hinzuschreiben, weil automatisch ein passender genommen wird...</p>
<p>Shen schrieb:</p>
<blockquote>
<p>unskilled schrieb:</p>
<blockquote>
<p>zu globalen Variablen:<br />
Das Array Global zu machen, ist vll gerade noch so ok - aber addition und erst recht tastatur ist... bäh xP</p>
</blockquote>
<p>das mit tastatur kann ich ins making_new_board machen, aber wenn ich addition lokal in eine Funktion schreibe, funktioniert es nicht, in der anderen funktion.<br />
Und wieso ist es schlimm, wenn ich es global deklariere? Welche Nachteile entstehen dadurch?</p>
</blockquote>
<p>Man versteckt sie besser mit static in den Funktionen, wo sie gebraucht werden - sonst wird es sehr schnell sehr unübersichtlich und vor allem hast du keinerlei Kapselung - jeder, der deine Fkt. später mal verwendet, kann deine Variablen nach Lust und Laune verändern...</p>
<p>Die &quot;richtigen&quot; Code-Tags sind übrigens in dem Fall die &quot;C/C++&quot; und nichts mit CLI^^</p>
<p>Ich hab mich mal versucht:</p>
<pre><code class="language-cpp">#include &lt;iostream&gt;
#include &lt;windows.h&gt; //brauchst du btw nicht...
#include &lt;cstdlib&gt; //brauchst du btw nicht...
using namespace std;

enum
{
	HEIGHT = 10,
	WIDTH = 20
};

int gameboard[HEIGHT][WIDTH];

void set_game_board_values(int x, int y);
void making_board(int x, int y);

int main()
{
    set_game_board_values(HEIGHT, WIDTH);
    making_board(HEIGHT, WIDTH);
}

// Interne Berechnung der Spielfeldgröße
void set_game_board_values(int x, int y)
{	//sieht nen bissl umständlich aus, was du hier machst - vll sagst du mal, was du bezwecken willst - geht bestimmt einfacher!?
    for (int i = 0; i &lt; x; i++)
    {
        for (int j = 0; j &lt; y; j++)
        {
            if ((i != 0) &amp;&amp; (j != 0))
                gameboard[i][j] = 0;
            else
                gameboard[i][j] = 1;

            if ((i == (x - 1)) || (j == (y - 1)))
            {
                gameboard[i][j] = 1;
            }

        }
    }
}

// Neuberechnung des Spielfeldes
void making_new_board(int x, int y, int addition, int substraction)
{
    char tastatur;
    cin &gt;&gt; tastatur;

    if (tastatur == 'y')
		return; //funktion verlassen

	for (int i = 0; i &lt; x; i++)
    {
        for (int j = 0; j &lt; y; j++)
        {
            if ((i == (4 - addition)) &amp;&amp; (j == (3 - substraction)))
            {
				switch(tastatur)
				{
				case 'w':
				case 'W':
                    addition++;
                    gameboard[i][j] = 0;
                    making_board(HEIGHT, WIDTH);
					break;

				case 's':
				case 'S':
                    addition--;
                    gameboard[i][j] = 0;
                    making_board(HEIGHT, WIDTH);
					break;

				case 'a':
				case 'A':
                    substraction++;
                    gameboard[i][j] = 0;
                    making_board(HEIGHT, WIDTH);
					break;

				case 'd':
				case 'D':
                    substraction--;
                    gameboard[i][j] = 0;
                    making_board(HEIGHT, WIDTH);
					break;
				}
            }
        }
    }
}

// Erstellen des Spielfeldes auf der Konsole
void making_board(int x, int y)
{
	static int addition = 0;
	static int substraction = 0;

    for (int i = 0; i &lt; x; i++)
    {
        for (int j = 0; j &lt; y; j++)
        {
            if ((i == (4 - addition)) &amp;&amp; (j == (3 - substraction)))
            {
                gameboard[i][j] = gameboard[i][j] = 2;	//???
//                cout &lt;&lt; gameboard[i][j];
//                continue;
            }
            cout &lt;&lt; gameboard[i][j];
        }
        cout &lt;&lt; &quot;\n&quot;;
    }
    making_new_board(HEIGHT, WIDTH, addition, substraction); //versteh ich noch immer nicht.. du malst das board und direkt danach willst du es neu erstellen?!
}
</code></pre>
<p>allerdings verstehe ich bei vielen Dingen in deinem Quelltext den Sinn nicht...</p>
<p>Wie man schon sieht, könnte man auch das Array noch in der Main-Fkt erstellen und immer mit übergeben - globale Variablen sind also mal wieder absolut unnötig...</p>
<p>bb</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1787952</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1787952</guid><dc:creator><![CDATA[unskilled]]></dc:creator><pubDate>Sun, 04 Oct 2009 13:58:53 GMT</pubDate></item></channel></rss>