vendredi 19 février 2016

Ordering of if/else if statements [on hold]

I am struggling with the last part of my c++ assignment. I have two problems first, the output of the customer's full name is not displaying entirely. Secondly, near the bottom I am trying to determine rank and shipping cost using simple if else/if without logical operators. Please help me solve these two problems, thanks! Comments are included so you know what I'm doing as well as where I need help.

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;

/*This program gets the weight of a package and
the distance it is to be shipped. Then it computes
the shipping charges for that package.
*/
int main(){
    const int DISTANCE_UNIT = 500;
    double rate, shippingCost;
    int weight, distance, distanceUnits;
    string customerName;

    cout << setprecision(2) << fixed;

    cout << "How much does the package weigh (in Kilograms)? ";
    cin >> weight;

    if  (weight <= 0 || weight > 20)
    {
        cout << "The Fast Freight Shipping Company does not ship packages with a weight of: " << weight << " Kilograms." << endl;
        return 0;
    }

    else {

        cout << "How far will the package be going? ";
        cin >> distance;

        if (distance < 10 || distance > 3000)
        {
            cout << "The Fast Freight Shipping Company does not ship packages at a distance of: " << distance << " Miles." << endl;
            return 0;
        }
    }

    cout << "Please enter the customer's full name: ";
    cin >> customerName;
    getline(cin, customerName);
    cout << customerName;



    distanceUnits = distance / DISTANCE_UNIT;
    if (distance%DISTANCE_UNIT != 0)
        distanceUnits += 1;

    //deteremine rate using if/else if with 
    //complex conditions the use 
    //logical operators to identify ranges

    if (weight <= 2)
    {
        rate = 1.10;
    }
    else if (weight > 2 && weight <= 6 )
    {
        rate = 2.20;
    }
    else if (weight > 6 && weight <= 10)
    {
        rate = 3.70;
    }
    else if (weight > 10 && weight <=20)
    {
        rate = 4.80;
    }


    shippingCost = distanceUnits * rate;

    //HELP output full customer name here
    cout 
        << "\nUsing logical operators to identify ranges:\n"
        << "The cost to ship a package that weighs " << weight
        << " Kilograms\nfor a distance of " << distance
        << " miles is $" << shippingCost << endl << endl;

    //HELP deteremine rate using if/else if with simple conditions
    //HELP using order to correctly separate the options


    if (weight <= 2)
    {
        rate = 1.10;

    }





    shippingCost = distanceUnits * rate;

    //HELP output full customer name here
    cout 
        << customerName
        << "Using the order of the if/else if statement:\n"
        << "The cost to ship a package that weighs " << weight
        << " Kilograms\nfor a distance of " << distance
        << " miles is $" << shippingCost << endl << endl;

    return 0;

Aucun commentaire:

Enregistrer un commentaire