<?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[Wie an bestimmte Position im String navigieren?]]></title><description><![CDATA[<p>Hallo, der folgenden Code besteht aus einer Eingabe mit einer Prompt, hierfür wurde getch() verwendet um auf spezielle Tastutur Anschläge zu reagieren, wie z.B.:</p>
<p>Enter = neue Zeile<br />
Backspace = lösche das letzte Zeichen<br />
ESC = lösche den ganzen Input</p>
<p>Nun schaffe ich es nicht mit den Pfeiltasten, an eine bestimmte Position im Input string zu gelangen, habe es mit gotoxy probiert, aber ich glaub hier wird nur die Ausgabe geändert aber nicht der string.</p>
<p>Noch ein Problem, wenn ich Backspace ausklammer dann versuche den String zu verschieben wird dieser dann in der Console überschrieben, deshalb muss ich Backspace selbst definieren, eigentlich ganz einfach da ja nur ein leere insert(' ') integriert wird, aber die Position wieder <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f621.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--pouting_face"
      title=":rage:"
      alt="😡"
    /></p>
<p>Hoffe ihr könnt mir weiterhelfen.</p>
<pre><code class="language-cpp">#include &lt;windows.h&gt;
#include &lt;string&gt;
#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;tchar.h&gt;
#include &lt;conio.h&gt;

using namespace std;

#pragma comment(lib, &quot;User32.lib&quot;)

void clear_line( string::size_type length, const string &amp;prompt ) 
{
	cout.put( '\r' );

	for( string::size_type i = 0; i &lt; ( length + prompt.length( ) ); ++i ) 
	{
		cout.put( ' ' );
	}

	cout &lt;&lt; '\r' &lt;&lt; prompt &lt;&lt; flush;
}

void gotoxy(short x, short y)
{
	HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(hCon, pos);
}

int main( ) 
{		
	bool do_exit = false;
	const char prompt[ ] = &quot;:\\&gt;&quot;;

	HANDLE std_output = GetStdHandle( STD_OUTPUT_HANDLE );
	CONSOLE_SCREEN_BUFFER_INFO console_screen_buffer_info;	

	do 
	{
		string input;
		int key;

		cout &lt;&lt; prompt;				

		do 
		{
			key = _getch( );

			//Backspace - Löschen
			if( ( key == 0x08 ) &amp;&amp; input.length( ) ) 
			{ 

				clear_line( input.length( ), prompt );

				if(input.length() != 0)
					input.erase( input.length( ) - 1, 1 );

				cout &lt;&lt; input &lt;&lt; flush;

			} 

			/*
			//Leertaste
			else if( key == 0x20)
			{	
				input.insert(&quot; &quot;);				
			}
			*/

			//Escape - ESC
			else if( key == 0x1b ) 
			{ 
				clear_line( input.length( ), prompt );
				input = &quot;&quot;;
			} 

			//Steuerzeichen
			else if( key == 0xe0 ) 
			{ 
				switch( _getch( ) ) 
				{
					int x, y;

					case 0x4B: // Pfeiltaste nach Links		

						GetConsoleScreenBufferInfo( std_output, &amp;console_screen_buffer_info );
						x = console_screen_buffer_info.dwCursorPosition.X;
						y = console_screen_buffer_info.dwCursorPosition.Y; 

						gotoxy(x-1, y);		

						break;

					case 0x4D: // Pfeiltaste nach Rechts			

						GetConsoleScreenBufferInfo( std_output, &amp;console_screen_buffer_info );
						x = console_screen_buffer_info.dwCursorPosition.X;
						y = console_screen_buffer_info.dwCursorPosition.Y; 

						gotoxy(x+1, y);

						break;
				}

			} 
			else if( key != '\r' ) 
			{
				input += key;
				cout.put( key );				
			}

		}while( key != '\r' ); // enter

		cout.put( '\n' );

		if( input.length( ) )
		{
			cout &lt;&lt; &quot;Input: &quot; &lt;&lt; input &lt;&lt; endl &lt;&lt; endl;				
		}

		if( input == &quot;exit&quot; ) 
		{			
			do_exit = true;
		}

	} while( !do_exit );
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/186573/wie-an-bestimmte-position-im-string-navigieren</link><generator>RSS for Node</generator><lastBuildDate>Thu, 24 Sep 2026 11:28:04 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/186573.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 10 Jul 2007 12:47:13 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Wie an bestimmte Position im String navigieren? on Tue, 10 Jul 2007 12:47:13 GMT]]></title><description><![CDATA[<p>Hallo, der folgenden Code besteht aus einer Eingabe mit einer Prompt, hierfür wurde getch() verwendet um auf spezielle Tastutur Anschläge zu reagieren, wie z.B.:</p>
<p>Enter = neue Zeile<br />
Backspace = lösche das letzte Zeichen<br />
ESC = lösche den ganzen Input</p>
<p>Nun schaffe ich es nicht mit den Pfeiltasten, an eine bestimmte Position im Input string zu gelangen, habe es mit gotoxy probiert, aber ich glaub hier wird nur die Ausgabe geändert aber nicht der string.</p>
<p>Noch ein Problem, wenn ich Backspace ausklammer dann versuche den String zu verschieben wird dieser dann in der Console überschrieben, deshalb muss ich Backspace selbst definieren, eigentlich ganz einfach da ja nur ein leere insert(' ') integriert wird, aber die Position wieder <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f621.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--pouting_face"
      title=":rage:"
      alt="😡"
    /></p>
<p>Hoffe ihr könnt mir weiterhelfen.</p>
<pre><code class="language-cpp">#include &lt;windows.h&gt;
#include &lt;string&gt;
#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;tchar.h&gt;
#include &lt;conio.h&gt;

using namespace std;

#pragma comment(lib, &quot;User32.lib&quot;)

void clear_line( string::size_type length, const string &amp;prompt ) 
{
	cout.put( '\r' );

	for( string::size_type i = 0; i &lt; ( length + prompt.length( ) ); ++i ) 
	{
		cout.put( ' ' );
	}

	cout &lt;&lt; '\r' &lt;&lt; prompt &lt;&lt; flush;
}

void gotoxy(short x, short y)
{
	HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(hCon, pos);
}

int main( ) 
{		
	bool do_exit = false;
	const char prompt[ ] = &quot;:\\&gt;&quot;;

	HANDLE std_output = GetStdHandle( STD_OUTPUT_HANDLE );
	CONSOLE_SCREEN_BUFFER_INFO console_screen_buffer_info;	

	do 
	{
		string input;
		int key;

		cout &lt;&lt; prompt;				

		do 
		{
			key = _getch( );

			//Backspace - Löschen
			if( ( key == 0x08 ) &amp;&amp; input.length( ) ) 
			{ 

				clear_line( input.length( ), prompt );

				if(input.length() != 0)
					input.erase( input.length( ) - 1, 1 );

				cout &lt;&lt; input &lt;&lt; flush;

			} 

			/*
			//Leertaste
			else if( key == 0x20)
			{	
				input.insert(&quot; &quot;);				
			}
			*/

			//Escape - ESC
			else if( key == 0x1b ) 
			{ 
				clear_line( input.length( ), prompt );
				input = &quot;&quot;;
			} 

			//Steuerzeichen
			else if( key == 0xe0 ) 
			{ 
				switch( _getch( ) ) 
				{
					int x, y;

					case 0x4B: // Pfeiltaste nach Links		

						GetConsoleScreenBufferInfo( std_output, &amp;console_screen_buffer_info );
						x = console_screen_buffer_info.dwCursorPosition.X;
						y = console_screen_buffer_info.dwCursorPosition.Y; 

						gotoxy(x-1, y);		

						break;

					case 0x4D: // Pfeiltaste nach Rechts			

						GetConsoleScreenBufferInfo( std_output, &amp;console_screen_buffer_info );
						x = console_screen_buffer_info.dwCursorPosition.X;
						y = console_screen_buffer_info.dwCursorPosition.Y; 

						gotoxy(x+1, y);

						break;
				}

			} 
			else if( key != '\r' ) 
			{
				input += key;
				cout.put( key );				
			}

		}while( key != '\r' ); // enter

		cout.put( '\n' );

		if( input.length( ) )
		{
			cout &lt;&lt; &quot;Input: &quot; &lt;&lt; input &lt;&lt; endl &lt;&lt; endl;				
		}

		if( input == &quot;exit&quot; ) 
		{			
			do_exit = true;
		}

	} while( !do_exit );
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1322265</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1322265</guid><dc:creator><![CDATA[kernel64]]></dc:creator><pubDate>Tue, 10 Jul 2007 12:47:13 GMT</pubDate></item><item><title><![CDATA[Reply to Wie an bestimmte Position im String navigieren? on Tue, 10 Jul 2007 12:53:58 GMT]]></title><description><![CDATA[<p>Du mußt dir vermutlich die Position in der Zeile merken und dann bei den normalen Eingaben an dieser Position Zeichen einfügen bzw. überschreiben.</p>
<p>PS: Diese längliche if-else-Kaskade kannst du auch als switch() schreiben - ist mit Sicherheit übersichtlicher (und vermutlich sogar schneller).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1322273</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1322273</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Tue, 10 Jul 2007 12:53:58 GMT</pubDate></item><item><title><![CDATA[Reply to Wie an bestimmte Position im String navigieren? on Tue, 10 Jul 2007 13:00:48 GMT]]></title><description><![CDATA[<blockquote>
<p>Du mußt dir vermutlich die Position in der Zeile merken und dann bei den normalen Eingaben an dieser Position Zeichen einfügen bzw. überschreiben.</p>
</blockquote>
<p>Und wie mache ich das? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1322278</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1322278</guid><dc:creator><![CDATA[kernel64]]></dc:creator><pubDate>Tue, 10 Jul 2007 13:00:48 GMT</pubDate></item><item><title><![CDATA[Reply to Wie an bestimmte Position im String navigieren? on Tue, 10 Jul 2007 13:06:23 GMT]]></title><description><![CDATA[<p>Zum Merken brauchst du eine einfache int-Variable - Pfeil rechts und die &quot;normalen&quot; Eingabetasten erhöhen den Wert, Pfeil links und Backspace verringern den Wert. Und bei der Eingabe kannst du noch auswählen zwischen Überschreiben und Einfügen:</p>
<pre><code class="language-cpp">//Überschreiben:
if(textpos&lt;input.length())
  input[textpos]=key;
else
  input+=key;

//Einfügen:
input.insert(textpos,key);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1322281</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1322281</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Tue, 10 Jul 2007 13:06:23 GMT</pubDate></item><item><title><![CDATA[Reply to Wie an bestimmte Position im String navigieren? on Tue, 10 Jul 2007 14:35:34 GMT]]></title><description><![CDATA[<p>Habs überarbeitet:</p>
<pre><code class="language-cpp">do 
	{
		string input;
		int key;
		int textpos = 0;

		cout &lt;&lt; prompt;				

		do 
		{
			key = _getch( );

			//Backspace - Löschen
			if( ( key == 0x08 ) &amp;&amp; input.length( ) ) 
			{ 	
				textpos--;
				clear_line( input.length( ), prompt );

				if(input.length() != 0)
					input.erase( input.length( ) - 1, 1 );

				cout &lt;&lt; input &lt;&lt; flush;

			} 

			//Leertaste
			else if( key == 0x20)
			{	
				//clear_line( input.length( ), prompt );
				input.insert(textpos, &quot; &quot;);	
				cout &lt;&lt; input &lt;&lt; flush;
			}

			//Escape - ESC
			else if( key == 0x1b ) 
			{ 
				textpos = 0;
				clear_line( input.length( ), prompt );
				input = &quot;&quot;;
			}

			//Steuerzeichen
			else if( key == 0xe0 ) 
			{ 
				switch( _getch( ) ) 
				{
					int x, y;

					case 0x4B: // Pfeiltaste nach Links		

						GetConsoleScreenBufferInfo( std_output, &amp;console_screen_buffer_info );
						x = console_screen_buffer_info.dwCursorPosition.X;
						y = console_screen_buffer_info.dwCursorPosition.Y;

						gotoxy(x-1, y);  					
						textpos--;							

						break;

					case 0x4D: // Pfeiltaste nach Rechts			

						GetConsoleScreenBufferInfo( std_output, &amp;console_screen_buffer_info );
						x = console_screen_buffer_info.dwCursorPosition.X;
						y = console_screen_buffer_info.dwCursorPosition.Y;

						gotoxy(x+1, y); 					
						textpos++;

						break;
				}
			} 

			else if( key != '\r' ) 
			{				
				string tmp;
				if(textpos &lt; input.length())
				{					 
					tmp = key;					
					input.insert(textpos, tmp);
					cout.put( key );   					
					textpos++;

				}
				else
				{
					input += key; 
					cout.put( key );					
					textpos++;
				}				
			}		

		}while( key != '\r' ); // enter

		cout.put( '\n' );

		if( input.length( ) )
		{
			cout &lt;&lt; &quot;Input: &quot; &lt;&lt; input &lt;&lt; &quot; -&gt; &quot; &lt;&lt; textpos &lt;&lt; endl &lt;&lt; endl;				
		}

		if( input == &quot;exit&quot; ) 
		{			
			do_exit = true;
		}

	} while( !do_exit );
</code></pre>
<p>Wenn ich nun mit den Pfeilen arbeite und dann was im string reinschreibe wird die Änderung aber nicht in der Console angezeigt, gleiches gilt für Backspace.</p>
<p>Gibt es eine andere Möglichkeit den Cursor zu verschieben ohne WinAPI wgen dem gotoxy dieser überschreibt immer meine Prompt <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1322395</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1322395</guid><dc:creator><![CDATA[kernel64]]></dc:creator><pubDate>Tue, 10 Jul 2007 14:35:34 GMT</pubDate></item><item><title><![CDATA[Reply to Wie an bestimmte Position im String navigieren? on Wed, 11 Jul 2007 06:28:29 GMT]]></title><description><![CDATA[<p>Ohne gotoxy() kannst du du Cursor-Position nicht wirklich steuern, also wirst du da nicht drumherum kommen. Was du Ausgaben angeht, mußt du beim Einfügen und Löschen (btw wäre es eine gute Idee, auch beim Löschen an der aktuellen Textposition anzusetzen) zumindest den hinteren Teil des Strings neu schreiben.</p>
<p>PS: Und ich finde immer noch, daß ein switch(key)... übersichtlicher wäre als deine if-else-Kaskade <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>PPS: Die Enter-Taste ist übrigens '\n' <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/1322744</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1322744</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Wed, 11 Jul 2007 06:28:29 GMT</pubDate></item><item><title><![CDATA[Reply to Wie an bestimmte Position im String navigieren? on Wed, 11 Jul 2007 08:16:07 GMT]]></title><description><![CDATA[<p>Habe ein Problem, ich bekomme es nicht hin eine Bedingung aufzustellen wenn man nach die linke Pfeiltaste drückt, dieser solange nach links gehen darf bis zum Promt ende.</p>
<p>Hier erstmal mein Teil für den Rechtsklick, hier darf der Cursor nur bis zum ende der Eingabe gehen:</p>
<pre><code class="language-cpp">case 0x4D: // Pfeiltaste nach Rechts: 			

						GetConsoleScreenBufferInfo( std_output, &amp;console_screen_buffer_info );
						x = console_screen_buffer_info.dwCursorPosition.X;
						y = console_screen_buffer_info.dwCursorPosition.Y;

						if(textpos &lt; input.length() + prompt.length())
						{
							textpos++;
							gotoxy(textpos, y); 					

						}
</code></pre>
<p>Habe es auch für Links versucht, doch meine Bedingung ist falsch:</p>
<pre><code class="language-cpp">case 0x4B: // Pfeiltaste nach Links		

						GetConsoleScreenBufferInfo( std_output, &amp;console_screen_buffer_info );
						x = console_screen_buffer_info.dwCursorPosition.X;
						y = console_screen_buffer_info.dwCursorPosition.Y;

						if(textpos &gt; prompt.length())
						{
							textpos--;
							gotoxy(textpos, y);  												
						}

						break;
</code></pre>
<p>Habe nun beim Backspace ein <strong>textpos--</strong> eingefügt.</p>
<p>Wenn ich nun nach links gehe und in ein bestehenden string etwas reinschreibe wird der string in der ausgabe nicht nach vorne vorgerückt, damit meine ich die alte ausgabe wird von der neuen überschrieben, wie kann man das verhindern?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1322816</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1322816</guid><dc:creator><![CDATA[kernel64]]></dc:creator><pubDate>Wed, 11 Jul 2007 08:16:07 GMT</pubDate></item><item><title><![CDATA[Reply to Wie an bestimmte Position im String navigieren? on Wed, 11 Jul 2007 11:21:52 GMT]]></title><description><![CDATA[<p>kernel64 schrieb:</p>
<blockquote>
<p>Habe ein Problem, ich bekomme es nicht hin eine Bedingung aufzustellen wenn man nach die linke Pfeiltaste drückt, dieser solange nach links gehen darf bis zum Promt ende.</p>
<p>Hier erstmal mein Teil für den Rechtsklick, hier darf der Cursor nur bis zum ende der Eingabe gehen:</p>
<pre><code class="language-cpp">case 0x4D: // Pfeiltaste nach Rechts: 			

						GetConsoleScreenBufferInfo( std_output, &amp;console_screen_buffer_info );
						x = console_screen_buffer_info.dwCursorPosition.X;
						y = console_screen_buffer_info.dwCursorPosition.Y;

						if(textpos &lt; input.length() + prompt.length())
						{
							textpos++;
							gotoxy(textpos, y); 					
							
						}
</code></pre>
<p>Habe es auch für Links versucht, doch meine Bedingung ist falsch:</p>
<pre><code class="language-cpp">case 0x4B: // Pfeiltaste nach Links		

						GetConsoleScreenBufferInfo( std_output, &amp;console_screen_buffer_info );
						x = console_screen_buffer_info.dwCursorPosition.X;
						y = console_screen_buffer_info.dwCursorPosition.Y;
						
						if(textpos &gt; prompt.length())
						{
							textpos--;
							gotoxy(textpos, y);  												
						}

						break;
</code></pre>
</blockquote>
<p>Die unterste Grenze für die Textposition ist 0, die oberste input.length() (es ist besser, wenn du dich bei der Position auf den Eingabestring beziehst, sonst fügst du die Daten noch an der falschen Stelle ein).</p>
<blockquote>
<p>Wenn ich nun nach links gehe und in ein bestehenden string etwas reinschreibe wird der string in der ausgabe nicht nach vorne vorgerückt, damit meine ich die alte ausgabe wird von der neuen überschrieben, wie kann man das verhindern?</p>
</blockquote>
<p>Du mußt nicht nur das neu eingegebene Zeichen schreiben, sondern auch alles, was nach der Einfügestelle kam, neu ausgeben (um eine Stelle nach hinten versetzt).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1322989</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1322989</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Wed, 11 Jul 2007 11:21:52 GMT</pubDate></item><item><title><![CDATA[Reply to Wie an bestimmte Position im String navigieren? on Wed, 11 Jul 2007 15:29:21 GMT]]></title><description><![CDATA[<p>So nun hab ichs geschafft, habe auch versucht es mit einer Switch-Anweisung zu machen, doch da bekomme ich seltsame Reaktionen auf Tasten, musste dann wieder mein altes nehmen <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /> will es auch lieber mit switch machen.</p>
<p>Bei der clearLineAt() wird alles gelöscht ab der Prompt bis zum Fensterende, hierfür habe ich die Funktion von Improved Console verwendet zum ermitteln der Fensterbreite.</p>
<pre><code class="language-cpp">#include &lt;windows.h&gt;
#include &lt;string&gt;
#include &lt;iostream&gt;
#include &lt;conio.h&gt;
#include &quot;ic.hpp&quot; //Improved Console

using namespace std; 
using namespace ic; // Auflösen des Improved Console-Namespace

#pragma comment(lib, &quot;User32.lib&quot;)

void clearLineAt(string prompt) 
{
	cout.put( '\r' );

	//Lösche alles ab der Prompt bis zum Ende der Konsole (Improved Console)
	for( string::size_type i = prompt.length(); i &lt; con.getWndSizeX() ; ++i ) 
	{
		cout.put( ' ' );
	}

	cout &lt;&lt; '\r' &lt;&lt; prompt &lt;&lt; flush;
}

void gotoxy(short x, short y)
{
	HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(hCon, pos);
}

int main( ) 
{		
	bool do_exit = false;
	string prompt = &quot;:\\&gt;&quot;;

	HANDLE std_output = GetStdHandle( STD_OUTPUT_HANDLE );
	CONSOLE_SCREEN_BUFFER_INFO console_screen_buffer_info;	

	do 
	{
		string input;
		int key, y;
		int textpos = 0;

		cout &lt;&lt; prompt;				

		do 
		{	

			key = _getch( );

			//Backspace - Löschen
			if( ( key == 0x08 ) &amp;&amp; input.length( ) ) 
			{ 	
				clearLineAt( prompt );					

				if(textpos &gt; 0)
				{										
					input.erase(textpos-1, 1 );
					cout &lt;&lt; input &lt;&lt; flush;					

					GetConsoleScreenBufferInfo( std_output, &amp;console_screen_buffer_info );
					y = console_screen_buffer_info.dwCursorPosition.Y;

					int currentPosition = prompt.length() + textpos;
					gotoxy(currentPosition-1, y);  
					textpos--;						
				}

			} 

			//Leertaste
			else if( key == 0x20)
			{					
				clearLineAt( prompt );
				input.insert(textpos, &quot; &quot;);					

				cout &lt;&lt; input &lt;&lt; flush;

				GetConsoleScreenBufferInfo( std_output, &amp;console_screen_buffer_info );
				y = console_screen_buffer_info.dwCursorPosition.Y;

				int currentPosition = prompt.length() + textpos;
				gotoxy(currentPosition+1, y);  
				textpos++;				
			}

			//Escape - ESC
			else if( key == 0x1b ) 
			{ 
				textpos = 0;
				clearLineAt( prompt );
				input = &quot;&quot;;
			}

			//Steuerzeichen
			else if( key == 0xe0 ) 
			{ 
				switch( _getch( ) ) 
				{					
					int x, y;

					case 0x4B: // Pfeiltaste nach Links		

						GetConsoleScreenBufferInfo( std_output, &amp;console_screen_buffer_info );
						y = console_screen_buffer_info.dwCursorPosition.Y;

						if(textpos &gt; 0)
						{		
							int currentPosition = prompt.length() + textpos;
							gotoxy(currentPosition-1, y);  
							textpos--;
						}

						break;

					case 0x4D: // Pfeiltaste nach Rechts: 			

						GetConsoleScreenBufferInfo( std_output, &amp;console_screen_buffer_info );
						y = console_screen_buffer_info.dwCursorPosition.Y;

						if(textpos &lt; input.length())
						{		
							int currentPosition = prompt.length() + textpos;
							gotoxy(currentPosition+1, y);  
							textpos++;							
						}

						break;

					case 0x53: //Entfernen Taste

						if(input.length() != 0)
						{
							clearLineAt( prompt );
							input.erase(textpos, 1 );
							cout &lt;&lt; input &lt;&lt; flush;	

							GetConsoleScreenBufferInfo( std_output, &amp;console_screen_buffer_info );
							y = console_screen_buffer_info.dwCursorPosition.Y;

							int currentPosition = prompt.length() + textpos;
							gotoxy(currentPosition, y); 
						}

						break;
				}
			} 

			else if( key != '\r' ) 
			{	
				string tmp;
				GetConsoleScreenBufferInfo( std_output, &amp;console_screen_buffer_info );
				y = console_screen_buffer_info.dwCursorPosition.Y;

				if(textpos &lt; input.length())
				{
					clearLineAt( prompt );

					tmp = key;					
					input.insert(textpos, tmp);
					//cout.put( key ); 
					cout &lt;&lt; input &lt;&lt; flush;
				}
				else
				{
					input += key; 
					cout.put( key );															
				}			
				int currentPosition = prompt.length() + textpos;
				gotoxy(currentPosition+1, y);  
				textpos++;
			}	

		}while( key != '\r' ); // enter

		cout.put( '\n' );

		if( input.length( ) )
		{
			cout &lt;&lt; &quot;Input: &quot; &lt;&lt; input &lt;&lt; &quot; -&gt; &quot; &lt;&lt; textpos &lt;&lt; endl &lt;&lt; endl;				
		}

		if( input == &quot;exit&quot; ) 
		{			
			do_exit = true;
		}

	} while( !do_exit );
}
</code></pre>
<p>Es ist mir aufefallen wenn ich mit der Backspace Taste alles lösche in nix mehr da steht bewegt sich der Cursor am Anfang der Prompt hin und her <strong>_</strong>&lt;-&gt;<strong>_</strong> einmal bei textpos=0 und textpos=1 woran liegt das?</p>
<p>So jetzt habe ich ein Grundgerüst erstellt, damit kann ich fast weiterarbeiten, was kann man noch verbessern, optimieren am Code?</p>
<p>Wenn man sich die cmd hierzu anschaut da sehe ich kein Flackern bei der Prompt, wird diese nicht überschrieben wie bei mir und wie kann man das verhindern?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1323252</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1323252</guid><dc:creator><![CDATA[kernel64]]></dc:creator><pubDate>Wed, 11 Jul 2007 15:29:21 GMT</pubDate></item><item><title><![CDATA[Reply to Wie an bestimmte Position im String navigieren? on Thu, 12 Jul 2007 06:07:19 GMT]]></title><description><![CDATA[<p>@switch: Hast du auch daran gedacht, jeden einzelnen Zweig mit break; zu beenden?</p>
<p>@Flackern: Vermutlich überschreibt die normale Konsole nur die Zeichen, die sich tatsächlich geändert haben (du überschreibst erst die komplette Zeile mit Leerzeichen, bevor du deine Ausgabe drübersetzt).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1323524</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1323524</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Thu, 12 Jul 2007 06:07:19 GMT</pubDate></item><item><title><![CDATA[Reply to Wie an bestimmte Position im String navigieren? on Thu, 12 Jul 2007 07:10:49 GMT]]></title><description><![CDATA[<p>Habs nochmal in Switch umgewandelt, doch es sind ganz andere Aktionen auf die Tasten und bei der Ausgabe.</p>
<pre><code class="language-cpp">do 
		{				
			key = _getch( );

			switch(key)
			{

			case 0x08: //Backspace

				clearLineAt( prompt );					

				if(textpos &gt; 0)
				{										
					input.erase(textpos-1, 1 );
					cout &lt;&lt; input &lt;&lt; flush;					

					GetConsoleScreenBufferInfo( std_output, &amp;console_screen_buffer_info );
					y = console_screen_buffer_info.dwCursorPosition.Y;

					currentPosition = prompt.length() + textpos;
					gotoxy(currentPosition-1, y);  
					textpos--;						
				}
				break;

			case 0x20: //Leertaste

				clearLineAt( prompt );
				input.insert(textpos, &quot; &quot;);					

				cout &lt;&lt; input &lt;&lt; flush;

				GetConsoleScreenBufferInfo( std_output, &amp;console_screen_buffer_info );
				y = console_screen_buffer_info.dwCursorPosition.Y;

				currentPosition = prompt.length() + textpos;
				gotoxy(currentPosition+1, y);  
				textpos++;				

				break;

			case 0x1b: //ESC

				textpos = 0;
				clearLineAt( prompt );
				input = &quot;&quot;;

				break;

			case 0xe0: //Steuerzeichen

				  switch( _getch( ) ) 
				  {					
					  int x, y;

					  case 0x4B: // Pfeiltaste nach Links		

						  GetConsoleScreenBufferInfo( std_output, &amp;console_screen_buffer_info );
						  y = console_screen_buffer_info.dwCursorPosition.Y;

						  if(textpos &gt; 0)
						  {		
							  currentPosition = prompt.length() + textpos;
							  gotoxy(currentPosition-1, y);  
							  textpos--;
						  }

						  break;

					  case 0x4D: // Pfeiltaste nach Rechts: 			

						  GetConsoleScreenBufferInfo( std_output, &amp;console_screen_buffer_info );
						  y = console_screen_buffer_info.dwCursorPosition.Y;

						  if(textpos &lt; input.length())
						  {		
							  currentPosition = prompt.length() + textpos;
							  gotoxy(currentPosition+1, y);  
							  textpos++;							
						  }

						  break;

					  case 0x53: //Entfernen Taste

						  if(input.length() != 0)
						  {
							  clearLineAt( prompt );
							  input.erase(textpos, 1 );
							  cout &lt;&lt; input &lt;&lt; flush;	

							  GetConsoleScreenBufferInfo( std_output, &amp;console_screen_buffer_info );
							  y = console_screen_buffer_info.dwCursorPosition.Y;

							  currentPosition = prompt.length() + textpos;
							  gotoxy(currentPosition, y); 
						  }

						  break;
				  }

				  break;
			}

			if( key != '\r' ) 
			{	
				string tmp;
				GetConsoleScreenBufferInfo( std_output, &amp;console_screen_buffer_info );
				y = console_screen_buffer_info.dwCursorPosition.Y;

				if(textpos &lt; input.length())
				{
					clearLineAt( prompt );

					tmp = key;					
					input.insert(textpos, tmp);
					//cout.put( key ); 
					cout &lt;&lt; input &lt;&lt; flush;
				}
				else
				{
					input += key; 
					cout.put( key );															
				}			
				currentPosition = prompt.length() + textpos;
				gotoxy(currentPosition+1, y);  
				textpos++;
			}
}while( key != '\r' ); // enter
</code></pre>
<p>Wenn ich nun die Pfeiltasten verwende, kommen unbekannte Zeichen in der Ausgabe und Backspace funktioniert nicht, bei Leerzeichen wird eine zuviel freigelassen...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1323550</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1323550</guid><dc:creator><![CDATA[kernel64]]></dc:creator><pubDate>Thu, 12 Jul 2007 07:10:49 GMT</pubDate></item><item><title><![CDATA[Reply to Wie an bestimmte Position im String navigieren? on Thu, 12 Jul 2007 07:19:10 GMT]]></title><description><![CDATA[<p>Erstmal kannst du auch den Abschnitt &quot;if(key!='\r')...&quot; in den switch mit reinziehen (als default:-Zweig) - und die Behandlung des Leerzeichens stimmt (vermutlich) mit allen anderen &quot;Normaltasten&quot; überein, benötigt also auch keine Sonderbehandlung.</p>
<p>(und das 'int x,y;' im inneren switch-Block sieht zumindest seltsam aus)</p>
<p>PS: Und anstelle von '\r' solltest du lieber '\n' als Endekriterium der Schleife verwenden <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/1323560</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1323560</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Thu, 12 Jul 2007 07:19:10 GMT</pubDate></item><item><title><![CDATA[Reply to Wie an bestimmte Position im String navigieren? on Thu, 12 Jul 2007 07:37:31 GMT]]></title><description><![CDATA[<p>Hab nun den if Teil in default reingepackt:</p>
<pre><code class="language-cpp">default:

				string tmp;
				GetConsoleScreenBufferInfo( std_output, &amp;console_screen_buffer_info );
				y = console_screen_buffer_info.dwCursorPosition.Y;

				if(textpos &lt; input.length())
				{					
					clearLineAt( prompt );

					tmp = key;					
					input.insert(textpos, tmp);
					//cout.put( key ); 
					cout &lt;&lt; input &lt;&lt; flush;
				}
				else
				{
					//cout &lt;&lt; &quot;TEST&quot; &lt;&lt; endl;

					input += key; 
					cout.put( key );															
				}			
				currentPosition = prompt.length() + textpos;
				gotoxy(currentPosition+1, y);  
				textpos++;
</code></pre>
<p>Jetzt ist mir aufgefallen wenn ich nix eingebe dann wird außerhalb der Scheife <strong>if( input.length( ) )</strong> d.h. input wurde gefüllt, deshalb habe ich eine Test Ausgabe oben in dem else Teil eingebaut und wie man sieht wird diese aufgerufen. Es wird immer ein Zeichen zuviel eingefügt, ich kann z.b. das programm mit exit nicht mehr beenden, außerdem wird gesagt das die Länge = 5 ist.</p>
<p>Habe auch bei der While-Schleife '\n' eingefügt nun wird aber keine neue Zeile erstellt, muss ich nun auch eine Funktion für ENTER einbauen?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1323585</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1323585</guid><dc:creator><![CDATA[kernel64]]></dc:creator><pubDate>Thu, 12 Jul 2007 07:37:31 GMT</pubDate></item><item><title><![CDATA[Reply to Wie an bestimmte Position im String navigieren? on Thu, 12 Jul 2007 07:45:53 GMT]]></title><description><![CDATA[<p>kernel64 schrieb:</p>
<blockquote>
<p>Jetzt ist mir aufgefallen wenn ich nix eingebe dann wird außerhalb der Scheife <strong>if( input.length( ) )</strong> d.h. input wurde gefüllt, deshalb habe ich eine Test Ausgabe oben in dem else Teil eingebaut und wie man sieht wird diese aufgerufen. Es wird immer ein Zeichen zuviel eingefügt, ich kann z.b. das programm mit exit nicht mehr beenden, außerdem wird gesagt das die Länge = 5 ist.</p>
</blockquote>
<p>Und was heißt das auf deutsch?</p>
<p>PS: string::insert() kann auch einzelne Zeichen Zeichen einfügen: <code>input.insert(textpos,1/*Anzahl*/,key/*Wert*/);</code> <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/1323591</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1323591</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Thu, 12 Jul 2007 07:45:53 GMT</pubDate></item><item><title><![CDATA[Reply to Wie an bestimmte Position im String navigieren? on Thu, 12 Jul 2007 12:55:57 GMT]]></title><description><![CDATA[<p>OK jetzt bin ich fertig dank deiner Hilfe <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="👍"
    /> Wie gut das es dich gibt <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>
<pre><code class="language-cpp">#include &lt;windows.h&gt;
#include &lt;string&gt;
#include &lt;iostream&gt;
#include &lt;conio.h&gt;
#include &quot;ic.hpp&quot; //Improved Console

#pragma comment(lib, &quot;User32.lib&quot;)

using namespace std; 
using namespace ic; // Auflösen des Improved Console-Namespace

#define ENTER			13
#define BACKSPACE		8
#define LEERTASTE		32
#define ESCAPE			27
#define STEUERZEICHEN	224
#define PFEIL_LINKS		75
#define PFEIL_RECHTS	77
#define DELETE			83

void clearLineAt(string prompt) 
{
	cout.put( '\r' );

	//Lösche alles ab der Prompt bis zum Ende der Konsole (Improved Console)
	for( string::size_type i = prompt.length(); i &lt; con.getWndSizeX() ; ++i ) 
	{
		cout.put( ' ' );
	}
	//cout &lt;&lt; prompt &lt;&lt; flush;
	cout &lt;&lt; '\r' &lt;&lt; prompt &lt;&lt; flush;	
}

void gotoxy(short x, short y)
{
	HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
	COORD pos;
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(hCon, pos);
}

int main( ) 
{		
	bool do_exit = false;
	string prompt = &quot;:\\&gt;&quot;;

	HANDLE std_output = GetStdHandle( STD_OUTPUT_HANDLE );
	CONSOLE_SCREEN_BUFFER_INFO console_screen_buffer_info;	

	do 
	{
		string input;
		int key, currentPosition, y;
		int textpos = 0;

		cout &lt;&lt; prompt;				

		do 
		{				
			key = _getch( );

			switch(key)
			{

			case BACKSPACE: //Backspace

				clearLineAt( prompt );					

				if(textpos &gt; 0)
				{										
					input.erase(textpos-1, 1 );
					cout &lt;&lt; input &lt;&lt; flush;					

					GetConsoleScreenBufferInfo( std_output, &amp;console_screen_buffer_info );
					y = console_screen_buffer_info.dwCursorPosition.Y;

					currentPosition = prompt.length() + textpos;
					gotoxy(currentPosition-1, y);  
					textpos--;						
				}
				break;

			case ENTER: //Enter == '\r'

				cout &lt;&lt; &quot;ENTER&quot; &lt;&lt; endl;
				input.append(&quot;\0&quot;);
				//textpos = 0;

				break;

			case LEERTASTE: //Leertaste

				clearLineAt( prompt );
				input.insert(textpos, &quot; &quot;);					

				cout &lt;&lt; input &lt;&lt; flush;

				GetConsoleScreenBufferInfo( std_output, &amp;console_screen_buffer_info );
				y = console_screen_buffer_info.dwCursorPosition.Y;

				currentPosition = prompt.length() + textpos;
				gotoxy(currentPosition+1, y);  
				textpos++;				

				break;

			case ESCAPE: //ESC

				textpos = 0;
				clearLineAt( prompt );
				input = &quot;&quot;;

				break;

			case STEUERZEICHEN: //Steuerzeichen

				  switch( _getch( ) ) 
				  {	
					  case PFEIL_LINKS: // Pfeiltaste nach Links								  

						  if(textpos &gt; 0)
						  {		
							  GetConsoleScreenBufferInfo( std_output, &amp;console_screen_buffer_info );
							  y = console_screen_buffer_info.dwCursorPosition.Y;

							  currentPosition = prompt.length() + textpos;
							  gotoxy(currentPosition-1, y);  
							  textpos--;
						  }

						  break;

					  case PFEIL_RECHTS: // Pfeiltaste nach Rechts: 			

						  if(textpos &lt; input.length())
						  {		
							  GetConsoleScreenBufferInfo( std_output, &amp;console_screen_buffer_info );
							  y = console_screen_buffer_info.dwCursorPosition.Y;

							  currentPosition = prompt.length() + textpos;
							  gotoxy(currentPosition+1, y);  
							  textpos++;							
						  }

						  break;

					  case DELETE: //Entfernen Taste

						  if(input.length() != 0)
						  {
							  clearLineAt( prompt );
							  input.erase(textpos, 1 );
							  cout &lt;&lt; input &lt;&lt; flush;	

							  GetConsoleScreenBufferInfo( std_output, &amp;console_screen_buffer_info );
							  y = console_screen_buffer_info.dwCursorPosition.Y;

							  currentPosition = prompt.length() + textpos;
							  gotoxy(currentPosition, y); 
						  }

						  break;
				  }
				  break;

			default:

				string tmp;
				GetConsoleScreenBufferInfo( std_output, &amp;console_screen_buffer_info );
				y = console_screen_buffer_info.dwCursorPosition.Y;

				if(textpos &lt; input.length())
				{					
					clearLineAt( prompt );

					tmp = key;					
					input.insert(textpos, tmp);
					cout &lt;&lt; input &lt;&lt; flush;
				}
				else
				{	
					input += key; 
					cout.put( key );
				}			
				currentPosition = prompt.length() + textpos;
				gotoxy(currentPosition+1, y);  
				textpos++;

			}//Switch()

		}while( key != ENTER ); // enter

		cout &lt;&lt; endl;

		if( input.length( ) )
		{
			cout &lt;&lt; &quot;Input: &quot; &lt;&lt; input &lt;&lt; &quot; -&gt; &quot; &lt;&lt; textpos &lt;&lt; endl &lt;&lt; endl;				
		}

		if( input == &quot;exit&quot; ) 
		{			
			do_exit = true;
		}

	} while( !do_exit );
}
</code></pre>
<p>Noch eine kleine Hilfe bräuchte ich, das Flackern ärgert mich immernoch, ich weiß das die ganze Zeile gelöscht wird, doch wenn ich z.b. die cout &lt;&lt; &quot;\r&quot; auslase, wird die Ausgabe versetzt ausgegeben, was sollte ich nun machen damit die Prompt nur einmal ausgegeben wird bzw. nicht mehr flackert?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1323871</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1323871</guid><dc:creator><![CDATA[kernel64]]></dc:creator><pubDate>Thu, 12 Jul 2007 12:55:57 GMT</pubDate></item><item><title><![CDATA[Reply to Wie an bestimmte Position im String navigieren? on Thu, 12 Jul 2007 13:02:50 GMT]]></title><description><![CDATA[<p>Das Flackern kommt daher, daß du erst die komplette Zeile mit Leerzeichen überschreibst und anschließend deine (geänderte) Eingabe darüber schreibst - dazwischen liegen zwar nur einige Mikosekunden, aber trotzdem macht sich das bemerkbar. Die einfachste Lösung ist es wohl, nur den mit Leerzeichen zu überschreiben, der auch tatsächlich leer werden soll (das bedeutet: bei ESC die komplette Eingabe, bei Backspace und Del das letzte Zeichen der Eingabe).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1323874</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1323874</guid><dc:creator><![CDATA[CStoll]]></dc:creator><pubDate>Thu, 12 Jul 2007 13:02:50 GMT</pubDate></item><item><title><![CDATA[Reply to Wie an bestimmte Position im String navigieren? on Thu, 12 Jul 2007 13:50:04 GMT]]></title><description><![CDATA[<p>OK, ich werds mal versuchen, trotzdem vielen Dank <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>
<p>Gibts vielleicht noch was anderes zu verbesseren?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1323911</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1323911</guid><dc:creator><![CDATA[kernel64]]></dc:creator><pubDate>Thu, 12 Jul 2007 13:50:04 GMT</pubDate></item></channel></rss>