예제 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 입니다.";
}


블로그 이미지

irostub

iro의 잡화상점

,