//-------------------------------------------------------------------- // // Example showing output stream formating format.cpp // //-------------------------------------------------------------------- #include // For cin and cout #include // For the I/O stream manipulators void main() { int modelNum; // Four-digit model number double basePrice, // Base price of car taxRate, // Tax rate finalPrice; // Total cost the car // Prompt the user for the car purchase data. cout << endl << "Enter the car's 4-digit model number: "; cin >> modelNum; cout << "Enter the car's base price: "; cin >> basePrice; cout << "Enter the tax rate (as a % of 1.0): "; cin >> taxRate; // Calculate the total cost of the purchase. finalPrice = basePrice * (1 + taxRate); // Output the model number and the purchase price with headings. cout << endl << "Model # Final price" << endl; cout << modelNum << " " << finalPrice << endl; // Use the setw() manipulator to set the width of the model // number field to 7 characters and the width of the final // price field to 11 characters. cout << endl << "Model # Final price" << endl; cout << setw(7) << modelNum << " " << setw(11) << finalPrice << endl; // Use the setprecision() manipulator to specify that the // final price should be rounded to two decimal places // (when necessary). cout << endl << "Model # Final price" << endl; cout << setprecision(2) << setw(7) << modelNum << " " << setw(11) << finalPrice << endl; // Set the ios::fixed flag to specify that the final price // should be displayed using fixed-point format (as opposed // to scientific notation) and set the ios::showpoint flag // to specify that trailing zeroes should be displayed to // the right of the decimal point. cout << endl << "Model # Final price" << endl; cout << setprecision(2) << setiosflags( ios::fixed | ios::showpoint ) << setw(7) << modelNum << " " << setw(11) << finalPrice << endl; // Various ios:: flags // ios::right Right-align output values [DEFAULT] // ios::left Left-align output values // ios::dec Output numbers in decimal form [DEFAULT] // ios::oct Output numbers in octal form // ios::hex Output numbers in hexadecimal form // ios::scientific Output floating-point numbers in scientific format // ios::fixed Output floating-point numbers in fixed format // ios::showpos Output plus signs (+) for positive numbers. // ios::showpoint Output the decimal point and trailing zeros for // floating-point numbers }