Question

Intro to C++

For this program, again use Program 4-18 in the text as an example. Assume youre writing this menu-driven program for a ship

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Please find below the code, code screenshots and output screenshots. Please refer to the screenshot of the code to understand the indentation of the code.  Please get back to me if you need any change in code. Else please upvote

CODE:

#include <iostream>

#include <iomanip>

//declaring constants for type of shipments

#define NORMAL 1

#define EXPEDITED 2

using namespace std;

int main()

{

    int shipment_type = -1; //variable for shipment type

    int flag = 0; //Variable for user input validation and initialize it to 0

    double weight; //Variable for weight of the package

    int distance; //Variable for distance

    double cost; //Variable for cost

   

    cout << "Enter 1 for normal shipments\nEnter 2 for expedited shipments\n";

    cout << "\nEnter your type of shipment: ";

    cin >> shipment_type; //reading the shipment type

   

    //set flag as 1 if invalid shipemnt type entered

    if(shipment_type != 1 && shipment_type != 2)

        flag = 1;

  

    //Reading the weight of packege

    cout <<"Enter the weight of the package: ";

    cin >> weight;

   

    //Reading the distance

    cout <<"Enter the distance: ";

    cin >> distance;

   

    //set flag as 1 if for normal shipment the distance is less than 15 and greater than 900

    if(shipment_type == NORMAL && (distance < 15 || distance > 900))

        flag = 1;

   

     //set flag as 1 if for expedited shipment the distance is less than 10 and greater than 500

    if(shipment_type == EXPEDITED && (distance < 10 || distance > 500))

        flag = 1;

   

    //calculate cost for normal shipment with weight less than or equal to 1

    if(shipment_type == NORMAL && weight <= 1)

        cost = distance * (2.03/100);

   

    //calculate cost for normal shipment with weight greater than 1 and less than 4

    if(shipment_type == NORMAL && weight > 1 && weight < 4)

        cost = distance * (3.95/100);

   

     //calculate cost for normal shipment with weight greater than or equal to 4 and less than 10

    if(shipment_type == NORMAL && weight >= 4 && weight < 10)

        cost = distance * (6.57/100);

   

     //calculate cost for normal shipment with weight greater than or equal to 10

    if(shipment_type == NORMAL && weight >= 10)

        cost = distance * (8.26/100);

   

    //calculate cost for expedited shipment with weight less than or equal to 1

    if(shipment_type == EXPEDITED && weight <= 1)

        cost = distance * (3.10/100);

   

    //calculate cost for expedited shipment with weight greater than 1 and less than 4

    if(shipment_type == EXPEDITED && weight > 1 && weight < 4)

        cost = distance * (4.28/100);

   

    //calculate cost for expedited shipment with weight greater than or equal to 4 and less than 10

    if(shipment_type == EXPEDITED && weight >= 4 && weight < 10)

        cost = distance * (7.13/100);

   

    //calculate cost for expedited shipment with weight greater than or equal to 10

    if(shipment_type == EXPEDITED && weight >= 10)

        cost = distance * (9.75/100);

       

    if(flag == 1) //display error if flag is 1

        cout <<"\nThere was invalid input. Please try again.";

   

    //displaying the shipment_type

    if(flag == 0 && shipment_type == NORMAL)

        cout <<"\n"<<setw(15) << left << "Shipment type" <<": " << setprecision(3) << fixed << "Normal";

    if(flag == 0 && shipment_type == EXPEDITED)

        cout <<"\n"<<setw(15) << left << "Shipment type" <<": " << setprecision(3) << fixed << "Expedited";

   

    //displaying the Weight, Distance and cost

    if(flag == 0){

        cout <<"\n"<<setw(15) << left << "Weight" <<": " << setprecision(3) << fixed << weight;

        cout <<"\n"<<setw(15) << left << "Distance" <<": " << distance;

        cout <<"\n"<<setw(15) << left << "Cost" <<": $" << setprecision(2) << fixed << cost;

    }

  

    return 0;

}

1 #include <iostream> 2 #include <iomanip> 3 4 //declaring constants for type of shipments 5 #define NORMAL 1 6 #define EXPED

flag = //set flag as 1 if for expedited shipment the distance is less than 10 and greater than 500 if(shipment_type == EXPEDI

if(shipment_type == EXPEDITED && weight >= 10) cost = distance (9.75/100); 71 72 73 74 75 76 == if(flag 1) //display error if

OUTPUT:

Enter i for normal shipments Enter 2 for expedited shipments Enter your type of shipment: 1 Enter the weight of the package:

Enter i for normal shipments Enter 2 for expedited shipments Enter your type of shipment: 2 Enter the weight of the package:

Enter 1 for normal shipments Enter 2 for expedited shipments Enter your type of shipment: 1 Enter the weight of the package:

