Thread: Alignment of Output

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    13

    Alignment of Output

    Im outputting text to a file, using setw and setfill.

    Im trying to create this in a text file:
    Code:
    Gross Amount............. $1000.00
    Federal Tax.............. $ 150.00
    However, I cannot get it to align correctly, and often the hundreds place lines up with the thousands place in the money column. Here's the code I'm using:
    Code:
    outFile << "Gross Amount" << setfill('.') << right << setw(15) << right << " $" << grossAmount << right << endl;
    outFile << "Federal Tax" <<  setfill('.')  << right << setw(15) << right << " $" << federalTax << right << endl;
    You probably noticed theres like 4 different 'right' in each line - basically because im tossing them everywhere to try and get it to align the money to the right. Any ideas? Im new at this.

  2. #2
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    you need to put a little math in your setw's

    Code:
    #include <iostream>
    #include <iomanip>
    #include <string>
    
    int main()
    {
    	float grossAmount=1000.00f;
    	float federalTax=150.00f;
    	std::string GA="Gross Amount";
    	std::string FT="Federal Tax";
    	
    	std::cout<<std::setprecision(2)<<std::fixed<<std::right
    		<<GA<<std::setfill('.')<<std::setw(30-GA.length())
    		<<std::right<<" $"<<std::setfill(' ')<<std::setw(7)
    		<<grossAmount<<'\n'
    		<<FT<<std::setfill('.')<<std::setw(30-FT.length())
    		<<std::right<<" $"<<std::setfill(' ')<<std::setw(7)
    		<<federalTax<<std::endl;
    	
    	return 0;
    }
    make sure you actually understand what I did there... if not let me know I'll show you what I did
    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

  3. #3
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    So really it has nothing to do with iostream. You should align it yourself.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by siavoshkc
    So really it has nothing to do with iostream. You should align it yourself.
    it has to do with iomanip... and you aren't aligning it yourself, you're using the context of the words around it to set the amount of spaces... aligning it yourself dynamically would take much more code...
    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

  5. #5
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    aligning it yourself dynamically would take much more code...
    Can you please show me how? Thanks.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You'd need something like this:
    Code:
    length = number_to_string()
    print number_to_string
    print length-maxlength spaces
    etc
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    oi... I'll take a crack at it... yuck...
    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

  8. #8
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    What about something like this:
    Code:
    outFile << left<< setfill('.')<< setw(15)<<"Gross Amount" << right << " $" << grossAmount << endl;
    outFile << left<< setfill('.')<< setw(15)<<"Federal Tax" << right << " $" << federalTax << endl;
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  9. #9
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    please don't try to learn anything from this code...
    Code:
    #include <iostream>
    #include <sstream>
    #include <string>
    
    namespace My
    {
    	std::string setw(const int num, const double dub);
    	std::string retval;
    }
    
    int main()
    {
    	std::cout<<"Gross Amount............ $"<<My::setw(7,1000.00)<<'\n'
    		<<"Federal Tax............. $"<<My::setw(7,150.10)<<std::endl;
    
    	return 0;
    }
    
    std::string My::setw(const int num, const double dub)
    {
    	My::retval="";
    
    	std::stringstream ss;
    	ss<<dub;
    	if(dub-static_cast<int>(dub)<0.01)
    	{
    		ss<<".00";
    	}
    	else if(dub-static_cast<int>(dub)<0.10)
    	{
    		ss<<"0";
    	}
    	My::retval=ss.str();
    
    	for(int i=0; i<static_cast<int>(num-My::retval.length()); i++)
    	{
    		My::retval.insert(0," ");
    	}
    
    	return My::retval;
    }
    Last edited by major_small; 08-31-2006 at 04:42 PM.
    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

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Especially not how to code a code formatter . . .
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by dwks
    Especially not how to code a code formatter . . .
    yeah, yeah. you build me a better one.

    edit: there, happy?
    Last edited by major_small; 08-31-2006 at 04:42 PM.
    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

  12. #12
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    It takes me some time to understand what you have written.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  13. #13
    System Novice siavoshkc's Avatar
    Join Date
    Jan 2006
    Location
    Tehran
    Posts
    1,246
    Thank you, I got it. I recently had a little problem with aligning that I solved it with your help.
    Learn C++ (C++ Books, C Books, FAQ, Forum Search)
    Code painter latest version on sourceforge DOWNLOAD NOW!
    Download FSB Data Integrity Tester.
    Siavosh K C

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Quote Originally Posted by major_small
    yeah, yeah. you build me a better one.

    edit: there, happy?
    Yes, because now I get to post the link for my (old) code formatter: http://dwks.theprogrammingsite.com/myprogs/codeform.htm

    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    well then... thank you

    I may end up using yours... mine has an HTML output part too, but it doesn't work >.<
    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. 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. execl()/fork() output
    By tadams in forum C Programming
    Replies: 19
    Last Post: 02-04-2009, 03:29 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