need another help

i got another problem with my program here's the code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
   cout << "-----------------------------------------------------------------"
         << "---------------";
    cout << "\t\t\t\t DEBTORS LIST\n";
    cout << "-----------------------------------------------------------------"
         << "---------------";
    cout << "\n\t\t  Name";
    cout << "\t\t\t  Amount of Debt\n\n";

    for (int j =0; j < 5;j++)
    {

       cout << setw(21) << debtors[j].name << setw(30)
            << debtors[j].amount << "\n\n";
    }

this is just a part of my program.. the problem is that it outputs like this.
1
2
3
4
5
6
7
8
9
10
11
12

                              DEBTORS LIST

---------------------------------------------------------------------------------------------------

              Name                                    Amount of Debt

           ried azmel                                      333444
        laala lalalal                                     0000000
              waa waa                                       34444
          who are you                                           4
              I am me                                         343


the name and amount are just an example my problem is the positions. I wanted it to look like this..
1
2
3
4
5
6
7
8
9
10
11
12

                                   DEBTORS LIST

---------------------------------------------------------------------------------------------------

                    Name                             Amount of Debt

                  ried azmel                             333444
                  laala lalalal                          0000000
                  waa waa                                34444
                  who are you                            34
                  I am me                                343



whats the problem with my code? mmmm
Last edited on
hi !
u just have to includ
for (int j =0; j < 5;j++)
{

cout << left<<setw(21) << debtors[j].name << left<<setw(30)
<< debtors[j].amount << "\n\n";
}

i didnt run it but check imay work .:).
mmmmmmm thnx.. but it didnt work... ill show you the code next time.. =)
Last edited on
hi !
i ll be waiting 4 the code but i undrestood is that you wanna jusfied the output to the right so here come the job of left AND rigth manipulator,
snd me the whole program so i can run it and see cause this one is incomplet. :).
mmmm ok... heres the code.:
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
#include <iostream>
#include <iomanip>

using namespace std;

struct client
{
    string name;
    double amount;
};

int main ()
{
    client debtors[5];
    double total = 0;


    cout << "-----------------------------------------------------------------"
         << "---------------";
    cout << "\t\t A Program That Computes The Debts of Users.\n";
    cout << "-----------------------------------------------------------------"
         << "---------------";


    cout << "Press " << "ENTER" << " to start the program.\n";

    for (int i=0; i < 5; i++)
    {


        cin.clear();
        cin.ignore (500,'\n');
        cout << "Enter name: ";
        getline (cin,debtors[i].name);

        cout << "\nEnter amount of debt: ";
        cin >> debtors[i].amount;

        //computation
        total += debtors[i].amount;

        system("cls");

    }

    system ("cls");


    cout << "-----------------------------------------------------------------"
         << "---------------";
    cout << "\t\t\t\t DEBTORS LIST\n";
    cout << "-----------------------------------------------------------------"
         << "---------------";
    cout << "\n\t\t  Name";
    cout << "\t\t\t  Amount of Debt\n\n";

    for (int j =0; j < 5;j++)
    {

       cout << setw(21) << debtors[j].name << setw(30)
            << debtors[j].amount << "\n\n";
    }


    return 0;

}


ill be waiting.. =)
Last edited on
I think you have to use coordinates man. Now I'm not sure what the code is but
go to google and search for "coordinates in C++"

You'll get alot of hits right away, so I quess you'll be fine

Hope it helped a bit
Hi all,

Kia was _almost_ exactly right about using "left" to justify the text, only problem is that you should use "setw() << left" NOT "left << setw()".

You also only needs to use "left" once -- all subsequent calls to setw() will then be left justified. To get back to right formatting use "setw() << right";

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <iomanip>

using namespace std;

int main(int argc, char *argv[])
{
    
    cout << setw(20) << left << "hi" << setw(20) << "but" << "\n";
    cout << setw(20) << "mum" << setw(20) << "next" << "\n";
    cout << setw(20) << "thanks for" << setw(20) << "Christmas I'd" << "\n";
    cout << setw(20) << "the socks" << setw(20) << "like a monkey!" << "\n\n";   
    
    cout << setw(20) << right << "hi" << setw(20) << "but" << "\n";
    cout << setw(20) << "mum" << setw(20) << "next" << "\n";
    cout << setw(20) << "thanks for" << setw(20) << "Christmas I'd" << "\n";
    cout << setw(20) << "the monkey" << setw(20) << "like a gun!" << "\n\n";              
    system("PAUSE");
    return EXIT_SUCCESS;
}


