0

Hi all,

I'm trying to write the code for a program that will convert MPH to KPH and vice versa. Here is what I have so far....

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

 
int main()
{
const double kph_to_mph = 0.6214;
int kph;
double mph = kph * kph_to_mph;
cout << "kilometers \tmiles\n";
cout << "per hour \tper hour\n\n";
cout << "========\t========\n\n";
 
cout << fixed << setprecision (1);
 for ( int kph = 0; kph <= 150; kph +=5)
cout << setw (5) << kph << "\t\t" << mph << setw(5) << endl;

system("PAUSE");
return 0;
}

As of now, the table is not showing the correct conversions so any bit of help would be great. Thanks!

2
Contributors
1
Reply
2
Views
9 Years
Discussion Span
Last Post by skatamatic
0

kph should be a double. When you make consts, read the data type from right to left. const double kph_to_mph should be double const kph_to_mph. In your loop you are not really doing any calculating. Instead, you are displaying the initial value of kph * kph_to_mph. Since kph is uninitialized it will be very erratic results. You should initialize kph to zero. In your loop, just do this cout << setw (5) << kph << "\t\t" << kph * kph_to_mph << endl;

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.