Question

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 less $1.10

Over 2 kg but not more than 6 kg $2.20

Over 6 kg but not more than 10 kg $3.70

Over 10 kg but not more than 20 kg $4.80

Write a program that asks for the weight of the package and the distance it is to be

shipped, and then displays the charges.

Input Validation: Do not accept values of 0 or less for the weight of the package. Do

not accept weights of more than 20 kg (this is the maximum weight the company will

ship). Do not accept distances of less than 10 miles or more than 3,000 miles. These

are the company’s minimum and maximum shipping distances.

*/

#include <iostream>

#include <iomanip>

using namespace std;

int main()

{

//CONSTANTS

const double RATE_0_2 = 1.10, RATE_2_6 = 2.20, RATE_6_10 = 3.70, RATE_10_20 = 4.80;

const double MIN_WEIGHT = 0, MAX_WEIGHT = 20, MIN_DIST = 10, MAX_DIST = 3000;

//Variables

double weight, distance, costPer500, totalCost;

// I/O

cout << "Enter the weight of the package in numbers only (Ex:12.75): ";

cin >> weight;

cout << "Enter the distance over which the package needs to be shipped: ";

cin >> distance;

//Formatting all numeric output to display numeric data to 2 decimal places

cout << setprecision(2) << fixed << endl;

//Running conditionals as per weight restrictions

if (weight < 0)

{

cout << "Invalid input. Weight cannot be negative (Ex: 25)" << endl;

cout << "The program will now halt. Press any key to terminate the program." << endl;

system("pause");

//If you call either of these 2 functions below, the program terminates instantly. The window flashes and disappears.

exit(EXIT_SUCCESS); //--> If argument to exit() is 0 or EXIT_SUCCESS, it indicates success. Terminates the process normally, performing the

//regular cleanup for terminating programs, and a successful termination status is returned to the host environment.

//exit(EXIT_FAILURE); --> If argument to exit() is EXIT_FAILURE, it indicates failure. Terminates the process normally, performing the

//regular cleanup for terminating programs, and an unsuccessful termination status is returned to the host environment.

}

else

if (weight >= 0 && weight <= 2)

costPer500 = weight * RATE_0_2;

else if (weight > 2 && weight <= 6)

costPer500 = weight * RATE_2_6;

else if (weight > 6 && weight <= 10)

costPer500 = weight * RATE_6_10;

else if (weight > 10 && weight <= 20)

costPer500 = weight * RATE_10_20;

else

{

cout << "Sorry, the maximum weight we accept is 20 kg. The program will now halt." << endl;

system("pause");

exit(EXIT_SUCCESS);

}

//This is just for testing the value stored in the variable costPer500 at this stage

//cout << "The cost of shipping per 500 miles is $" << costPer500 << endl;

/*

//Running conditionals as per distance restrictions

if (distance < 10 || distance > 3000)

{

cout << "Sorry, the minimum distance we accept is 10 miles and the maximum distance is 3000 miles." << endl <<

"The program will now halt. Press any key to terminate the program." << endl;

system("pause");

exit(EXIT_SUCCESS);

}

else if (distance >= 10 && distance <= 500)

{

totalCost = costPer500 * 1;

cout << "The total cost for shipping the package is $" << totalCost << endl;

}

else if (distance > 500 && distance <= 1000)

{

totalCost = costPer500 * 2;

cout << "The total cost for shipping the package is $" << totalCost << endl;

}

else if (distance > 1000 && distance <= 1500)

{

totalCost = costPer500 * 3;

cout << "The total cost for shipping the package is $" << totalCost << endl;

}

else if (distance > 1500 && distance <= 2000)

{

totalCost = costPer500 * 4;

cout << "The total cost for shipping the package is $" << totalCost << endl;

}

else if (distance > 2000 && distance <= 2500)

{

totalCost = costPer500 * 5;

cout << "The total cost for shipping the package is $" << totalCost << endl;

}

else if (distance > 2500 && distance <= 3000)

{

totalCost = costPer500 * 6;

cout << "The total cost for shipping the package is $" << totalCost << endl;

}

//Resetting cout to its default values

cout << defaultfloat;

cout << setprecision(0);

*/

//I could have also accomplished the distance restrictions using a for loop

if (distance < 10 || distance > 3000)

{

cout << "Sorry, the minimum distance we accept is 10 miles and the maximum distance is 3000 miles." << endl <<

"The program will now halt. Press any key to terminate the program." << endl;

system("pause");

exit(EXIT_SUCCESS);

}

else

{

int i, count = 1;

for (i = 0; i <= 3000; i = i + 500);

if (distance >= 10 && distance <= i)

{

totalCost = costPer500 * count;

cout << "The value of i is: " << i << endl;

cout << "The value of count is: " << count << endl;

cout << "The total cost for shipping the package is $" << totalCost << endl;

}

count = count + 1;

}

system("pause");

return 0;

}

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

The correct code for the FOR loop:

