| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
Else if you are still facing problem use setw() defined in iomanip.h.
|
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Set field |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|