A bookshop gives discount to customers as follows:
Students get 10% discount,
Book dealers get 12% discount and
Pensioners get 15% discount.
All other customers get 10% discount only if their total purchases are more than R200.
You are requested to write two versions of a program that calculates and displays the final amount that is due, after
discount.
(i) The first version of the program uses a switch statement to implement the above program.
(ii) The second version of the program uses nested-if statements
#include <iostream>
using namespace std;
int main()
{
// Part I
double sum;
char ch;
cout << "Enter type of discount (S, D, P, O): ";
cin >> ch;
cout << "Enter sum of purchases: ";
cin >> sum;
switch (ch)
{
case 'S':
{
cout << "Sum with discount is: R" << sum * 0.9 << endl;
break;
}
case 'D':
{
cout << "Sum with discount is: R" << sum * 0.88 << endl;
break;
}
case 'P':
{
cout << "Sum with discount is: R" << sum * 0.85 << endl;
break;
}
case 'O':
{
if (sum > 200) cout << "Sum with discount is: R" << sum * 0.9 << endl;
else cout << "Sum with discount is: R" << sum << endl;
break;
}
default:
cout << "Wrong value!" << endl;
break;
}
cout << endl;
cout << "Enter type of discount (S, D, P, O): ";
cin >> ch;
cout << "Enter sum of purchases: ";
cin >> sum;
if (ch == 'S') cout << "Sum with discount is: R" << sum * 0.9 << endl;
else if (ch == 'D') cout << "Sum with discount is: R" << sum * 0.88 << endl;
else if (ch == 'P') cout << "Sum with discount is: R" << sum * 0.85 << endl;
else if (ch == 'O')
{
if (sum > 200) cout << "Sum with discount is: R" << sum * 0.9 << endl;
else cout << "Sum with discount is: R" << sum << endl;
}
else cout << "Wrong value!" << endl;
return 0;
}
Comments
Leave a comment