function
May 5, 2020 at 11:53am UTC
This is a parking charge calculator program. I used function to calculate the charge for each car. I could not find out how I could get the total charge because there are no variables that I could sum. Could someone help me with it please?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
double calculateCharges(double );
int main(){
double hours1{1.5};
double hours2{4.0};
double hours3{24.0};
double totalHours{0};
double totalCarges{0};
cout << "Car" <<setw(9) << "Hours"
<< setw(13) << "Charges" << endl;
cout << setprecision(2) << fixed<< "1" << setw(11) << hours1
<< setw(8) << "$" << calculateCharges(hours1)
<< endl;
cout << "2" << setw(11) << hours2
<< setw(8) << "$" << calculateCharges(hours2)
<< endl;
cout << "3" << setw(11) << hours3
<< setw(8) << "$" << calculateCharges(hours3)
<< endl;
totalHours = hours1 + hours2 + hours3;
cout << "Total" << setw(7) << totalHours
<< setw(8) << endl;
}
double calculateCharges(double hour){
double charge{0};
if (hour <= 3){
charge = 20.00;
}
else if (hour >3 && hour <24){
charge = 20.00 + (hour-3) * 5.0;
}
else if (hour == 24){
charge = 50.00;
}
return charge;
}
May 5, 2020 at 12:23pm UTC
I could not find out how I could get the total charge because there are no variables that I could sum.
Why don't you use totalHours{0} double totalCarges{0}?
May 5, 2020 at 12:43pm UTC
Hello medosz,
To go with what
Thomas1965 has said:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
double totalCarges{ 0 };
cout << "Car" << setw(9) << "Hours"
<< setw(13) << "Charges" << endl;
charges1 = calculateCharges(hours1);
cout << setprecision(2) << fixed << "1" << setw(11) << hours1
<< setw(8) << "$" << charges1
<< endl;
cout << "2" << setw(11) << hours2
<< setw(8) << "$" << calculateCharges(hours2)
<< endl;
cout << "3" << setw(11) << hours3
<< setw(8) << "$" << calculateCharges(hours3)
<< endl;
totalHours = hours1 + hours2 + hours3;
totalCarges = charges1 + ...;
cout << "Total" << setw(7) << totalHours
<< setw(8) << endl;
For line 1: the (0) zero should be (0.0), but empty {}s will do the same thing and set the variable to (0.0).
This is just one idea. You could use
totalChargrs += calculateCharges(hours1);
and leave the function call in the "cout" statement. It really depends on what you need to do and what you want to keep.
Andy
P.S. the blank lines really help the readability.
Last edited on May 5, 2020 at 12:44pm UTC
May 5, 2020 at 12:54pm UTC
Thank you for your help. I like the idea.
May 5, 2020 at 2:20pm UTC
Hello medosz,
Playing around with the program I came up with this to do it in a different way. See what you think and use what you want.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
double calculateCharges(double );
int main()
{
double hours1{ 1.5 };
double hours2{ 4.0 };
double hours3{ 24.0 };
double totalHours{};
double totalCharges{};
std::cout << std::fixed << std::setprecision(2); // <--- Only needs done once.
cout << "Car" << setw(9) << "Hours"
<< setw(13) << "Charges" << endl;
std::cout << std::string(25, '-' ) << '\n' ;
totalCharges += calculateCharges(hours1);
cout << " 1" << setw(11) << hours1
<< setw(6) << "$" << std::setw(6) << calculateCharges(hours1)
<< endl;
totalCharges += calculateCharges(hours2);
cout << " 2" << setw(11) << hours2
<< setw(6) << "$" << std::setw(6) << calculateCharges(hours2)
<< endl;
totalCharges += calculateCharges(hours3);
cout << " 3" << setw(11) << hours3
<< setw(6) << "$" << std::setw(6) << calculateCharges(hours3)
<< endl;
totalHours = hours1 + hours2 + hours3;
std::cout << std::string(25, '-' ) << '\n' ;
cout << "Total" << setw(8) << totalHours
<< setw(6) << '$' << std::setw(6) << totalCharges << endl;
return 0; // <--- Not required, but makes a good break point.
}
double calculateCharges(double hour)
{
double charge{ 0 };
if (hour <= 3)
{
charge = 20.00;
}
else if (hour > 3 && hour < 24)
{
charge = 20.00 + (hour - 3) * 5.0;
}
else if (hour == 24)
{
charge = 50.00;
}
return charge;
}
Andy
Topic archived. No new replies allowed.