int i, count = 0; for (1-0; i < distance; i i+ 500){ count = count + 1; if (distance- 10 && distance < i) ( totalCost costPer500 count; KKn 1 cost for shipping the package is $ <s totalCost << endl;

Explanation:

You need to run the for loop till (i <= distance) rather than( i<=3000) because you need to count how many 500 miles are there in the distance. Also for every iteration of the for loop, we will increase the count by 1 and the count variable will be initially initialized to zero.

Code (Text):

int i, count = 0;

for (i = 0; i <= distance; i = i + 500){

   count = count + 1;

}

if (distance >= 10 && distance <= i) {

totalCost = costPer500 * count;

cout << "The total cost for shipping the package is $" << totalCost << endl;

}

Sample Output:

Testing the for loop by printing cost using both if-else and for loop:

Add a comment
Know the answer?
Add Answer to:
My program works fine with if else conditions but when it is not working when I...
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
  • My if/else statement wont run the program that I am calling. The menu prints but once...

    My if/else statement wont run the program that I am calling. The menu prints but once I select a number the menu just reprints, the function called wont run. What can I do to fix this probelm? #include <iostream> #include "miltime.h" #include "time.h" #include "productionworker.h" #include "employee.h" #include "numdays.h" #include "circle.h" using namespace std; int main() { int select=1;     while (select>0) { cout << "Option 1:Circle Class\n"<< endl; cout << "Option 2:NumDay Class\n" << endl; cout <<"Option 3:Employee and Production...

  • 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;...

  • Hey, so i am trying to have my program read a text file using a structure...

    Hey, so i am trying to have my program read a text file using a structure but i also want to be able to modify the results(I kinda have this part but it could be better). I cant seem to get it to read the file(all the values come up as 0 and i'm not sure why because in my other program where it wrote to the txt file the values are on the txt file) i copied and pasted...

  • I did a program in computer science c++. It worked fine the first and second time...

    I did a program in computer science c++. It worked fine the first and second time when I ran the program, but suddenly it gave me an error. Then, after a few minutes, it started to run again and then the error showed up again. This is due tonight but I do not know what is wrong with it. PLEASE HELP ME! Also, the instruction for the assignment, the code, and the error that occurred in the program are below....

  • I'm just a beginner in programming,how to make this program more simple without using #include<iostream> and #include<redex> here is the question Terms to know: If-else statement,for.....

    I'm just a beginner in programming,how to make this program more simple without using #include<iostream> and #include<redex> here is the question Terms to know: If-else statement,for..while..do while,loops,pointer,address,continue,return,break. Create a C++ program which contain a function to ask user to enter user ID and password. The passwordmust contain at least 6 alphanumeric characters, 1 of the symbols !.@,#,$,%,^,&,* and 1 capital letter.The maximum allowable password is 20. Save the information. Test the program by entering the User ID and password. The...

  • Program is in C++, program is called airplane reservation. It is suppose to display a screen...

    Program is in C++, program is called airplane reservation. It is suppose to display a screen of seating chart in the format 1 A B C D E F through 10. I had a hard time giving the seats a letter value. It displays a correct screen but when I reserve a new seat the string seats[][] doesn't update to having a X for that seat. Also there is a file for the struct called systemUser.txt it has 4 users...

  • Hello, I am working on my final and I am stuck right near the end of...

    Hello, I am working on my final and I am stuck right near the end of the the program. I am making a Tic-Tac-Toe game and I can't seem to figure out how to make the program ask if you would like to play again, and keep the same names for the players that just played, keep track of the player that wins, and display the number of wins said player has, after accepting to keep playing. I am wanting...

  • Hi there. I tried to submit this before, but the copy/paste didn't copy my vectors correctly....

    Hi there. I tried to submit this before, but the copy/paste didn't copy my vectors correctly. Hello there. I am trying to implement the djikstras shortest path algorithm into my code in C++ with a vector of vectors. However, when I test I get an error "Exception thrown at 0x75FFC762 in graphMain.exe: Microsoft C++ exception: std::out_of_range at memory location 0x009CF26C" for line 115 in graph.cpp. Could you help me debug? I am so close! ----------------------------FILE NAME: pch.h--------------------------------------- // pch.cpp: source...

  • i need this written in C, not C++ or anything else, just C, thank you. apparently...

    i need this written in C, not C++ or anything else, just C, thank you. apparently this works, but I can't use this, hope this helps though: #include<iostream> #include<string.h> using namespace std; int main() { char finalist1[10], finalist2[10], winner[10]; char decision[3][10]; int count1=0,count2=0,i; cout<<"Enter name of the first finalist: "; gets(finalist1); cout<<"\nEnter name of the second finalist: "; gets(finalist2); cout<<"\n\nEnter decision of the first committee member: "; gets(decision[0]); if(strcmp(decision[0],finalist1)==0 || strcmp(decision[0],finalist2)==0) { cout<<"\nEnter decision of the second committee member: ";...

  • EXPLAIN HOW THIS PROGRAM WORKS, USING DEV C++ #include <iostream> #include <cmath> #define PI 3.1415926535897 using...

    EXPLAIN HOW THIS PROGRAM WORKS, USING DEV C++ #include <iostream> #include <cmath> #define PI 3.1415926535897 using namespace std; typedef struct ComplexRectStruct {    double real;    double imaginary; } ComplexRect; typedef struct ComplexPolarStruct {    double magnitude;    double angle; } ComplexPolar; double radToDegree (double rad) {    return (180.00 * rad) / PI; } double degToRadian (double deg) {    return (deg * PI) / 180; } ComplexPolar toPolar (ComplexRect r) {    ComplexPolar p;    p.magnitude = round(sqrt(pow(r.real,...

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