Question
16. In this exercise, you explore the use of integers in monetary calculations. a. Follow the instructions for starting C++ and opening the Advanced16.cpp file.
16. In this exercise, you explore the use of integers in monetary calculations. a. Follow the instructions for starting C++ and opening the Advanced16.cpp file. Run the program. Enter 256.7 and 223.3 as the sales for Store 1 and Store 2, respectively. The total that appears on the computer screen (504.00) is incorrect because it is not the result of adding together the numbers 269.54 and 234.47. Press any key to stop the program. b. Review the code contained in the Advanced16.cpp file. The #include directive tells the C++ compiler to include the contents of the iomanip file in the current program. The file contains the definition of the setprecision stream manipulator,whichappearsinthecout << fixed << setprecision(2) << endl; statement. The fixed stream manipulator, which is defined in the iostream file, forces a real number to display a specific number of decimal places, as specified by the setprecision stream manipulator. In this program, the output values will display with two decimal places. You will learn about the directive and both stream manipulators in Chapter 5. c. Why does the total appear as 504.00 rather than 504.01? Hint: Change the cout << fixed << setprecision(2);statementtoacomment,andthensaveandrun the program. Enter 256.7 and 223.3 as the sales for Store 1 and Store 2, respectively. Study the output, and then stop the program. Change the comment back to a statement. d. Use the seven comments that appear below the main function to modify the programs code. Why do you need to add .5 to the expressions that calculate the increased sales for both stores?
//Advanced16.cpp //Created/revised by on #include #include using namespace std; int main() { //declare constant and variables const double increase = 1.05; double store1 = 0.0; double store2 = 0.0; double total = 0.0; cout << "Store 1 sales: "; cin >> store1; cout << "Store 2 sales: "; cin >> store2; store1 = store1 * increase; store2 = store2 * increase; total = store1 + store2; cout << fixed << setprecision(2) << endl; cout << store1 << " --->Store 1" << endl; cout << store2 << " --->Store 2" << endl; cout << "-----------------" << endl; cout << total << " --->Total" << endl; return 0; } //end of main function //1. Declare and initalize three int variables named store1Int, store2Int, and totalInt //2. Change the first assignment statement to store1Int = static_cast(store1 * increase * 100 + .5); //3. Change the second assignment statement to store2Int = static_cast(store2 * increase * 100 + .5); //4. Change the third assignment statement to totalInt = store1Int + store2Int; //5. Change store1 in the second cout statement to store1Int / 100.0 //6. Change store2 in the third cout statement to store2Int / 100.0 //7. Change total in the last cout statement to totalInt / 100.0
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started