程序计算英里旅行和MPH

C++ Program to calculate Miles traveled and MPH

本文关键字:MPH 旅行 英里 计算 程序      更新时间:2023-10-16

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

我需要让用户输入一次旅行的开始和结束里程以及所花费的小时数。然后我需要以英里和公里的形式打印结果。

我的测试变量是1230年开始1240.5结束时间点

结果应该是10.5英里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;

}

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

然而我仍然有一个类似的问题。我的答案并没有像我需要的那样以小数点形式打印出来。

交货:1e+001而不是10.59e+001代替87.5

更正代码:

// 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的顺序。请确保在获取用户的相关输入后使用/计算变量的值,否则将使用随机值。

另外,为了使计数不使用科学符号,必须使用std::fixed流操纵器。

看这里:

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又犯了一些错误。我让你自己想。

改变这些行的顺序。因为之前你在计算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;