User Input Starts from Right To Left (Closer to Solution)

This question is actually a follow up on how to get the user input start from right to left. The idea is to simulate a Windows calculator where every time a user enters a number, this number gets moved to the left by the next one and so on. I found a code by searching through this forum that actually seems to provide that functionality but I made small modifications to accomplish my task. This is just the first draft so it needs to be modified even further but so far the idea is working when I enter numbers from 0 to 9. Now the problem is that it only works for this sequence of numbers but not for others. For example if I type the number "3", the console obviously reads "0123" instead of just "3". Or if I type a "0" and then a "1", the "0" doesn't disappear like in regular calculators. I know this is logical because this is just a draft. But can somebody give me any idea on how to fix these problems? This is driving me crazy and I need this for tomorrow! I would immensely appreciate it.

This is the 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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#include <iostream>
#include <iomanip>
#include <windows.h>
using namespace std;

struct console_t
  {
  DWORD  mode;
  HANDLE hstdin;
  HANDLE hstdout;

  console_t()
    {
    hstdin  = GetStdHandle( STD_INPUT_HANDLE  );
    hstdout = GetStdHandle( STD_OUTPUT_HANDLE );
    GetConsoleMode( hstdin, &mode );
    }

  ~console_t()
    {
    if (hstdin != INVALID_HANDLE_VALUE)
      SetConsoleMode( hstdin, mode );
    }

  bool iskeypressed( unsigned timeout_ms = 0 ) const
    {
    return WaitForSingleObject( hstdin, timeout_ms ) == WAIT_OBJECT_0;
    }

  int getkeypress() const
    {
    INPUT_RECORD inrec;
    DWORD        count;

    if (hstdin == INVALID_HANDLE_VALUE) return -1;
    SetConsoleMode( hstdin, 0 );

    do ReadConsoleInput( hstdin, &inrec, 1, &count );
    while ((inrec.EventType != KEY_EVENT) || inrec.Event.KeyEvent.bKeyDown);

    SetConsoleMode( hstdin, mode );

    return inrec.Event.KeyEvent.wVirtualKeyCode;
    }

  void gotoxy( int column, int line )
    {
    COORD coord;
    coord.X = column;
    coord.Y = line;
    SetConsoleCursorPosition( hstdout, coord );
    }

  void clearscreen()
    {
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    DWORD                      count;
    DWORD                      cellCount;
    COORD                      homeCoords = { 0, 0 };

    if (hstdout == INVALID_HANDLE_VALUE) return;

    if (!GetConsoleScreenBufferInfo( hstdout, &csbi )) return;
    cellCount = csbi.dwSize.X * csbi.dwSize.Y;

    if (!FillConsoleOutputCharacter(
      hstdout,
      (TCHAR) ' ',
      cellCount,
      homeCoords,
      &count
      )) return;

    if (!FillConsoleOutputAttribute(
      hstdout,
      csbi.wAttributes,
      cellCount,
      homeCoords,
      &count
      )) return;

    SetConsoleCursorPosition( hstdout, homeCoords );
    }
  };

int main()
  {
  console_t console;
  int key = 0;

  console.clearscreen();

  while (key != '3')
    {
    console.gotoxy( 0, 0 );
    key = console.getkeypress();
    /*Below is the part that needs to be more efficient....*/
    if(key == '0') {
        key = 0;
        cout << setw(11) << key;
    }
    else if(key == '1') {
        key = 0;
        cout << setw(10) << key;
        key = 1;
        cout << key;
    }
    else if(key == '2') {
        key = 0;
        cout << setw(9) << key;
        key = 1;
        cout << key;
        key = 2;
        cout << key;
    }
    else if(key == '3') {
        key = 0;
        cout << setw(8) << key;
        key = 1;
        cout << key;
        key = 2;
        cout << key;
        key = 3;
        cout << key;
    }
    else if(key == '4') {
        key = 0;
        cout << setw(7) << key;
        key = 1;
        cout << key;
        key = 2;
        cout << key;
        key = 3;
        cout << key;
        key = 4;
        cout << key;
    }
    else if(key == '5') {
        key = 0;
        cout << setw(6) << key;
        key = 1;
        cout << key;
        key = 2;
        cout << key;
        key = 3;
        cout << key;
        key = 4;
        cout << key;
        key = 5;
        cout << key;
    }
    else if(key == '6') {
        key = 0;
        cout << setw(5) << key;
        key = 1;
        cout << key;
        key = 2;
        cout << key;
        key = 3;
        cout << key;
        key = 4;
        cout << key;
        key = 5;
        cout << key;
        key = 6;
        cout << key;
    }
    else if(key == '7') {
        key = 0;
        cout << setw(4) << key;
        key = 1;
        cout << key;
        key = 2;
        cout << key;
        key = 3;
        cout << key;
        key = 4;
        cout << key;
        key = 5;
        cout << key;
        key = 6;
        cout << key;
        key = 7;
        cout << key;
    }
    else if(key == '8') {
        key = 0;
        cout << setw(3) << key;
        key = 1;
        cout << key;
        key = 2;
        cout << key;
        key = 3;
        cout << key;
        key = 4;
        cout << key;
        key = 5;
        cout << key;
        key = 6;
        cout << key;
        key = 7;
        cout << key;
        key = 8;
        cout << key;
    }
    else if(key == '9') {
        key = 0;
        cout << setw(2) << key;
        key = 1;
        cout << key;
        key = 2;
        cout << key;
        key = 3;
        cout << key;
        key = 4;
        cout << key;
        key = 5;
        cout << key;
        key = 6;
        cout << key;
        key = 7;
        cout << key;
        key = 8;
        cout << key;
        key = 9;
        cout << key;
    }

    }

  return 0;
  }
Try this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
  {
  console_t console;
  int key = 0;

  console.clearscreen();

  int pos = 10;

  while (key != '3')
    {
    console.gotoxy( pos, 0 );
    key = console.getkeypress();
   
    cout << (char)key;
    pos--;

    }

  return 0;
  }

Note that you'll have to deal with lines too. Also, your input isn't great. You are printing key codes, instead of characters (even though they are equal most of the time, now you only have capitals, and num pad won't work). Sadly I don't know of a good way to fix that..
Topic archived. No new replies allowed.