add space between array strings

I'm trying to find a easier way to add space between the Client and the Business type i tried using setw() in the array and in the first cout i had no luck. How can i add space between the strings and keep them in their columns instead of spacing out the array like i did in this program. I gotthis program format fromthis site.
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
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

void bubbleSort(string[], int);

int main ()
{
  int y = 0;
  string Client[12] =  { "Acme Construction                Machinery design", "Johnson Electrical               Switch manufacturing", "Brown Heating and Cooling        Boiler design", "Smith Switches                   Switch manufacturing",  "Jones Computers                  Computers sales", 
	                     "Williams Cleaning                Equipment Machinery sales" };
  cout << "Clients"<< "                          Business types" << endl;
  for (int i=0; i<12; i++)
	 cout << Client[i] << endl; 
     
  return 0;
}

void bubbleSort(string arr[], int n)
{
   bool swapped = true;
   int j = 0;
   string tmp;

   while (swapped)
   {
      swapped = false;
      j++;
      for (int i = 0; i < n - j; i++)
      {
         if ( arr[i].compare(arr[i - 1]) )
         {

            tmp = arr[i];
            arr[i] = arr[i - 1];
            arr[i - 1] = tmp;
            swapped = true;


         }
      }
   }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
1
1>Deleting intermediate and output files for project 'WeekSixProgram', configuration 'Debug|Win32'
1>Compiling...
1>WeekSixProgram.cpp
1>Compiling manifest to resources...
1>Microsoft (R) Windows (R) Resource Compiler Version 6.1.6723.1
1>Copyright (C) Microsoft Corporation.  All rights reserved.
1>Linking...
1>LINK : C:\Users\Martin\Documents\Visual Studio 2008\Projects\WeekSixProgram\Debug\WeekSixProgram.exe not found or not built by the last incremental link; performing full link
1>Embedding manifest...
1>Microsoft (R) Windows (R) Resource Compiler Version 6.1.6723.1
1>Copyright (C) Microsoft Corporation.  All rights reserved.
1>Build log was saved at "file://c:\Users\Martin\Documents\Visual Studio 2008\Projects\WeekSixProgram\WeekSixProgram\Debug\BuildLog.htm"
1>WeekSixProgram - 0 error(s), 0 warning(s)
========== Rebuild All: 1 succeeded, 0 failed, 0 skipped ==========
It is possible, but I can tell you it is easier just to add space manually like you have done.
The answer is setw:
http://www.cplusplus.com/reference/iostream/manipulators/setw/

If you need to perform the operations into a string, rather than directly to cout, use a stringstream as an intermediary:
http://www.cplusplus.com/reference/iostream/stringstream/
Last edited on
thank you for your feedback.
I don't see how setw is going to help you with that problem if you wish for the client and business type to be part of the same string. You should use \t characters (tabs) by inserting \t instead of spaces between the strings.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;

int main()
{
    stringstream ss;
    ss << setw( 10 ) << "Column 1" << setw( 10 ) << "Column 2" << endl;
    int num = 100;
    string s = "asdf";
    ss << setw( 10 ) << num << setw( 10 ) << s << endl;
    cout << ss.str() << endl;
    return 0;
}
  Column 1  Column 2
       100      asdf
Topic archived. No new replies allowed.