Please can you assist.

Mar 24, 2016 at 5:48am
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

#include <iostream>


using namespace std;

int main()
{


float amount,finalAmount,discount;
char customerType; //S=Student ,D=Dealer,P=Pensioner or O=other
const float students=0.10;
const float dealer=0.12;
const float pensioner=0.15;
const float other=0.10;

//Request price

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

#include <iostream>


using namespace std;

int main()
{


float amount,finalAmount,discount,s,d,p,o,S,D,P,O;
char customerType; //S=Student ,D=Dealer,P=Pensioner or O=other
const float students=0.10;
const float dealer=0.12;
const float pensioner=0.15;
const float other=0.10;

//Request price

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;}

if(customerType==o && amount>200)
discount=amount*other;
finalAmount=amount-discount;








cout<<"The Amount is R "<<finalAmount;

return 0;
}


The output repeats 4 times and doesn't work out the discount, also I'm not how how to allocate the discount for purchases over 200

Last edited on Mar 24, 2016 at 6:05am
Mar 24, 2016 at 10:51am
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;
}

do it with ur all the if condition
Mar 24, 2016 at 11:01am
Hi,

Please use code tags: http://www.cplusplus.com/articles/z13hAqkS/

You need to use if then a bunch of else if then else

It's also a good idea to have an else to catch bad input, like a switch has a default: case.

Prefer to use double rather than float.

Good Luck !!
Mar 24, 2016 at 12:43pm
Thank you so much guys, this helped me out big time. Much,much appreciated!
Topic archived. No new replies allowed.