C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old February 28th, 2006, 03:50 PM
mini-wheat mini-wheat is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Feb 2006
Posts: 2 mini-wheat User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 21 m 17 sec
Reputation Power: 0
Set field

Hey, first post :P

ok so what im trying to do is display this:

candidate votes percent
grassman 1276 57.27
Lee 952 42.73
Total: 2228

(ya these forums dont let me make graphs so, just imagine all the numbers under the names. and they are right aligned along with "Total" and everything else is left aligned)

okay, so candidate, grassman and lee are aligned left, and everything else is aligned right...

ok so heres the code...

/*Case study typing in zero for a percentage
Feb 27 Lindsay L*/

#include <iostream.h>
#include <lvp\string.h>

int main()

{
//first part
String firstname, secondname;

cout<<"--Vote Analysis Program--\n\n";
cout<<"Enter name of first candidate: ";
cin>>firstname;
cout<<"Enter name of second candidate: ";
cin>>secondname;

long firstv, secondv;
cout<<"Enter votes for "<<firstname<<": ";
cin>>firstv;
cout<<"Enter votes for "<<secondname<<": ";
cin>>secondv;
cout<<endl<<endl;

//second part
long total = firstv + secondv;
double percent1 = 100*double(firstv)/total;
double percent2 = 100*double(secondv)/total;

const int ColWidth = 10;
cout.setf(ios::fixed);
cout.precision(2);

//first line
cout.setf(ios::right);

cout.width(ColWidth); cout<<"Candidate";
cout.width(ColWidth); cout<<"Votes";
cout.width(ColWidth); cout<<"Percent"<<endl;

//second line
cout.width(ColWidth); cout<<firstname;
cout.width(ColWidth); cout<<firstv;
cout.width(ColWidth); cout<<percent1<<endl;

//third line
cout.width(ColWidth); cout<<secondname;
cout.width(ColWidth); cout<<secondv;
cout.width(ColWidth); cout<<percent2<<endl;

//fourth line
cout.width(ColWidth); cout<<"TOTAL: ";
cout.width(ColWidth); cout<<total<<endl<<endl;

return(0);
}




okay so when i enter cout.setf(ios::left);
before the lines i want left aligned and then change it back to cout.setf(ios::right);

everything seems to be turned into left aligned and looks liek this

candidate votes percent
grassman 1276 57.27
Lee 952 42.73
Total: 2228

Reply With Quote
  #2  
Old March 1st, 2006, 10:07 PM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 276 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 48 m 58 sec
Reputation Power: 4
Visit this link. A similar code to what you have been trying is here.See if you are missing anything.

http://library.thinkquest.org/C0111571/viewcode.php?cid=47

Reply With Quote
  #3  
Old March 1st, 2006, 10:09 PM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 276 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 48 m 58 sec
Reputation Power: 4
Else if you are still facing problem use setw() defined in iomanip.h.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Set field


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT