(Calculating Total Sales) A mail order house sells five different products whose retail prices
are: product 1 — $2.98, product 2—$4.50, product 3—$9.98, product 4—$4.49 and product 5— $6.87.
are: product 1 — $2.98, product 2—$4.50, product 3—$9.98, product 4—$4.49 and product 5— $6.87.
Write a program that reads a series of pairs of numbers as follows:
a) product number
b) quantity soldYour program should use a switch statement to determine the retail price for each product. Your program should calculate and display the total retail value of all products sold.
Use a sentinel-controlled loop to determine when the program should stop looping and display the final results.
Solution:
#include < iostream> using namespace std; int main() { int number, quantity = 0; int quantity1 = 0, quantity2 = 0, quantity3 = 0, quantity4 = 0, quantity5 = 0; double amount1 = .0, amount2 = .0, amount3 = .0, amount4 = .0, amount5 = .0; for( ;number != -1;) { cout << "Enter product number and quantity sold (enter -1 to end):"; cin >> number; cin >> quantity; switch( number ) { case 1: quantity1 += quantity; amount1 += quantity * 2.98; break; case 2: quantity2 += quantity; amount2 += quantity * 4.50; break; case 3: quantity3 += quantity; amount3 += quantity * 9.98; break; case 4: quantity4 += quantity; amount4 += quantity * 4.49; break; case 5: quantity5 += quantity; amount1 += quantity * 6.87; break; default: cout << "Enter correct product number!" << endl; break; } }
cout << "Sold product number: " << quantity1 + quantity2 + quantity3 + quantity4 + quantity5 << endl; cout << "Sold product value: " << amount1 + amount2 + amount3 + amount4 + amount5 << endl; //for pause system("PAUSE"); return 0; }
Post A Comment:
0 comments: