簡體   English   中英

C ++-如何添加用於貨幣計算器的多種情況的函數?

[英]C++ - How do I add functions to work on a multitude of cases for a currency calculator?

For my currency calculator, I need to add 3 different types of functions-
1) enterCostUSD (I enter the cost in USD)
2) convertCost (convert the cost from USD to, say, rupees)
3) displayOutput (display the output of the new cost)

我總共有10例。 如何添加功能,使其適用於所有10種情況?

我需要在每種情況的中間添加它們嗎?

這是一種情況,我是否需要以這種方式進行操作,或者有更好的方法?

    case 1: //Finding cost in Altairian Dollars
    cout << "Enter the cost in US Dollars: ";
    cin >> enterCostUSD;
    altairianDollars = enterCostUSD*0.72
    cout << "$" << enterCostUSD << " is equal to " << altairianDollars   
    cout << "Thank You for using the Currency Calculator!" << endl;
    break;

從您的問題中我了解到,您有10種情況下嘗試轉換貨幣。 因此,您必須創建10個問題中提到的案例。

如果您有所有案例共有的任何功能,則可以將其保留在一個函數中,並在每個cases.調用它cases.

在這里,打印輸出和轉換貨幣是可以分開放置的功能。

例如:

 case 2: //Finding cost in Rupee
    CostUSD=enterUSDCost();//function 1
    conversion_rate=63.5;//for rupee
    Rupee = convertCost(CostUSD,conversion_rate);//function2
    displayOutput(enterCosstUSD,Rupee,"Rs");//function 3
    break;

這三個功能需要超出您主要功能的范圍,這是三個獨立的功能。

float enterCostUSD()
{
float USD_cost;
cout << "Enter the cost in US Dollars: ";
        cin >> USD_cost;
return USD_cost;
}
float convertCost(float USD, float rate)
{
return USD*rate;
}

void displayOutput(float enterCostUSD, float Rupee,string conversion_currency){
cout << "$" << enterCostUSD << " is equal to " << Rupee <<conversion_currency   ;

    cout << "Thank You for using the Currency Calculator!" << endl;
    }

如果您想更好地了解開關盒,請點擊此鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM