Thread: Right Justifying output

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109

    Right Justifying output

    I need help with right justifying some text. What I want to do is output something like this:
    Code:
    One          Two              Three
    1              $  4.99         $    3.98
    2              $ 34.56         $  905.30
    What problem I'm having is that the code is not right justifying like how I want it to in this program. The numbers are variable as they are calculated based on input from the user.

    I tried flagging cout as such:
    Code:
    cout.setf(ios::adjustfield | ios::right);
    and
    Code:
    cout.setf(ios::right);
    to try and get this output. Either way didn't work. I believe it works without the $ sign, but not with. Is there a way to get the desired output?

    Thanks.

    Edit: It does work without the $ sign, but the needs to be there as it is part of the output that the assignment calls for. Thanks.
    Last edited by alpha; 10-10-2003 at 02:59 PM.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Does this help:
    http://faq.cprogramming.com/cgi-bin/...&id=1043284385

    post again if you don't get it going
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    It's still not outputting right.

    I have these lines:
    Code:
    cout.setf(ios_base::right, ios_base::basefield);
       cout.setf(ios_base::adjustfield);
    and these lines:
    Code:
     cout << "Company" << "     " << "Beginning Balance"
          << "     " << "Service Fees" << "     " << "New Balance";
       cout << endl;
       
       cout << setw(4) << "1" << setw(12) << "$ " << balanceCo1
          << setw(14) << "$ " << serviceFeesCo1 << setw(12) << "$ " << newBalanceCo1;
       cout << endl;
       
       cout << setw(4) << "2" << setw(12) << "$ " << balanceCo2
          << setw(14) << "$ " << serviceFeesCo2 << setw(12) << "$ " << newBalanceCo2;
       cout << endl;
    or
    Code:
    cout << setw(4) << 1 << setw(12) << "$ ";
       cout << balanceCo1;
       cout << setw(14) << "$ ";
       cout << serviceFeesCo1;
       cout << setw(12) << "$ ";
       cout << newBalanceCo1;
       cout << endl;
       
       cout << setw(4) << 2 << setw(12) << "$ ";
       cout << balanceCo2;
       cout << setw(14) << "$ ";
       cout << serviceFeesCo2;
       cout << setw(12) << "$ ";
       cout << newBalanceCo2;
       cout << endl;
    doesn't give me the desired output. Thanks.

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    What's it coming out like that's wrong? An example of the output would be nice.

    Try shorting your code down to the section that's giving you grief, and post a compilable example here. As it stands, I don't know what variable types we're dealing with etc.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    1,109
    I'll probably just forget the $ signs.

    Thanks, I'll figure it out sometime else. I don't really have the time to fix the program. You could delete this thread now if you'd like.

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Well, even if you can't be bothered, I can. Is this what you want:
    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main(void)
    {
      float balanceCo1 = 1.1, 
            serviceFeesCo1 = 12.2, 
            newBalanceCo1 = 113.3, 
            balanceCo2 = 224.4, 
            serviceFeesCo2 = 335.5,
            newBalanceCo2 = 446.6;
    
      cout.precision (2);
      cout.setf(ios_base::right, ios_base::basefield);
      cout.setf(ios_base::fixed, ios_base::floatfield);
      
      cout << setw(4) << 1 << setw(12) << "$ ";
      cout << setw(8) << balanceCo1;
      cout << setw(14)<< "$ ";
      cout << setw(8) << serviceFeesCo1;
      cout << setw(12)<< "$ ";
      cout << setw(8) << newBalanceCo1;
      cout << endl;
    
      cout << setw(4) << 2 << setw(12) << "$ ";
      cout << setw(8) << balanceCo2;
      cout << setw(14) << "$ ";
      cout << setw(8) << serviceFeesCo2;
      cout << setw(12) << "$ ";
      cout << setw(8) << newBalanceCo2;
      cout << endl;
    }
    
    
    /*
    
    Output
    
      1          $     1.10            $    12.20          $   113.30
      2          $   224.40            $   335.50          $   446.60
      
      */
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code output...
    By roaan in forum C Programming
    Replies: 6
    Last Post: 07-03-2009, 02:22 AM
  2. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 03:08 PM
  3. Replies: 4
    Last Post: 11-30-2005, 04:44 PM
  4. Formatting output into even columns?
    By Uncle Rico in forum C Programming
    Replies: 2
    Last Post: 08-16-2005, 05:10 PM
  5. Output problems with structures
    By Gkitty in forum C Programming
    Replies: 1
    Last Post: 12-16-2002, 05:27 AM