This compiles and runs okay under Dev-Cpp 4.9.9.2

There's a few more formating codes you could also try -- there's a good reference here;

http://www.fredosaurus.com/notes-cpp/io/omanipulators.html

and a code example here;

http://www.cs.niu.edu/~freedman/courses/cs240/just-short2.cpp

Hope it helps,
muzhogg
Last edited on
but that doesnt run in my code muzhogg... =(
what error are you getting?
I'm guessing that your problem is still connected with formatting (?) and that the problem is that your output looks something like this;

1
2
3
4
5
6
                    Name                                 Amount of Debt
John                                456
Steve                               34
Tim                                 1567
Peter                               22
Andrew                              11082


If so, you need to insert a field to "pad out" your text. Do this by adding;

 
  << setw(fieldwidth) << " " 


to the start of your cout statement in your for() loop. This should give you something like this;

1
2
3
4
5
6
    for (int j =0; j < 5;j++)
    {
        cout << setw(20) << " " << 
             setw(18) << left << debtors[j].name << 
             setw(30) << debtors[j].amount << "\n\n";
    }


Does this help?

If not, we'll have to dig a bit deeper. Repost your modified code if the above doesn't fix your problem.
Last edited on
oh thnx muzhogg.. =) it did work.. thank you thank you a lot..
and i think i dont need to setw at the debtors[j].amount.. :
1
2
3
4
5
6
for (int j =0; j < 5;j++)
    {
       cout << setw(16) << " " << setw(30)
            << left << debtors[j].name
            << debtors[j].amount << "\n\n";
    }
Now that you mention it, you're right. Using setw() for debtors[].amount is unnecessary because the output is left justified.

For what it's worth, the following might help to picture what's going on here (or perhaps you've already got it?);

If you think of setw() as doing something similar to defining the column widths of a table, then defining the width of the debtors[].amount field gives;

1
2
3
4
5
6
7
  first "column"           second "column"          third "column"
  defined by the           defined by the           defined by the
  first set(w)             second set(w)            third set(w)
X========================X========================X========================X
X " "                    X debtors[j].name        X debtors[j].amount      X 
X========================X========================X========================X
 


I think it should be easy to see that if the contents of the third "column" are left justified, then it doesn't really matter whether this "column" exists or not. What you've done is essentially the following -- and it works just as well;

1
2
3
4
5
6
7
  first "column"           second "column"          no third "column"
  defined by the           defined by the           
  first set(w)             second set(w)            
X========================X========================X
X " "                    X debtors[j].name        X debtors[j].amount  
X========================X========================X


Only if you wanted to output another value after debtors[].amount would you need to add another "column" to this imaginary table (you need to keep in mind that this talk of tables and columns is just a helpful way of thinking about what set(w) is doing).

The reason the you were still having formating problems before adding ' << setw(fieldwidth) << " " ' can be seen if you remove the first "column";

1
2
3
4
5
6
7
  first "column"           second "column"          
  defined by the           defined by the           
  first set(w)             second set(w) 
X========================X========================X
X debtors[j].name        X debtors[j].amount      X 
X========================X========================X
 


Here the output of the first field, being left justified, pushes it right up against the edge of the output window.

All that said, you don't even need the first setw() - you could set the position of the debtors[].name field simply by padding the output with spaces;

1
2
3
4
5
6
    for (int j =0; j < 5;j++)
    {
        cout << "                           " << 
             setw(18) << left << debtors[j].name << 
             debtors[j].amount << "\n\n";
    }


which gives;

1
2
3
4
5
6
7
  first "column"           second "column"          no third "column"
  defined by               defined by 
  spaces                   set(w)            
"                        "X========================X
"                        "X debtors[j].name        X debtors[j].amount 
"                        "X========================X
 


Anyway, glad it all worked out in the end!
Last edited on
thnx muzhogg.. =) I do understand it now... and I hope it would also help a lot of beginners in this forum or guess that would somehow visit this site..
Topic archived. No new replies allowed.