如何避免totalCalc重置?

时间:2019-05-08 02:40:42

标签: c function

我正在制作一个程序,用于计算用户输入一定的美元价值时要提供的最有效的找零额。

该程序在尝试使其成为函数之前就已经起作用,并且当我尝试通过使用函数使程序更健壮时,该程序无法正常工作。

double getValueofCoins(int totalCalc, int coinValue, char* coinName) {

    double coinType;

    coinType = totalCalc / coinValue;
    totalCalc = totalCalc % coinValue; 
    printf("\n%s: %.0lf", coinName, coinType);

    return totalCalc;

}



int makeChange(int totalCalc) {

    double total;

    // calculate the most efficient amount of change

    getValueofCoins(totalCalc, 200, "Toonies");
    getValueofCoins(totalCalc, 100, "Loonies");
    getValueofCoins(totalCalc, 25, "Quarters");
    getValueofCoins(totalCalc, 10, "Dimes");
    getValueofCoins(totalCalc, 5, "Nickels");
    getValueofCoins(totalCalc, 1, "Pennies");

    return total;    
}

我想要代码执行的操作是给出最有效的找零金额(例如:如果用户输入20美元,则将显示10张toonies),但是它所做的是打印每种硬币的数量增值(例如:如果用户输入20美元,则表示10张印度桃花心,20张印度卢布,80个季度等)

我认为这是因为每次我调用getValueofCoins()时,totalCalc变量都将重置为用户最初输入的内容。我该如何解决?

0 个答案:

没有答案
相关问题