Add a comment
Know the answer?
Add Answer to:
Intro to C++ For this program, again use Program 4-18 in the text as an example....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • In this assignment, you will develop a C++ program which calculates a shipping charge and determines...

    In this assignment, you will develop a C++ program which calculates a shipping charge and determines the “type of trip” for a freight shipping company. Ask the user to enter the distance a package is to be shipped, and use a menu and a switch statement to determine the rate based on the package’s weight. Then display the shipping charge and using the table in #7 below, display the type of trip. Below is the chart to use to calculate...

  • In C using (printf and scanf) Write a program to ask the user for the weight...

    In C using (printf and scanf) Write a program to ask the user for the weight of a package, and display the shipping charges for the package. If the weight of the package is less than or equal to 0 pounds, inform the user and exit the program. The shipping charges are: Weight of Package Shipping Charge 2 pounds or less $1.25 Over 2 pounds but not more than 6 pounds $2.50 Over 6 pounds but not more than 10...

  • My program works fine with if else conditions but when it is not working when I...

    My program works fine with if else conditions but when it is not working when I try to implement it using a for loop. Can you fix my for loop and please show me how to do this using loops? I would appreciate if you could explain the loop that you are using. /* Programming Challenge: Shipping Charges The Fast Freight Shipping Company charges the following rates: Weight of Package (in Kilograms) Rate per 500 Miles Shipped 2 kg or...

  • write a program for C++ to use on Putty . C++ not Java The Fast Freight...

    write a program for C++ to use on Putty . C++ not Java The Fast Freight Company provides three kinds of shipping services for packages: regular, priority and overnight. They charge the following rates: Regular Priority Overnight <= 2 Kg $1.00 per Kg $3.50 per Kg $11.50 per Kg > 2 Kg but <=6 Kg $1.50 per Kg $5.50 per Kg $16.50 per Kg > 6 Kg but <=10 Kg $2.00 per Kg $7.50 per Kg $21.50 per Kg >...

  • C++ programming I need at least three test cases for the program and at least one...

    C++ programming I need at least three test cases for the program and at least one test has to pass #include <iostream> #include <string> #include <cmath> #include <iomanip> using namespace std; void temperatureCoverter(float cel){ float f = ((cel*9.0)/5.0)+32; cout <<cel<<"C is equivalent to "<<round(f)<<"F"<<endl; } void distanceConverter(float km){ float miles = km * 0.6; cout<<km<<" km is equivalent to "<<fixed<<setprecision(2)<<miles<<" miles"<<endl; } void weightConverter(float kg){ float pounds=kg*2.2; cout<<kg<<" kg is equivalent to "<<fixed<<setprecision(1)<<pounds<<" pounds"<<endl; } int main() { string country;...

  • Requirement Write pseudocode and translate it to ONE C-program for each the following problems. In your...

    Requirement Write pseudocode and translate it to ONE C-program for each the following problems. In your pseudocode and C-program, use only what you have learned in this class so far. (Menu) Design a menu for question 2 and 3. So far, you use one program to solve all lab questions. But some of you may feel awkward when you want to demo/test only one lab question. To overcome that, your program should show a menu so that the users of...

  • C PROGRAMMING Introduction In this part, you will solve a problem described in English. Although you...

    C PROGRAMMING Introduction In this part, you will solve a problem described in English. Although you may discuss ideas with your classmates, etc., everyone must write and submit their own version of the program. Do NOT use anyone else’s code, as this will result in a zero for you and the other person! Shipping Calculator Speedy Shipping Company will ship your package based on the weight and how far you are sending the package, which can be anywhere in the...

  • CSC 130 Lab Assignment 8 – Program Menu Create a C source code file named lab8.c...

    CSC 130 Lab Assignment 8 – Program Menu Create a C source code file named lab8.c that implements the following features. Implement this program in stages using stepwise refinement to ensure that it will compile and run as you go. This makes it much easier to debug and understand. This program presents the user a menu of operations that it can perform. The choices are listed and a prompt waits for the user to select a choice by entering a...

  • Write the program in C The following statistics for cities of the Vege country are stored...

    Write the program in C The following statistics for cities of the Vege country are stored in a file: Name: a string of characters less than 15 characters long. Population: an integer value. Area: square miles of type double Write a program that: asks and gets the name of the input file from the user. Read the contents of the file into an array length 10 of structures -ask the user for the name of a city (less than 15...

  • C++ Lone Star Package Service ships packages within the state of Texas. Packages are accepted for...

    C++ Lone Star Package Service ships packages within the state of Texas. Packages are accepted for shipping subject to the following restrictions: Shipping requirements The package weight must not exceed 50 pounds. The package must not exceed 3 feet in length, width, or height. The girth of the package must not exceed 5 feet. The girth is the circumference around the two smallest sides of the package. If side1, side2, and side3 are the lengths of the three sides, and...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT