Chapter+2_Ex+2.3+Setf+Display

2.3 --Setf Display You can use setf to control many aspects of formatting including spacing, numbers display and justification.

Assignment: Write a program that asks the user to enter the candidate names and election resultsand then displays a summary table similar to the following. Please note the precisionplacement and the alignment issues with the names and the numbers.

Example: Candidates Votes Percent Awbrey 616618 49.71 Berlin 623926 50.29

Total Votes: 1240544

include component="comments" page="Chapter 2_Ex 2.3 Setf Display" limit="10"

Code:

code format="cpp" using namespace std;
 * 1) include
 * 1) include

int main(int argc, char *argv[]) {   string candidate1,candidate2; int vote1,vote2; cout << "Enter your candidates' names" << endl; getline (cin,candidate1); getline (cin,candidate2);

cout.setf(ios_base::left, ios_base::adjustfield); cout.width(50);

cout << "Enter your votes:" << endl; cin >> vote1; cin >> vote2;

cout << "Left Justification:" << endl; cout.width(20); cout << "Candidates"; cout.width(15); cout << "Votes"; cout.width(15); cout << "Percent" << endl; cin.ignore(100,'\n');

cout.width(20); cout << candidate1; cout.width(15); cout << vote1; cout.width(15); cout.setf(ios::fixed); cout.precision(2); cout << ((float)vote1/((float)vote1+(float)vote2))*100 << endl;

cout.width(20); cout << candidate2; cout.width(15); cout << vote2; cout.width(15); cout << ((float)vote2/((float)vote1+(float)vote2))*100;

cin.get; return (0); }

code