I have to make a program for Uni to calculate the shipping cost
starts with a £50 fee..
Each of the first 100 miles (included) cost £5.50.
Over 100 miles and up to 500 miles (included): £4.00 per mile.
Over 500 miles: £2.50 per mile.
//include libraries
#include <iostream> //includes the input output stream
usingnamespace std; //includes the standard namespace
double distanceInMile; //global declaration
double overAllCost;
void error() //Errorr handler for program
{
if (distanceInMile <=0) //if statement for the invalide value
{
cout<<"\nERROR: the distance should be a positive Integer Value\n";
terminate; //terminates the program
}
}
void CalculateVariableShippingCost()
{
float fixedCost,uptoHundred, uptoFiveHundred, overFiveHundred;
double overAllCost;
/*fixedCost=50;
uptoHundred= 5.50;
uptoFiveHundred =4.00;
overFiveHundred =2.50; */
overAllCost = (50+(100*5.50)+(400*4.00));
if (distanceInMile >= 500)
(overAllCost+(50.5*2.5));
}
int main()
{
cout<<"\t\t\t Luke Waugh Group 1V \n\n \t\t\tShipping Costs Program\n";
cout<<"\n\nPlease Enter the distance in miles..";
cin>>distanceInMile;
void error();
error();
void CalculateVariableShippingCost();
CalculateVariableShippingCost;
cout<<"\nThe cost of shipment over " <<distanceInMile<< " is £"<<overAllCost;
cout<<"\n\n\n\n\n";
system ("pause");
}
So if I enter the distance 50 the answer is ú0..
Any help?
Lines 39, 41, and 42: These aren't quite proper ways of calling functions. Line 40, however, is correct, but CalculateVariableShippingCost still never gets called.
Line 44: You have a £ character in the output, which unfortunately isn't one of the 128 ASCII characters, which is the reason for the garbage output. If you want to output it, you'll need to look into wide character stuff, which can be a pain (though I will explain how if you'd like to know).
You've got some other problems which Albatross didn't mention.
1.)you've got double overAllCost; in global scope on line 8. You then declare a local variable with the same type and identifier on line 21 in your void CalculateVariableShippingCost() function. This results in the more localized variable being modified, and the global variable remains untouched.
1.5.)Line 14 is scary...
2.)Line 30 does nothing. You're not assigning anything to anything.
3.)Your main function doesn't return anything. I'm assuming your compiler magic'd that in for you, but it's bad practice.
4.)As Albatross said, line 39 does not call error(). You then actually call error() one line underneath. It looks as if you think you need to provide function prototypes in the local scope of main (which you don't).
5.)Line 42 - You're not actually calling CalculateVariableShippingCost().
Here is rev.2: I got rid of terminate and changed to exit changed the calls for the main and sorted out the double overAllCost; problem! Thank you for your help!
//include libraries
#include <iostream> //includes the input output stream
usingnamespace std; //includes the standard namespace
double distanceInMile; //global declaration
double overAllCost;
void error() //Error handler for program
{
if (distanceInMile <=0) //if statement for the invalide value
{
cout<<"\nERROR: the distance should be a positive Integer Value\n";
exit; //terminates the program
}
}
void CalculateVariableShippingCost()
{
float fixedCost,uptoHundred, uptoFiveHundred, overFiveHundred;
fixedCost=50;
uptoHundred= 5.50;
uptoFiveHundred =4.00;
overFiveHundred =2.50;
overAllCost = distanceInMile+50+(100*uptoHundred)+(400*uptoFiveHundred);
//if statement for when the miles entered are equal to or over 500 miles
if (distanceInMile >= 500)
overAllCost=(overAllCost+(50.5*2.5));
}
int main() //main body of the program
{
cout<<"\t\t\t Luke Waugh Group 1V \n\n \t\t\tShipping Costs Program\n";
cout<<"\n\nPlease Enter the distance in miles..";
cin>>distanceInMile; //takes input from the user
error();//void error();
CalculateVariableShippingCost();//void CalculateVariableShippingCost();
//displays the amount he user input and the cost
cout<<"\nThe cost of shipment over " <<distanceInMile<< " is "<<overAllCost;
cout<<"\n\n\n\n\n";
system ("pause"); //holds the screen
}