Question

This is my first post on SO, my first month of programming, and I'm learning on my own, so please be nice :) Here goes!

In a nutshell, I'm looking to have right-aligned input of currency amounts. I'm using getline(), by the way, so that I can validate the input. Can anyone help? Thanks!!

Rationale, details:

Sometimes this:

    $  12345.67
    $     45.30
    $      1.01
    

Is easier to read than:

    $ 12345.67
    $ 45.30
    $ 1.01
    

I think I figured out how to do this when OUTPUTTING:

cout << setfill(' ') << setw(40) << right << "Dr., SUBTOTAL:";
cout << " $";
cout << setw(10) << right << sum << endl;

Which looks like:

                           Dr., SUBTOTAL $     12.34
or
                           Dr., SUBTOTAL $   1234.56
Perfect! (I think.)

But when the user is being prompted to input a dollar amount, I can't figure out how to make it behave like a calculator, which is to say right-align whatever digits have been entered thusfar. A "stop-motion animation" of typing 123.45 it would look like:

    Dr. Cash.......$     1
    Dr. Cash.......$    12
    Dr. Cash.......$   12.
    Dr. Cash.......$  12.3
    Dr. Cash.......$ 12.34
Hope that made sense.

UPDATE: Running Windows, Code::Blocks 13.12, GNU GCC, C++11

Was it helpful?

Solution

You can't do this strictly with cout/cin and getline. You need something like the curses library http://en.wikipedia.org/wiki/Curses_(programming_library) and you need to capture each key separately and redraw the line.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top