formatting output with cout

This is a discussion on formatting output with cout within the C++ Programming forums, part of the General Programming Boards category; i need to format the output of my program so that it's like this: Total=______ 1232.3 Count=________ 2 Average=_____616.2 the ...

  1. #1
    Registered User boojus's Avatar
    Join Date
    Oct 2003
    Posts
    9

    Question formatting output with cout

    i need to format the output of my program so that it's like this:

    Total=______ 1232.3
    Count=________ 2
    Average=_____616.2


    the "_" character is just a fill character cause it wouldnt let me do more than one space, which is awesome.

    so that the decimal point of all of them lines up but the count doesnt have a decimal point. i was thinking i could count the number of digits in each variable and set the width relative to that but i dont know how.
    thanks for the help.

  2. #2
    Registered User boojus's Avatar
    Join Date
    Oct 2003
    Posts
    9
    i did it in case anyone wanted something similar

    Code:
        cout.setf(ios::fixed);
    	cout.precision(1);
    	cout << "\nTotal="<< setw(7+GetNumOfDigits(total)) << total << endl; 
    	cout.precision(0);
    	cout << "Count=" << setw(7+(GetNumOfDigits(total)-2))  << count << endl;
    	cout.precision(1);
    	cout << "Average=" << setw(5+GetNumOfDigits(total)) << total/count << endl;
    	cout<< GetNumOfDigits(total);
    	return 0;
    }
    
    int GetNumOfDigits(int n)
    {
       int d = 0;
       while(n > 0)
       {
          d++;
          n /= 10;
       }
       return d;
    }

  3. #3
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787

    Re: formatting output with cout

    Originally posted by boojus
    the "_" character is just a fill character cause it wouldnt let me do more than one space, which is awesome.
    put it in code tags... they preserve spaces....
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help for my output array
    By qwertysingh in forum C Programming
    Replies: 1
    Last Post: 02-17-2009, 02:08 PM
  2. Problem with formatting output
    By williamsb52 in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2003, 07:34 PM
  3. changing cout output
    By Yohumbus in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2002, 04:09 AM
  4. Output formatting
    By Beginner2002 in forum C Programming
    Replies: 3
    Last Post: 09-05-2002, 06:59 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