简体   繁体   中英

Why doesn't this simple C++ work?

I need to write a program that calculates the cost for the distance traveled following these rules

  • Each of the first 100 miles (inclusive) cost £5.50
  • Over 100 miles and up to 500 miles (inclusive): £4.00 per mile.
  • Over 500 miles: £2.50 per mile.

This is my program so far

#include <iostream>     //for cin >> and cout <<
using namespace std;


int main()          // main algorithm
{
    double distance;
    double Milecost1;
    double Milecost2;
    double Milecost3;
    double totalcost;

    cout << "enter distance";
    cin >> (distance);

    void CFM();

    if (distance >= 100) {
        Milecost1 = 100 * 5.50;
    } else {
        totalcost = distance * 5.50;
    }

    void CSM();

    if ( (distance >100) && (distance <= 500) ) {
        Milecost2 = (distance - 100) *4.00;
    }

    void CTM();
    if (distance > 500) {
        Milecost3 = distance - 500 * 2.50;
    }

    void totalcost1();
    totalcost = Milecost1 + Milecost2 + Milecost3;

    cout << "the total cost for the distance travelled is" << totalcost 

    system("pause");        //to hold the output screen
    return(0);
}

My first question is, is the maths correct to work out the cost?

Second question is I run the program and it says Milecost 2 is being used without being initialized how do I solve this?

No, the math is not correct, eg with distance = 501 , you'd get

Milecost1: 550
Milecost2: (unitialised)
Milecost3: 2.50

That's assuming you correct the operator precedence on Milecost3 , since right now you're multiplying 500 times 2.5 and subtracting that from distance .

Milecost2 is only assigned when distance is within its relevant values, instead, it should be 0 for distance <= 100 AND it should also be calculated when distance > 500 , if I understood the exercise correctly.

I would write it down as:

#include <iostream>     //for cin >> and cout <<
using namespace std;


int main()          // main algorithm
{
    double distance;
    double Milecost1;
    double Milecost2;
    double Milecost3;
    double totalcost;


    cout << "enter distance";
    cin >> (distance);

    if (distance >= 100) {
        Milecost1 = 100 * 5.50;
    } else {
       Milecost1 = distance * 5.50;
    }

    if ( (distance >100) && (distance <= 500) ) {
       Milecost2 = (distance - 100) *4.00;
    } else {
        Milecost2 = 0;
    }

    if (distance > 500) {
        Milecost3 = distance - 500 * 2.50;
    } else {
        Milecost3 = 0;
    }

    totalcost = Milecost1 + Milecost2 + Milecost3;

    cout << "the total cost for the distance travelled is" << totalcost ;

    system("pause");        //to hold the output screen
    return(0);
 }

getting rid of your calls to undefined functions; and not depending on initialization of variables to 0.

I think this should work fine

void main(void)
{
double distance;
double cost;
if(distance <= 100)
{
    cost = distance * 5.50;
}
else if(distance > 100 && distance <= 500)
{
    cost = 100 * 5.50;
    cost += 4.00 * (distance - 100);
}
else
{
    int temp = 100;
    cost = temp * 5,50;
    temp = 400;
    cost += temp * 4.00;
    temp = distance - 500;
    cost += temp * 2.50;
}
}

Personally, this is the way I'd do it:

#include <iostream>
#include <algorithm>
using namespace std;

int main() {
    double distance;
    cout << "Enter distance (Miles): ";
    cin >> distance;

    double milesOver500   = max(0.0, distance-500.0);
    double milesInBetween = max(0.0, distance-milesOver500-100.0);
    double milesUnder100  = max(0.0, distance-milesInBetween-milesOver500);

    double totalCost = milesOver500 * 2.5 +
                       milesInBetween * 4.0 + 
                       milesUnder100 * 5.5;

    cout << "The total cost for the distance traveled is £" << totalCost << endl;

    return 0;
}

In your implementation, Milecost2 is only initialized if the conditional (distance >100) && (distance <= 500) is true, which won't always be the case. In the code above, everything is initialized at the time of its declaration with a meaningful value that doesn't need to be computed later. It just so happens that this approach works well in this case, but there are others where it's a bit more difficult to do so. When such cases arise you should try to assign some meaningful default value to each variable you declare at the time of its declaration which can change if certain conditions arise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM