HELP!!

This is a discussion on HELP!! within the C++ Programming forums, part of the General Programming Boards category; I really could use some help with learning C++ programming . . . I'm trying to write a program that ...

  1. #1
    Registered User Virtuous1's Avatar
    Join Date
    Nov 2003
    Posts
    5

    HELP!!

    I really could use some help with learning C++ programming . . . I'm trying to write a program that needs to print my numbers right-justified in a column on the screen and I haven't been successful . . . PLEASE HELP!!

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Show us what code you have so far, and then we'll be able to do more for ya.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User Virtuous1's Avatar
    Join Date
    Nov 2003
    Posts
    5
    I've tried posting the code, however, I keep getting an error message telling me to post the code tags and I guess I'm not inserting them in the right places.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User Virtuous1's Avatar
    Join Date
    Nov 2003
    Posts
    5
    Here it is:

    Code:
    #include <iostream>
    #include <iomanip>
    using namespace std;
    
    const int INT_NUMBER = 1066;
    
    intValue_1 = 1066;
    intValue_2 = 1492;
    intValue_3 = 512;
    intValue_4 = 1;
    intValue_5 = -23;
    
    int main()
    {
    
       cout << fixed << showpoint;
       cout << setw(5) << intValue_1
    	    << setw(5) << intValue_2
    	    << setw(4) << intValue_3
    	    << setw(2) << intValue_4
    	    << setw(4) << intValue_5;
    
      return 0;
    }

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    to force right justified you can do this:

    cout.setf(ios::right);

    but I think right justification is the default. To use multiple ios flags you can do something like this, I think;

    cout.setf(ios::fixed | ios::showpoint | ios::right);

    To create a column 10 char wide with decimal representation to precision of 3, right justified you could try this:
    Code:
    float num[3];
    num[0] = 3.14159;
    num[1] = 123.4
    num[2] = 9999.9999999999999999999999999;
    
    int i;
    for(i = 0; i < 3; ++i)
    {
      cout.setw(10);
      cout.setprecision(3);
      cout.setf(ios::fixed | ios::showpoint | ios::right);
      cout << num << endl;
    }
    
    and hopefully output will be
    
         3.141
       123.400
      9999.999
    not compiled so not sure everything works as intended. You can play around with it and look up ios manipulators and ios flags in your favorite resource to try to get it all set up.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21