formatting output

This is a discussion on formatting output within the C++ Programming forums, part of the General Programming Boards category; <edit> i gave old code by accident. here is the actual code </edit> here is the code i am concerned ...

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    12

    formatting output

    <edit>
    i gave old code by accident. here is the actual code
    </edit>

    here is the code i am concerned with. the formatted output looks like it is correct after you run the program, but i don't know if this is how it should be done. i was getting a few warning messages so i thought i would ask the experts. i also attached a zip file with the entire program and input/output files.

    Code:
    //Print heading to log file program5.out
    fout<<STARS<<endl;
    fout<<setw(40-storeName.length())<<storeName<<endl;
    fout<<STARS<<endl;
    fout<<"Product Code   Description                    Unit Price"<<endl;
    fout<<"************   *************************      **********"<<endl;
    fout<<setiosflags(ios::fixed|ios::showpoint)<<setprecision(2);
    
    
    //Format file output with setw and length
    fout<<setw(5)<<productCode<<setw(14-productCode.length()+descriptions[index].length())<<descriptions[index]<<setw(42-descriptions[index].length())<<price[index]<<endl;
    }
    
    else
    {
       cout<<"***ERROR: PRODUCT CODE "<<productCode<<" is not in list."<<endl;
       fout<<setw(5)<<productCode<<"          Invalid Product Code"<<endl;
    }
    i put the heading code there so you could get an idea of what i am trying to get the output to look like. if you don't understand you could just run the program i attached and see what i am trying to do. like i said...the way i did it looks right, but i'm not sure if it is because of warning messages. any help is greatly appreciated.
    Last edited by z.tron; 11-22-2002 at 12:33 PM.

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    beware of setprecision(). Say the cost of a product is 59.693 and you intend to round to the closest .01 to get 59.69. if you use setprecision(2) you will end up with 60.00 instead.

  3. #3
    Registered User
    Join Date
    Jul 2002
    Posts
    12
    here is new code attached
    Attached Files Attached Files

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    12
    Originally posted by elad
    beware of setprecision(). Say the cost of a product is 59.693 and you intend to round to the closest .01 to get 59.69. if you use setprecision(2) you will end up with 60.00 instead.
    i don't think i have to worry about that yet. all of the prices in this list are something like

    78.99
    132.99
    39.99
    etc...

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    learned some new stuff about setprecision:

    setprecision will round values.

    setprecision without the fixed flag counts the total number of digits.

    setprecision with the fixed flag set counts only the digits after the decimal point.

    Therefore:

    double cost = 59.693;
    cout << setflags(ios::fixed|ios::showpoint);
    cout << setprecision(2) << cost;

    should yield 59.69

    Thanks for the lesson of the day!

  6. #6
    Registered User
    Join Date
    Jul 2002
    Posts
    12
    can i have some input from anybody that is familiar with this? is what i did correct? i mean i know it looks right when it actually writes to the file, but i just kept guessing the setw until it looked right. it doesn't really have any logic behind it. i wrote another program that had formatting similar and i counted the stars and then substracted that from the length of whatever came before it. any help is appreciated.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch statement and returning the correct output
    By slowcoder in forum C Programming
    Replies: 6
    Last Post: 09-28-2006, 04:00 PM
  2. Formatting output to screen from a file
    By clearrtc in forum C Programming
    Replies: 2
    Last Post: 08-20-2006, 03:19 PM
  3. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  4. dos game help
    By kwm32 in forum Game Programming
    Replies: 7
    Last Post: 03-28-2004, 05:28 PM
  5. formatting output
    By spliff in forum C Programming
    Replies: 2
    Last Post: 08-14-2001, 06:50 PM

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