conversion code error

Hello,

First option km -> m option works but the second option m-> km doesn't work. Please help me figure out why it is not working.


//m-km converter

#include <iostream.h>
#include <iomanip.h>
#include <stdlib.h>

void c_to_f(void);
void f_to_c(void);

int main()
{
int choice;
char again;

do
{
system("CLS");

cout << setw(10) <<" "<< " What conversion would you like to make?\n"; // menu
cout << setw(20) <<" "<< "1. km to m\n\n"; // make a choice which function to use.
cout << setw(20) <<" "<< "2. m to km\n\n";
cin >> choice;

switch(choice) // go to chosen function.
{
case 1 :
{
c_to_f();
break;
}
case 2 :
{
f_to_c();
break;
}
default :
{
cout << setw(10) <<" "<< "you must enter 1 or 2 "<< endl ; // validate and correct input of function choice.

}


}



cout << setw(10) <<" "<< "Do you wish to do another conversion? y for yes, n for no "; // rerun loop on y for yes
cin >> again;
}while (again == 'Y' || again =='y');

}


void c_to_f(void)
{
system("CLS"); // clear screen for function data.
int km,m;

cout << "\n\n\n";
cout << setw(10) <<" "<< "Enter your value \a";
cin >> km;

m = km * 1000;
cout << endl << setw(10) <<" "<< km << " km is " << m << " m \a\n\n\n";
}

void f_to_c(void)
{
system("CLS"); // clear screen for function data.
int km,m;

cout << "\n\n\n";
cout << setw(10) <<" "<< "enter the value \a";
cin >> m;

km = m / 1000;

cout << endl << setw(10) <<" "<< km << " m is " << km << " km \a\n\n\n";
}

km = m / 1000;

km must be a float.

When km is an int
and m = 777 then 777 / 1000 is 0
If m = 1234 then 1234 / 1000 is 1

BTW. It's better to use some meaningful names for your variables and functions.

instead void f_to_c(void) better MeterToKilometer
Ah okay. I understand. Thank you very much for your advice.
Topic archived. No new replies allowed.