Aligning a cin to the right

How to properly use setw with cout and cin
How to align the output of cin to the left by 30
1
2
3
4
5
6
 
cout<<" Example for Cplusplus Forums  ";
cin>> output;


<<setw(33) no idea where to put or how to put the setw properly
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <iomanip>

int main()
{
    double d = 123.45678 ;

    std::cout << '|' << std::fixed << std::setprecision(3) << d << "|\n" ; // |123.457|

    std::cout << '|' << std::left << std::setw(15) << d << "|\n" ; // |+123.457       |

    std::cout << std::showpos << std::setfill('0')  ;

    std::cout << '|' << std::left << std::setw(15) << d << "|\n" ; // |+123.4570000000|

    std::cout << '|' << std::right << std::setw(15) << d << "|\n" ; // |0000000+123.457|

    std::cout << '|' << std::internal << std::setw(15) << d << "|\n" ; // |+0000000123.457|
}

http://coliru.stacked-crooked.com/a/b2d1dab33f219291
No I got it for cout, but I am trying to figure out how to align the cin's for example.

cout blah blah
1
2
3
4
5
cin                                                23
cin                                                457
cin                                               cplusplus.com

I know this isnt code, I did it for spacing reasons.



I don't know how to do the setw for the cin
Last edited on
Formatted input skips leading white space characters.

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
#include <iostream>
#include <iomanip>
#include <string>

int main()
{
    std::string str ;

    std::cin >> str ; // enter 123456789
    std::cout << str << '\n' ; // 123456789

    std::cin >> std::setw(3) >> str ; // enter 123456789
    // reads "123", "456789\n" remains in the input buffer
    std::cout << str << '\n' ; // 123

    char cstr[32] ;
    std::cin >> std::setw(3) >> cstr ; // we don't need to enter anything; "456789\n" is in the input buffer
    // reads "45" from the input buffer, and appends a null character as the third character
    // "6789\n" remains in the input buffer
    std::cout << cstr << '\n' ; // 45

    std::cin >> std::setw(8) >> str ; // we don't need to enter anything; "6789\n" is in the input buffer
    // reads four characters "6789", formatted input stops when a white space is encountered
    // a new line character remains in the input buffer
    std::cout << str << '\n' ; // 6789

    int n ;
    std::cin >> std::setw(4) >> n ; // enter 123456789
    // setw (width) is ignored for numeric input, reads "123456789" into n
    std::cout << n << '\n' ; // 123456789
}

http://coliru.stacked-crooked.com/a/c47cffda43d83eb9
Last edited on
I am not sure I read any of that correctly. It may still be early for me :-/
1
2
3
4
5
6
7
8
9
I am looking for print fields. ie.

int exp;
cout<<"example here"<<setw(7)<<exp;

example here _ _ _ _ _ exp

or is that all wrong?
But aligning the Cin so its to the right
Last edited on
Anyone have an Idea on how to approach this?
What do you want to print? And what should the output look like?
You need to give an example with actual data to be printed and the desired output format.
Topic archived. No new replies allowed.