J
John Ruan
This is a small program in C++. I want to try to type in real number instead
of integer to see how the program runs.
And I am confused. If I input integer for both variables, the program runs
fine. But if I input a real number for the height (like 68.2), the second
cin doesn't work at all!!
I expected any real number will be truncated and that is my only
expectation. But it seems that there are other problems. Can anybody try it?
#include <iostream>
using namespace std;
int main(void)
{
const double INCHES_PER_METER = 39.37;
const double POUNDS_PER_KG = 2.24;
int height;
int weight;
cout << "METRIC CONVERTER" << endl << endl ;
cout << "Enter your height in inches " ;
cout << "(No fractions, please!) : " ;
cin >> height;
cout << "Enter your weight in pounds" ;
cout << "(No fractions, please!)" ;
cin >> weight;
cout << endl ;
double metric_height = height/INCHES_PER_METER;
double metric_weight = weight/POUNDS_PER_KG;
cout << "Your height is " << metric_height << " meters." << endl;
cout << "Your weight is " << metric_weight << " kilograms." << endl;
return 0;
}
of integer to see how the program runs.
And I am confused. If I input integer for both variables, the program runs
fine. But if I input a real number for the height (like 68.2), the second
cin doesn't work at all!!
I expected any real number will be truncated and that is my only
expectation. But it seems that there are other problems. Can anybody try it?
#include <iostream>
using namespace std;
int main(void)
{
const double INCHES_PER_METER = 39.37;
const double POUNDS_PER_KG = 2.24;
int height;
int weight;
cout << "METRIC CONVERTER" << endl << endl ;
cout << "Enter your height in inches " ;
cout << "(No fractions, please!) : " ;
cin >> height;
cout << "Enter your weight in pounds" ;
cout << "(No fractions, please!)" ;
cin >> weight;
cout << endl ;
double metric_height = height/INCHES_PER_METER;
double metric_weight = weight/POUNDS_PER_KG;
cout << "Your height is " << metric_height << " meters." << endl;
cout << "Your weight is " << metric_weight << " kilograms." << endl;
return 0;
}