게시판 즐겨찾기
편집
드래그 앤 드롭으로
즐겨찾기 아이콘 위치 수정이 가능합니다.
C++ BMI Calculator 다시
게시물ID : programmer_10199짧은주소 복사하기
작성자 : Benny
추천 : 0
조회수 : 342회
댓글수 : 2개
등록시간 : 2015/05/18 12:54:03
옵션
  • 본인삭제금지
항상 프로그래머 게시판에서 많은 정보와 도움을 얻고 가는 프로그래밍 공부하는 학생입니다 :-)
얼마전에 같은 질문으로 글을 올렸는데 아직도 해결이 되지 않아서 이렇게 다시 염치없게 질문 합니다 ㅠㅠ

kg, lbs, feet, inch, m, cm 으로 입력시 결과를 도출하는 건데 (예: 60kg and 1.7m) 

feet과 inch를 '(single quotation) 와 "(double quotation)으로 입력시 결과가 다오게 하려면 어떻게 해야 하는지 아무리 머리를
쥐어짜도 답이 안나오네요 ㅠㅠ

요약: 5 ft6 in 를 5'6"로도 입력 가능하게 해서 같은 값을 나오게 하고 싶음 

#include <iostream>
#include <string>
#include <iomanip>
#include <cmath>

using namespace std;

double weight_convert(double, string);
double height_convert(double, string);

int main()
{
double weight;
double height;
// double height_add;
string weight_unit;
string height_unit;
// string height_unit_add;
string and;
cout << "Welcome to BMI Calculator!" << endl;
cout << "--------------------------" << endl;
cout << "Enter your weight in kg or lb and height in m, cm, ft, or in: ";
cin >> weight;
cin >> weight_unit;
cin >> and;
cin >> height;
cin >> height_unit;
// bmi formular using double, pow
double bmi = weight_convert(weight, weight_unit) / pow(height_convert(height, height_unit), 2);
cout << "\nBMI: " << fixed << setprecision(3) << bmi << endl;

//    name: category
//  return: if
// purpose: to determine the category of the person depends upon the BMI calculated
//  accept: if, else if
if (bmi < 15)
{
cout << "You are very severely underweight.\n";
}
else if (bmi >= 15.0 && bmi < 16.0)
{
cout << "You are severely underweight.\n";
}
else if (bmi >= 16.0 && bmi < 18.5)
{
cout << "You are underweight.\n";
}
else if (bmi >= 18.5 && bmi < 25)
{
cout << "You are normal(Healthy weight!)\n";
}
else if (bmi >= 30 && bmi < 35)
{
cout << "You are obese class 1 (Moderately obese!)\n";
}
else if (bmi >= 35 && bmi < 40)
{
cout << "You are obese class 2 (Severely obese!)\n";
}
else if (bmi >= 40)
{
cout << "You are obese class 3 (Very severely obese!)\n";
}
}

//    name: weight_convert
//  return: double
// purpose: to convert other units into 'kg'
//  accept: double, string
double weight_convert(double weight, string unit)
{
if (unit == "lb")
return weight * 0.453592;
else if (unit == "kg")
return weight;
cout << "Error: Invalid input. Please try again!" << endl;
exit(0);
}

double height_convert(double height, string unit)
{
if (unit == "m")
return height;
else if (unit == "cm")
return height * 0.01;
else if (unit == "ft")
return height * 0.3048;
else if (unit == "in")
return height * 0.0254;
cout << "Error: Invalid input. Please try again!" << endl;
exit(0);
}
전체 추천리스트 보기
새로운 댓글이 없습니다.
새로운 댓글 확인하기
글쓰기
◀뒤로가기
PC버전
맨위로▲
공지 운영 자료창고 청소년보호