Hallo Leute, ich möchte eine eigene getline() Funktion schreiben, d.h. ich muss alles manuell mit getch() machen und auf einzelne Tasten reagieren wie ENTER, LEERTASTE usw.
Soweit so gut, einzelne Zeichen werden am ende zum string angehängt, dabei wird immer die textposition des Cursor hoch oder runtergezählt (BACKSPACE).

Ich habe noch paar Probleme mit meinem Code, z.B. wenn ich BACKSPACE bis zum anfang der zeile alles lösche bekomme ich ein Laufzeitfehler sowie bei der LEERTASTE, außerdem wird der string nicht richtig angezeigt, sondern versetzt.

Schauts euch bitte mal an und hoffentlich könnt ihr mir weiterhelfen:

Code :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <windows.h>
#include <string>
#include <iostream>
#include <conio.h>
 
#define ENTER            13
#define BACKSPACE        8
#define TAB                 9
#define LEERTASTE        32
#define ESCAPE            27
#define STEUERZEICHEN    224
#define PFEIL_LINKS        75
#define PFEIL_RECHTS    77
#define PFEIL_OBEN        72
#define PFEIL_UNTEN        80
#define DELETE            83
 
using namespace std;
 
 
void clearLine(int LineSize)
{
    cout.put( '\r' );
 
    for( string::size_type i = 0; i < LineSize; ++i )
    {
        cout.put( ' ' );
    }
}
 
void gotoxy(short x, short y)
{
    HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE);
    COORD pos;
    pos.X = x;
    pos.Y = y;
    SetConsoleCursorPosition(hCon, pos);
}
 
string GetKeyInputString(string Prompt)
{
    int x, y, key, currentPosition, textpos = 0;
    string input;
    int LineSize;
 
    //Handle für x,y Koordinaten
    HANDLE std_output = GetStdHandle( STD_OUTPUT_HANDLE );
    CONSOLE_SCREEN_BUFFER_INFO console_screen_buffer_info;  
 
    do
    {
        key = _getch( );
        switch(key)
        {            
 
        case BACKSPACE:
            if(textpos > 0)
            {                
                input.erase(textpos-1, 1);            
                cout.put(' ');
                                
                GetConsoleScreenBufferInfo( std_output, &console_screen_buffer_info );
                y = console_screen_buffer_info.dwCursorPosition.Y;
 
                currentPosition = Prompt.length() + textpos;
                gotoxy(currentPosition-1, y); 
                textpos--;
                
 
            }break;
 
        case ENTER:        
 
            if(input.length() != 0)
                input.append("\0");
            break;
            
        case LEERTASTE:
            
            input.insert(textpos, " "); 
            cout << input << flush;
 
            GetConsoleScreenBufferInfo( std_output, &console_screen_buffer_info );
            y = console_screen_buffer_info.dwCursorPosition.Y;
 
            currentPosition = Prompt.length() + textpos;
            gotoxy(currentPosition+1, y);  
            textpos++;                           
 
            break;
 
        case ESCAPE:
 
            LineSize = Prompt.length() + input.length();
            clearLine(LineSize);
            cout << '\r' << Prompt << flush;   
            textpos = 0;
            input.clear();
            break;
 
        case STEUERZEICHEN:
 
            switch( _getch() )
            {    
                
            case DELETE:
                if(input.length() > 0)
                {                    
                    input.erase(textpos, 1 );
                    
                    GetConsoleScreenBufferInfo( std_output, &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, &console_screen_buffer_info );
            y = console_screen_buffer_info.dwCursorPosition.Y;
 
            //An bestimmter Stelle im String wird neues Zeichen eingefügt
            if(textpos < input.length() )
            {        
                tmp = key;
                input.insert(textpos, tmp);
                cout << input << flush;
            }
            
            else
            {                    
                input += key;
                cout.put( key );
            }
            currentPosition = Prompt.length() + textpos;
            gotoxy(currentPosition+1, y);  
            textpos++;
        }
    }while( key != ENTER );
 
    cout << endl;
 
    if( input.length() != 0)
    {                
        cout << "Input: [" << input << "] CursorPos: " << textpos << endl << endl; 
        return input;        
    }
    return "";
}
 
int main()
{    
    bool do_exit = false;
    string Prompt = ":\\>";
    string GetInput;
 
    do
    {
        cout << Prompt;
        GetInput = GetKeyInputString(Prompt);
        if(GetInput == "exit")
            do_exit = true;        
 
    }while( !do_exit );
 
 
    return 0;
}