-3

Ok so basically, I am trying to make a program that will take a KPH of 185 and convert it to MPH all the way to 0 like so. (using prototypes) Kilometers per hour converted to miles per hour:

Kph     Mph
185     115
180     112
175     109
... ...
10      6
5       3
0       0

Unfortunately my conversion is a bit off, can someone heed some information on why that may be?

#include <stdio.h>

// Prototypes
double mph2kph(double); // convert Miles to KM
double kph2mph(double); // convert KM to Miles

int main()
{

    int loop = 1; 
    double kph = 185;              // kilometers per hour   
    double mph = 115;              // miles per hour for computation 

    printf("Kilometers per hour converted to miles per hour: \n");
    printf("Kph          Mph\n"); // Display Header

    while (loop == 1){
        printf("%.2d     %.2d \n", kph, kph2mph(kph));
        break;
    }
loop = 0;

}

//Other Functions:
double mph2kph(double x){
    return x*1.61;
}

double kph2mph(double x){
    return x*1.61;
}

Output =

Kilometers per hour converted to miles per hour:                                                                                                                                             
Kph          Mph                                                                                                                                                                             
40325120     38090656
John3136
  • 28,809
  • 4
  • 51
  • 69
Ardowi
  • 59
  • 7

2 Answers2

2

You are using %d to show your final result, which is used for int variables. In your case, as you are using double variables, you should go for %f or %lf.

printf("%.2lf     %.2lf \n", kph, kph2mph(kph));

Also, your kilometers per hour to miles per hour conversion function is wrong. You should divide and not multiply.

double kph2mph(double x){
    return x/1.61;
}

Testing your code with those corrections leads to correct results:

Kilometers per hour converted to miles per hour: 
Kph          Mph
185.00     114.91 
Izuka
  • 2,572
  • 5
  • 29
  • 34
  • [`%lf` and `%f` are handled identically for `printf`](http://stackoverflow.com/a/4264154/1270789). – Ken Y-N Nov 09 '16 at 01:59
  • @KenY-N Fair point. I'm editing the answer this way. – Izuka Nov 09 '16 at 02:00
  • @Isuka: Even though `%f` works fine four `double`, there was no reason to edit the answer. Quite the opposite, since C99 `%lf` is supported by `printf`. And it is actually a better idea to use `%lf` for `double` and keep `%f` reserved for `float`. That keeps `printf` consistent with `scanf`. – AnT stands with Russia Nov 09 '16 at 02:03
0

I have made some changes to your program to give your desired output. Some errors in your program are already identified by some other users. Compare this with yours and try to learn. Best of luck!

#include <stdio.h>

// Prototypes
double mph2kph(double); // convert Miles to KM
double kph2mph(double); // convert KM to Miles

int main()
{

int loop = 1; 
int kph = 185;              // kilometers per hour   
double mph = 115;              // miles per hour for computation 

printf("Kilometers per hour converted to miles per hour: \n");
printf("Kph          Mph\n"); // Display Header

while (kph != -5){
    printf("%d     %.2lf \n", kph, kph2mph(kph));
    getchar();
    kph = kph - 5;
}

 }

//Other Functions:
double mph2kph(double x){
    return x*1.61;
}

double kph2mph(double x){
   return x/1.61;
}
VHS
  • 9,534
  • 3
  • 19
  • 43
  • Thanks for the help, I wasn't aware that a while loop would be the best option. How would I make it completely loop until kph = 0 and mph = 0? – Ardowi Nov 09 '16 at 03:07
  • The condition `kph != -5` makes sure that the `while` loop continues until kph reaches -5. Note that kph is decremented by 5 on every iteration. – VHS Nov 09 '16 at 03:09