R
Richard
The program I write ask two questions. If user enter of the question invalid
or out of range, then the program will exit. My code cannot do that with a
break. Can anyone take a look and help.
#include <iostream>
#include <iomanip>
using namespace std;
void main()
{
double Weight, Distance, Charge;
// ask for information
cout<<"How many Kg is the package weighted? : ";
cin>>Weight;
if (Weight < 0 || Weight > 20)
{
cout<<"We do not ship package weight 0 or below, and more than 20
kg."<<endl;
break;
}
cout<<endl;
cout<<"How many miles is the packaged shipped? :";
cin>>Distance;
if (Distance < 10 || Distance > 3000)
{
cout<<"We do not ship package in a distance of less than 10 or more than
3000 miles";
break;
}
cout.setf(ios::fixed |ios::showpoint);
cout.precision(2);
if (Weight <= 2)
{
Charge = (Weight * 1.10)* (Distance/500);
cout<<"The total cost for this package to be shipped is:
"<<Charge<<endl<<endl;
}
if (Weight > 2 && Weight <= 6)
{
Charge = (Weight * 2.20)* (Distance/500);
cout<<"The total cost for this package to be shipped is:
"<<Charge<<endl<<endl;
}
if (Weight > 6 && Weight <= 10)
{
Charge = (Weight * 3.70)* (Distance/500);
cout<<"The total cost for this package to be shipped is:
"<<Charge<<endl<<endl;
}
if (Weight > 10 && Weight <=20)
{
Charge = (Weight * 4.80)* (Distance/500);
cout<<"The total cost for this package to be shipped is:
"<<Charge<<endl<<endl;
}
else
{
cout<<"You enter invalid data. Please try again"<<endl;
}
}
or out of range, then the program will exit. My code cannot do that with a
break. Can anyone take a look and help.
#include <iostream>
#include <iomanip>
using namespace std;
void main()
{
double Weight, Distance, Charge;
// ask for information
cout<<"How many Kg is the package weighted? : ";
cin>>Weight;
if (Weight < 0 || Weight > 20)
{
cout<<"We do not ship package weight 0 or below, and more than 20
kg."<<endl;
break;
}
cout<<endl;
cout<<"How many miles is the packaged shipped? :";
cin>>Distance;
if (Distance < 10 || Distance > 3000)
{
cout<<"We do not ship package in a distance of less than 10 or more than
3000 miles";
break;
}
cout.setf(ios::fixed |ios::showpoint);
cout.precision(2);
if (Weight <= 2)
{
Charge = (Weight * 1.10)* (Distance/500);
cout<<"The total cost for this package to be shipped is:
"<<Charge<<endl<<endl;
}
if (Weight > 2 && Weight <= 6)
{
Charge = (Weight * 2.20)* (Distance/500);
cout<<"The total cost for this package to be shipped is:
"<<Charge<<endl<<endl;
}
if (Weight > 6 && Weight <= 10)
{
Charge = (Weight * 3.70)* (Distance/500);
cout<<"The total cost for this package to be shipped is:
"<<Charge<<endl<<endl;
}
if (Weight > 10 && Weight <=20)
{
Charge = (Weight * 4.80)* (Distance/500);
cout<<"The total cost for this package to be shipped is:
"<<Charge<<endl<<endl;
}
else
{
cout<<"You enter invalid data. Please try again"<<endl;
}
}