Erdowi Erdowi - 1 month ago 13
C Question

Syntax error in my C code?

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

VHS VHS
Answer

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