-3

How do I pass the user entered distance gathered from one function to another function to calculate a shipping cost? And then show the cost via the int main function?

My distance function

double getDistance(double distance)
{
cout << "Enter the distance to be shipped: (min 10 Mi, max 3000 Mi): ";
cin >> distance;
while(distance<=10 || distance>= 3000){
    cout << "We do not ship less than 10 miles or more than 3000 miles, 
please reenter: ";
    cin >> distance;
}
return distance;
}

I was thinking I could return distance and then use it in another function?? Is that right?

I need the variable distance to calculate the final shipping cost in another function ie calcCost then in the main function int main() simply display it cout << "the cost is " << finalCost << endl;

How do I pass the variable distance to another function to calculate cost and then display the cost via the int main() function?

Meno
  • 1
  • 3

1 Answers1

-1

Yes, you can use the returned value of distance in the main function as a parameter for another function:

int main(){
distance= getDistance();
finalCost = calculatefinalCost(distance);
..
}

By the way: you don't need a parameter in getDistance, the value will be replace by the user input anyway.

pTz
  • 144
  • 3