본문 바로가기

Programming language/C++

[C++] 환율 계산 프로그램

#include <iostream>
using namespace std;

int main() {
    double exchangeRate;
    double amountInDollars;

    cout << "Currency Converter" << endl;
    cout << "------------------" << endl;

    cout << "Enter exchange rate (1 USD to your currency): ";
    cin >> exchangeRate;

    if (exchangeRate <= 0) {
        cout << "Invalid exchange rate. Please enter a positive value." << endl;
        return 1; // Exit with an error code
    }

    cout << "Enter amount in USD: $";
    cin >> amountInDollars;

    if (amountInDollars < 0) {
        cout << "Invalid amount. Please enter a positive value." << endl;
        return 1; // Exit with an error code
    }

    double convertedAmount = amountInDollars * exchangeRate;

    cout << "Equivalent amount in your currency: " << convertedAmount << endl;

    return 0; // Exit successfully
}

출력 결과

Currency Converter
------------------
Enter exchange rate (1 USD to your currency): 0.85
Enter amount in USD: $100
Equivalent amount in your currency: 85

이 예제에서는 사용자가 환율 0.85(1달러를 해당 통화의 0.85로 환산)와 금액 $100을 입력했습니다. 

프로그램은 해당 금액을 사용자 통화로 계산하여 표시했으며, 이 경우 85입니다.