Spacing problem in code that sorts names using parallel arrays

I have some code that sorts names using parallel arrays, however there are too few spaces in between the 2 columns of names. The expected output of the program:
http://tonynoname.com/i/c67.png
and my output:
http://tonynoname.com/i/ea9.png
An example is that there are 3 spaces between Kell and Joe, but there should be 5.
My code is:
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include <iostream>
#include <string>

using namespace std;
int len(string s)  {
      int i=0;
      while(s[i]!=0)
              ++i;
      return i;
}

bool less_str(string s1, string s2)  {
      int i=0;
      while (s1[i]!=0 && s2[i]!=0)  {
              if (toupper(s1[i]) < toupper(s2[i]))
                      return true;
              if (toupper(s1[i]) > toupper(s2[i]))
                      return false;
              ++i;
      }
      if (s1[i]==0)
              return true;
      return false;
}
int main()  {
      string children[512], parents[512];
      cout << "This program prints parent-child pairs.\nEnter parents and children below, use 'quit' to stop.\n";
      int n=0;
      while(1)  {
              string tmp, tmpc, tmpp;
              cout << "Parent: ";
              getline(cin,tmpp);
              if (tmpp == "quit") 
                      break;
              cout << "Children of "<< tmpp <<": ";
            //  cin.ignore();
              getline(cin,tmpc);
              int k = 0;
              while (tmpc[k]!=0) {
                      tmp="";
                      while(tmpc[k]!=0 && tmpc[k]!=' ')  {
                              tmp += tmpc[k];
                              ++k;
                      }
                      if (tmpc[k]==' ')
                              ++k;
                   parents[n] = tmpp;
                      children[n] = tmp;
              
                      ++n;
              }
      }
      for (int i=0; i<n; ++i)  {
              bool in=false;
              for (int j=0; j<n; ++j)
                      if(children[j]==parents[i])  {
                              in=true;
                              break;
                      }
                      
              if (!in)  {
                      children[n]=parents[i];
                      parents[n]="";
                      ++n;
              }                         
      }
for (int i=1; i<n-1; ++i)  {
              int j = i;
               while (j>0 && less_str(children[j], children[j-1]))  {
                       string t1=children[j], t2=parents[j];
                       children[j]=children[j-1];
                       parents[j]=parents[j-1];
                       children[j-1]=t1;
                       parents[j-1]=t2;
                       --j;
               }
       }
       int longest_child=5;
       for (int i=0; i<n; ++i) 
               if (len(children[i]) > longest_child)
                       longest_child = len(children[i]);
       ++longest_child;
       cout << endl;
       string out ="";
       out += "Child";
       for (int i=5; i<longest_child; ++i)
               out+=" ";
       out += "Parent\n-----";
       for (int i=5; i<longest_child; ++i)
               out+=" ";
       out+="------\n";
       cout<<out;
       for (int i=0; i<n; ++i)  {
               out="";
               out += children[i];
               for (int j=len(children[i]); j<longest_child; ++j)  
                       out+=" ";
               out += parents[i] + "\n";
               cout<<out;
       }
       return 0;
}

Please help me fix this.
Last edited on
http://www.cplusplus.com/reference/iomanip/setw/

std::setw, std::left, std::right
I know about those, but what specifically do I do with them and where?
bump
Do you claim that you cannot read/understand the explanations and examples that are within the reference documentation?
1
2
3
4
constexpr auto longest = 12;
std::cout << std::left;
std::cout << std::setw(longest) << "Hello" << "World\n";
std::cout << std::setw(longest) << "foo" << "bar\n";
No, I understand that fine. But I want the finished code with those put in the correct places.
If you do understand both the manipulators and your code, then you should have no problem in doing that yourself.
But I want them put in the right places.
Topic archived. No new replies allowed.