How to make the user input start from right to left?

I'm currently building a calculator and using cin to get the user input. But I don't know how to align what the user types into the console. Just like a Windows calculator, basically I want the numbers to start from right to the left.

For example:

The one is the first number to be typed and is displaced to the
left as the second number is typed.
<--------
123456789

But so far, the numbers start from the left to the right:
-------->
123456789

Therefore, how can I make the user input start from right to left? Any ideas would be greatly appreciated.
Last edited on
I don't know if there's any way of doing that. I might be wrong though. Write your own console? :P
You'll have to SetConsoleCursorPosition() to where your display area is, then output a properly padded string, for example cout << setfill( ' ' ) << right << setw( CALC_DISPLAY_AREA_WIDTH ) << number;
Every time the user types a new digit of the number, you'll have to reposition the cursor and display again.

Oh, you did turn off ECHO and LINE BUFFERING, right?
SetConsoleMode( GetStdHandle( STD_INPUT_HANDLE ), 0 );
Make sure to restore the mode to what it was when your program started, or your users will hate you. (When your program starts, use GetConsoleMode(), and just before it ends, set it back.)

http://www.google.com/search?btnI=1&q=msdn+Console+Functions

Mind, if this is homework, and your professor uses Linux, none of this MS stuff will help you...
You'll need to either accept that you'll have to take it as it is, or code for both platforms.

Good luck!
Thanks Duoas. I don't know if it is too much to ask but could you please provide me with a small example where the numbers (input) would start from right to left? I found the following code that could provide me with some understanding but I still don't know how to implement the int main() method. Once again I would greatly appreciate it.

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
#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(){
//What does it go here?
}


Topic archived. No new replies allowed.