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.
Source code
(i)
#include <iostream>
using namespace std;
int main()
{
float total_purchase;
float price;
cout<<"\nEnter total purchase: ";
cin>>total_purchase;
int c;
cout<<"\nChoose type of customer:\n1. Student\n2. Book dealer\n3. Pensioner\n4. Other\n";
cin>>c;
switch(c){
case 1:
price=total_purchase*0.90;
break;
case 2:
price=total_purchase*0.88;
break;
case 3:
price=total_purchase*0.85;
break;
case 4:
if(total_purchase>200){
price=total_purchase*0.90;
}
else{
price=total_purchase;
}
break;
default:
cout<<"\nInvalid choice";
}
cout<<"\nPrice after discount = "<<price;
return 0;
}
(ii)
#include <iostream>
using namespace std;
int main()
{
float total_purchase;
float price;
cout<<"\nEnter total purchase: ";
cin>>total_purchase;
int c;
cout<<"\nChoose type of customer:\n1. Student\n2. Book dealer\n3. Pensioner\n4. Other\n";
cin>>c;
if (c==1)
price=total_purchase*0.90;
else if(c==2)
price=total_purchase*0.88;
else if(c==3)
price=total_purchase*0.85;
else if(c==4){
if(total_purchase>200){
price=total_purchase*0.90;
}
else{
price=total_purchase;
}
}
else{
cout<<"\nInvalid choice";
}
cout<<"\nPrice after discount = "<<price;
return 0;
}
Sample run 1
Sample run 2
Comments
Leave a comment