• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 242
  • Last Modified:

How to align numbers in an array

I have 2 arrays that produce this output

joe 10
terry 23
bobby 45

how can I align the numbers so it looks like this

joe      10
terry   23
bobby 45

now I don't know what numeric values will be entered so each time the program runs different numbers could be entered  so it may not just be 2 digits may be 1 digit or 3 digits or digit with decimal point
0
WestonGroup
Asked:
WestonGroup
  • 3
  • 2
1 Solution
 
Infinity08Commented:
>> how can I align the numbers

One option is to use tabs instead of spaces :

        cout << name << "\t" << value << endl;

Another is to use setw :

        #include <iomanip>

        cout << setw(20) << name << setw(10) << value << endl;
0
 
Infinity08Commented:
Oops, forgot left in the last example to align left :

        #include <iomanip>

        cout << left << setw(20) << name << setw(10) << value << endl;


The respective reference pages :

        http://www.cplusplus.com/reference/iostream/manipulators/setw.html
        http://www.cplusplus.com/reference/iostream/manipulators/left.html
0
 
WestonGroupAuthor Commented:
what about for the input
cout << name <<endl;
cin  >> age ;

name is array
age is array
they are both in a loop for say 12 names

so on the screen you see the input can you format that as well?

0
 
WestonGroupAuthor Commented:
nevermind figured it out

thanks
0
 
Infinity08Commented:
>> nevermind figured it out

Nice ;)
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now