0

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]

4
Contributors
5
Replies
40
Views
3 Years
Discussion Span
Last Post by David W
0

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.

0

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;

}
0

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

1

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.

Edited by tinstaafl

Votes + Comments
Thanks Tinstaafl
1

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

Edited by David W: added comment

Votes + Comments
Thanks David
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.