2014-09-18 5 views
0

간단한 변경 분류 프로그램을 만들려고합니다. 모든 부분이 제대로 작동하고 있으며 분기를 감지 할 것으로 예상됩니다. 전류 출력의모든 동전을 계산하지 않습니다.

예 :

The amount you entered is: 52.50 

You have this many Fifty dollars: 1 

You have this many Ten dollars: 0 

You have this many One: 2 

You have this many Quarters: 0 

원하는 출력 :

The amount you entered is: 52.50 

You have this many Fifty dollars: 1 

You have this many Ten dollars: 0 

You have this many One: 2 

You have this many Quarters: 2 

코드 :

#include <iostream> 

using namespace std; 

const int FIFTY = 50; 
const int TEN = 10; 
const int ONE = 1; 
const double QUARTER = 0.25; 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    int change; 

    cout << "Enter the amount of money in your wallet: "; 
    cin >> change; 
    cout << endl; 


    cout << "The amount you entered is: " << change << endl; 
    cout << "The number of Fifty dollars to be returned is: " << change/FIFTY << endl; 
    change = change % FIFTY; 

    // 
    cout << "The number of Ten dollars to be returned is: " << change/TEN << endl; 
    change = change % TEN; 

    // 
    cout << "The number of One dollars to be returned is: " << change/ONE << endl; 
    change = change % ONE; 

    // 
    cout << "The number of Quarters to be returned is: " << change/QUARTER << endl; 
    change = change % QUARTER; 

    return 0; 
} 

내가 갖는 2 개의 오류가 있습니다 :

Error 1 error C2297: '%' : illegal, right operand has type 'double' 

Error2 IntelliSense: expression must have integral or unscoped enum type 
+0

제 제안은 달러가 아닌 단위로 센트를 사용하는 것입니다. 그렇게하면 정수가 아닌 형식을 처리 할 필요가 없습니다. –

+0

모든 것을 센트로 변환하면이 문제를 피할 수 있습니다. –

+0

'FIFTY','TEN' 및 'ONE'은 모두 정수이지만'QUARTERS '는'double '(0.25)입니다. 부동 소수점 값으로 나눌 때 나머지 부분을 가져 오는 것은 의미가 없습니다. – Eric

답변

0

change 변수는 int 유형이므로 52.50을 전혀 저장하지 않습니다.

그러면 52을 읽고 중지합니다.

이 외에도 % 연산자에서 부동 소수점 값을 사용할 수 없습니다.

는 다음 int로 그 배치 가능한 부동 소수점 정밀도 문제를 방지하기 위해 아마도 작은 델타 (0.001 등)을 첨가하고, 백하여 승산 이중으로 값 판독 제안했다. 즉, 센트의 정수로 만드십시오.

그런 다음 계산을 수행하려면 int을 사용하십시오.

관련 문제