In: Computer Science
Instructions
Write a program to implement the algorithm that you designed in Exercise 19 of Chapter 1.
Your program should allow the user to buy as many items as the user desires.
Instructions for Exercise 19 of Chapter 1 have been posted below for your convenience.
TEXT ONLY PLEASE (PLEASE NO PDF OR WRITING)
C++ CODE
Exercise 19
Jason typically uses the Internet to buy various items. If the total cost of the items ordered, at one time, is $200 or more, then the shipping and handling is free; otherwise, the shipping and handling is $10 per item. Design an algorithm that prompts Jason to enter the number of items ordered and the price of each item. The algorithm then outputs the total shipping and handling fee, and the billing amount. Your algorithm must use a loop (repetition structure) to get the price of each item. (For simplicity, you may assume that Jason orders no more than five items at a time.)
An example of the program is shown below:
Enter the number of items ordered: 3 Enter the price of item no. 1: 79.00 Enter the price of item no. 2: 23.50 Enter the price of item no. 3: 1.99 The shipping and handling fee is: $30.00 The billing amount is: $134.49
Since your program handles currency, make sure to use a data type that can store decimals with a decimal precision of 2.
#include <iostream> #include <iomanip> using namespace std; int main() { int n; double shipping = 0, price, total = 0; cout << "Enter the number of items ordered: "; cin >> n; for (int i = 0; i < n; ++i) { cout << "Enter the price of item no. " << i+1 << ": "; cin >> price; total += price; } if (total < 200) { shipping = 10*n; } cout << setprecision(2) << fixed; cout << "The shipping and handling fee is: $" << shipping << endl; cout << "The billing amount is: $" << total+shipping << endl; return 0; }