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.
Hint:
Use the following variables: float amount; // the amount due before discount char customerType; // the type of customer: 'S' (student) or // 'D' (dealer) or 'P' (pensioner) or // 'O'(other) float discount, finalAmount;
Submit both programs and their output.
this is my output using the Switch, this works perfectly fine
cout<<"Please enter price: ";
cin>>amount;
cout<<"please enter customer type (S,D,P,O): ";
cin>>customerType;
//create switch with statements and calculations
switch(customerType)
{
case 's':
case 'S':
discount=amount*students;
finalAmount=amount-discount;
cout<<"The Amount is R "<<finalAmount;
break;
case 'D':
case 'd':
discount=amount * dealer;
finalAmount= amount - discount;
cout<<"The Amount is R "<<finalAmount;
break;
case 'p':
case 'P':
discount=amount*pensioner;
finalAmount=amount-discount;
cout<<"The Amount is R "<<finalAmount;
break;
case 'o':
case 'O':
if(amount>200)
discount=amount*other;
finalAmount=amount-discount;
if(amount<=200)
finalAmount=amount;
cout<<"The Amount is R "<<finalAmount;
}
return 0;
}
I am having an issue changing it to nested if form, this is what I have so far
cout<<"Please enter price: ";
cin>>amount;
cout<<"please enter customer type (S,D,P,O): ";
cin>>customerType;
//create if with statements and calculations
{
if(customerType==s)
discount=amount*students;
finalAmount=amount-discount;
cout<<"The amount is R "<<finalAmount;}
{
if(customerType==d)
discount=amount*dealer;
finalAmount=amount-discount;
cout<<"The amount is R "<<finalAmount;}
{
if(customerType==p)
discount=amount*pensioner;
finalAmount=amount-discount;
cout<<"The amount is R "<<finalAmount;}
Put the bracket properly after every if it should present and comparison also do with character like 's'
for example
if(customerType=='s' ||customerType=='S' )
{
discount=amount*students;
finalAmount=amount-discount;
cout<<"The amount is R "<<finalAmount;
}