You are here:

C++/arithmetics

Advertisement


Question
QUESTION: Using switch statement,how will i write a program that accepts the quantities of steel bar purchased and displays the amount to be paid by the customer.
Using if..else statement,how will i write a program that accepts the quantities of steel bar purchased and displays the amount to be paid by the customer in the following question????? please help me
A hardware merchant sells steel bars at a flat rate of Kes 1000.00 per meter, and has announced the following discounts:
Quantity of steel bar bought (per meter)          Discount (%)

<20          0
21-40          5
41-60          10
61-80          15
81-100          20
>100          25

ANSWER: Hello there,

I assume that the bar means when when the steal bar purchased is less than 20 i.e <20, the customer pays 0 kenya shillings right? and when it is between 21 and 40, the customer pays 5 . If that is what it implies then fine. Here is how to use the switch cases.


===================using switch statement=====================================================
You have to assume figures when you use switch cases..i.e you assume that when the user enters say 20, it will display 0. so, here is what you can do for switch cases..

cout<<"Enter 1 if you bought less than 20 steel bars"<<endl;
cout<<"Enter 2 if you bought more  than 21steel bars but less than 40 steel bars"<<endl;
......... there are about 6 options so, you need 4 more cout statements..

then in your switch statement you go this way :

switch case(option)
{
 case 1: cout<<"you have to pay 0 kenya shillings!"<<endl; //here you just display the amount to be paid
         break;
case 2 : cout<<"You have to pay 5 kenyan shillings!!"<<endl;
         break;
.........complete the other 4 as well, and adding a break statement at the end of each of them..
}


========using If-else statement========================

the same thing as before but this time you just go this way :

if(steel bars <20)
cout<<"you have to pay 0 kenya shillings!"<<endl;
else if(steel bar>21&& steal bar <40)
cout<<"you have to pay 5 kenya shillings!"<<endl;
else if(steel bar >41 && steel bar < 60)
cout<<"you have to pay 10 kenya shillings!"<<endl;
/* you have to continue until the last statement which now goes like this */

else
cout<<"you have to pay 25 kenya shillings!"<<endl;

so, in the if -else statements, you got 6 options so, the structure is as this :

1 if() statement
4  else if()  statements
1 else statement

all together 6 statements..

Hope this helps. I will also suggest you read some books online on switch and if-else statements.

Good luck

---------- FOLLOW-UP ----------

QUESTION: thanks for your help but am kinda confused a little bit.would you write for me the whole code in C++ language?
thnx

Answer
Hello,

Follow the instructions I have given you with the help of a good programming text book and you can as well follow. If after putting in effort and you still can't solve it, I will then solve it but try first using those structures I gave you.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


henry

Expertise

I can confidently answer any question both basic and advanced in C++. Areas like templates, algorithms, Gui and design patterns are of interest to me. i will not answer questions about web programming.

Experience

I have a 3- year- work experience in software Engineering.

Education/Credentials
I am a bachelor of Engineering graduate

©2016 About.com. All rights reserved.