Member Avatar for Techboy1523

Ok I have to print two charts: one showing kilometers per hour converted to miles per hour and the second showing degrees Celsius converted to degrees Fahrenheit.

The speed conversion table should list speeds from 50 to 130 kilometers in increments of 5. (50, 55, 60 …)

The temperature conversion table should list degrees Celsius from 0 to 100 in increments of 4. (0, 4, 8 …)

Make sure to right justify the numbers in the columns.
Decimal values should be printed with one decimal place.
You should use a for loop to print each chart.
Use constants for the conversion factors.

I have the two charts and the celsius and and kilometers going in the right increments, but I can't get them to correctly convert. What's wrong with my code?

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

int main()
{
    int MPH;
    int KPH;
    MPH = KPH * 0.6214;
    cout << "kilometers \tmiles\n";
    cout << "per hour \tper hour\n\n";
    cout << "========\t========\n\n";
    
    cout << fixed << setprecision (1);
 for ( int MPH = 50; MPH <= 130 ; MPH +=5)
      cout << setw (5) << MPH << "\t\t" <<  MPH << setw(5) << endl;
    
    cout << endl<< endl;            
    
 
    
    cout << "\t===========\n";
    cout << "\tConversions\n";
    cout << "\t===========\n\n\n";
    
    cout << "Celsius  \tFahrenheit\n";
    cout << "========\t========\n\n";
    
    cout << fixed << setprecision (1);
    
      int C;
    for( C  = 0; C <= 100; C += 4)
    cout << setw(5) << C <<  " \t\t" <<((9/5) * C + 32) << setw(5) << endl;
    
    cout << endl<< endl;   
 


   cout <<endl;


   cout <<endl;
   system ("pause");
   return 0;
}
  • Doing

MPH = KPH * 0.6214; doesn't magically make the machine Do What You Mean (tm) later on.
[*]Use floating point, i.e. double MPH and 9.0/5.0 .

Hmm .. shouldn't that first loop be something like

for ( int KPH = 50; KPH <= 130 ; KPH +=5)
cout << setw (5) << KPH << "\t\t" << /* --- KPH converted to MPH here --- */ << setw(5) << endl;

Capitalized variable names are, by convention, constants. I think you are trying to follow your instructions here. It says the constants should be used for "conversion factors". A conversion factor is a constant number for which a value in one unit can be multiplied by to convert it to another unit.

If I wanted to convert from inches to centimeters, I could do this:

const double IN_TO_CM = 2.54;
double inches = 10;
double cm = inches * IN_TO_CM; //cm is equal to 10 inches in centimeters

If you need to iterator through a range of kilometers, do just that and use this method of conversion *within* the loop.

Use lowercase names for your loop variables and declare them in the loop:

for (int i=0; i<10; i++)
{
// do someting
}
Member Avatar for Techboy1523

Thanks for the help I figured it out.:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.