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
Implement the following programs:

a)  Using if..else statement, write a program that accepts the quantities of steel bar purchased and displays the amount to be paid by the customer.            [10 Mks]

b) Using switch statement, write a program that accepts the quantities of steel bar purchased and displays the amount to be paid by the customer. [10 Mks]

There are no questions, just a request for people to do your homework for you.

I suggest you post your own attempts at completing your own work before asking for help with it.

Hello Suzie999,here's what i have

include<iostream>

using namespace std;
int main ()

int length,flat=1000;
float disc;
cout<<"Enter the length of steel bars bought :\n ";
cin>>length;
if(length<=20)
cout<<"NO DISCOUNT!!!";
if (length>=21&&length<=40)

    {

disc=0.05*flat;
cout<<"disount ="<<disc;

}

else
if (length>=41&&length<=60)

    {

disc=0.1*flat;
cout<<"disount ="<<disc;

}

else
if (length>=61&&length<=80)

    {

disc=0.15*flat;
cout<<"disount ="<<disc;

}

else
if (length>=81&&length<=100)

    {

disc=0.20*flat;
cout<<"disount ="<<disc;

}

else
if (length>100)

    {

disc=0.25*flat;
cout<<"disount ="<<disc;

}

return 0;

}

Suzie999 that's what i have,is that the way to go about it and what else do i do?please help.

Instead of sending your output to the screen as soon as you get the discount value, it's much easier to read if you save the discount and have only 1 cout statement. Also checking for the maximum value first simplifies the if-else structure. Something like this:

#include <iostream>

using namespace std;

int main ()
{
    int length,flat=1000;
    double disc = 0;
    cout<<"Enter the length of steel bars bought :\n ";
    cin>>length;
    if(length > 100)
        disc = .25;
    else
        if(length > 80)
            disc = .20;
        else
            if(length > 60)
                disc = .15;
            else
                if(length > 40)
                    disc = .10;
                else
                    if(length > 20)
                        disc = .05;
    cout<<"Disount = "<< (disc * 100) << "%\n";
    double total = flat * length;
    cout << "Net Total = " << total - (disc * total);
    return 0;
}

On a side note when you paste your code into the message, re-select all the code then hit the tab key. This will get it formatted better.

commented: Thanks Tinstaafl +0

Just a variation on the theme... (for your enlightment and enjoyment) ...

Note the simple looping exit condition ...

The loop is exited when a cin error flag is raised by any invalid (here, any NON non-numeric) input ...

#include <iostream>
#include <iomanip> // re. formatting output ...
#include <string>

using namespace std;


int main()
{
    // set output to 2 decimal places ...
    cout << fixed << setprecision(2) ;

    const string prompt = "Enter the length of steel "
                          "bars bought (q to quit) : ";
    const int flat = 1000;
    const int min_len = 2;
    int length;

    // begin entry loop ... until 'q' ...
    // i.e. loop until any cin error here ... is reached
    while( cout << prompt << flush &&
           cin >> length && cin.get()  == '\n' )
    {
        double discFraction = 0;

        if( length >= 100 ) discFraction = .25;
        else if( length >= 80 ) discFraction = .20;
        else if( length >= 60 ) discFraction = .15;
        else if( length >= 40 ) discFraction = .10;
        else if( length >= 20 ) discFraction = .05;

        if( length >= min_len )
        {
            double total = flat * length;
            double disc = total*  discFraction;
            double discPercent = 100 * discFraction;

            cout << "\nTotal before discount :  $"
                 << setw(10) << total
                 << "\nDiscount              :  $"
                 << setw(10) << disc
                 << " (" << discPercent << "%)"
                 << "\nNet due               :  $"
                 << setw(10) << total - disc << endl;
        }
        else
            cout << "\nMinimun stock size is " << min_len;

        cout << "\n\nNext order ... \n\n";

    }

    cout << "\nQuitting ... " << flush;

    return 0;
}
commented: Thanks David +0
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.