Mohamed Elmalah - 1 year ago 59

C++ Question

Okay so I am new to C++ and I really don't understand what I am doing wrong in this program.

I need to have the user input the start and end mileage of a trip and the amount of hours it took. I then need to print the results in miles and kilometers.

My test variables are

start 1230

end 1240.5

hours .12

The results should be

Miles 10.5

mph 87.5

kilometers 16.9

kph 140.8

But that is not what I get.

`// 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;

}

Okay so I fixed the problem that I was computing the equations before I had the values.

However I still have a similar problem. My answers are not printed in single decimal points like I need them to be.

EX:

1e+001 instead of 10.5

9e+001 instead of 87.5

Answer Source

Corrected code:

```
// 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;
}
```

You had messed with the ordering of variables `total_hours`

and `trip_mileage`

. Make sure you use/calculate the value of the variables after you have taken relevant input from the user, otherwise random values will be used.

Additionally, to make cout not use scientific notation, you must use `std::fixed`

stream manipulator.