mobile wallpaper 1
mobile wallpaper 2
mobile wallpaper 3
mobile wallpaper 4
mobile wallpaper 5
mobile wallpaper 6
162 자
1 분
C++ cm를 feet&inch로 변환하기
2018-09-05

예제 1)

cm로 값을 입력받아 feet&inch로 표현하여라.

결과 예)

키를 cm로 입력하세요 : 190

6feet 2inch 입니다.

#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int cm, feet, inch = 0;
cout << "키(cm)를 입력하세요 : ";
cin >> cm;
inch = cm / 2.54f;
feet= inch / 12;
inch %=12;
cout << feet<<" feet "<< inch<<" inch 입니다.";

문제점. inch의 정확한 소수점 표현 실패

조금 생각해서 Float 변수 선언해주고 나눗셈 결과를 Float 변수와 int 변수에 저장해서

int 변수에 들어가있는 제수를 Float 변수에서 빼줬다.

일단 꽤 정확하게 나오는데는 성공했지만, 문제는 보기에 이쁘지않다는 것

실행 결과가 xx feet 0.xxxxxxinch 입니다.

‘0.’ 이녀석 지우고싶다.

#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
int cm, feet;
float inch, temp;
cout << "키(cm)를 입력하세요 : ";
cin >> cm;
inch = cm / 2.54f;
feet= temp = inch / 12;
inch = temp-feet;
cout << feet<<" feet "<< inch<<" inch 입니다.";
}
공유하기

이 글이 도움이 되었다면 더 많은 분들께 공유해 주세요!

C++ cm를 feet&inch로 변환하기
https://nirobase.me/posts/180905-cpp-cm-feet-inch/
작성자
irostub
게시일
2018-09-05
라이선스
CC BY-NC-SA 4.0

일부 정보가 오래되었을 수 있습니다

목차