用于计算行驶里程和MPH的C ++程序

时间:2016-09-22 05:31:07

标签: c++

好的,所以我是C ++的新手,我真的不明白我在这个程序中做错了什么。

我需要让用户输入旅程的起点和终点里程以及所花费的小时数。然后我需要以英里和公里为单位打印结果。

我的测试变量是 从1230开始 1240.5结束 小时.12

结果应该是 英里10.5 mph 87.5 公里16.9 kph 140.8

但这不是我得到的。

// Lab 3 Exercise 2
// Calculate MPH (miles Per Hour) and KPH (Kilometers Per Hour).
//
// Program by: Mohamed El-Malah

#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{

    // Have the user enter his start and end mileage
    double start_mileage, end_mileage;

    cout << "Enter your starting mileage: ";
    cin >> start_mileage;

    cout << "Enter your end mileage: ";
    cin >> end_mileage;

    double trip_mileage = end_mileage - start_mileage;

    // Have user input the hours the trip took
    double total_hours, mph;


    cout << "How man hours was the trip: ";
    cin >> total_hours;

    mph = trip_mileage / total_hours;

    // Print the results in Miles and Kilometers

    double trip_kilometers, kph;

    trip_kilometers = trip_mileage * 1.6;
    kph = trip_kilometers / total_hours;

    cout << "Total miles " << setprecision(1) << setw(15) << trip_mileage << endl;
    cout << " Miles/Hour " << setw(15) << mph << endl;
    cout << "Total Kilometers" << setw(10) << trip_kilometers << endl;
    cout << " Kilometers/Hour" << setw(10) << kph << endl;


}

好的,所以我解决了在计算方程之前计算方程式的问题。

但是我仍然有类似的问题。我的答案不会像我需要的那样以单个小数点打印出来。

EX: 1e + 001而不是10.5 9e + 001而不是87.5

3 个答案:

答案 0 :(得分:2)

见这里:

double trip_mileage = end_mileage - start_mileage;

cout << "Enter your starting mileage: ";
cin >> start_mileage;

cout << "Enter your end mileage: ";
cin >> end_mileage;

您在向用户询问必要的输入之前计算行程里程。声明变量但直到稍后才进行计算:

double trip_mileage;

cout << "Enter your starting mileage: ";
cin >> start_mileage;

cout << "Enter your end mileage: ";
cin >> end_mileage;

trip_mileage = end_mileage - start_mileage;

您使用total_hours再次犯了一些错误。我会让你想出那个。

答案 1 :(得分:2)

更改这些行的顺序。因为在你输入mph之前,你早先计算total_hours。在这种情况下,total_hours被分配了垃圾值,结果也不同。

double total_hours, mph;
cout << "How man hours was the trip: ";
cin >> total_hours;
mph = trip_mileage / total_hours;

trip_mileage

相同
    cout << "Enter your starting mileage: ";
    cin >> start_mileage;

    cout << "Enter your end mileage: ";
    cin >> end_mileage;

    double trip_mileage = end_mileage - start_mileage;

答案 2 :(得分:2)

更正后的代码:

// Lab 3 Exercise 2
// Calculate MPH (miles Per Hour) and KPH (Kilometers Per Hour).
//
// Program by: Mohamed El-Malah

#include <iostream>
#include <iomanip>

using namespace std;

int main ()
{

    // Have the user enter his start and end mileage
    double start_mileage, end_mileage;

    cout << "Enter your starting mileage: ";
    cin >> start_mileage;

    cout << "Enter your end mileage: ";
    cin >> end_mileage;

    double trip_mileage = end_mileage - start_mileage;
    // Have user input the hours the trip took
    double total_hours, mph;


    cout << "How man hours was the trip: ";
    cin >> total_hours;

    mph = trip_mileage / total_hours;
    // Print the results in Miles and Kilometers

    double trip_kilometers, kph;

    trip_kilometers = trip_mileage * 1.6;
    kph = trip_kilometers / total_hours;

    /**** fixed stream manipulator makes cout not use scientific notation ****/

    cout << "Total miles " << fixed << setprecision(1) << setw(15) << trip_mileage << endl;
    cout << " Miles/Hour " << setw(15) << mph << endl;
    cout << "Total Kilometers" << setw(10) << trip_kilometers << endl;
    cout << " Kilometers/Hour" << setw(10) << kph << endl;


}

你已经搞乱了变量total_hourstrip_mileage的排序。确保在从用户获取相关输入后使用/计算变量的值,否则将使用随机值。

此外,为了使cout不使用科学记数法,您必须使用std::fixed流操纵器。