Why is this returning Garbage? : <p>I am making a calculator for a game.<br>
I usually first make sure the code is working in C++, and then convert it to the desired language. I had made this program. In this program, some of the times, optDura stores the correct value and other times, it stores garbage.
<br><br>
Here is the code :-</p>

<pre><code>#include &lt;iostream&gt;
using namespace std;

/* 
    cd - current Durability
    se - smith efficiency in decimal
    sc - smith charges in decimal
    sE - smith efficiency in %
    sC - smith charges in %
    ic - initial cost
    md - maximum durability
    tmd - temporary maximum durability
    tD - temporary durability
    td - total durability
    rc - repair cost
    cpb - cost per battle
*/
int main(){

    long int currDura, maxDura, tempMaxDura, tempDura, totDura, optDura;
    double iniCost, repCost;
    long int  smithEffi, smithCharge;
    double se, sc;
    double totCostTillNow, costPerBattle = 0, minCPB;
    int i;
    long int repCount = 1;
    iniCost = 25500;
    currDura = 58;
    maxDura = 59;
    repCost = 27414;
    se = 0.90;
    sc = 0.85;
    tempMaxDura = maxDura;
    tempDura = currDura;
    totDura = tempDura;
    totCostTillNow = iniCost;
    costPerBattle = totCostTillNow / totDura;
    minCPB = costPerBattle;
    cout&lt;&lt;"\n Cost without any repair = "&lt;&lt;costPerBattle;

    for(i=1; i&lt;=maxDura; i++)
    {
        totCostTillNow += (double) repCost * sc;
        tempDura = tempMaxDura * se;
        totDura += tempDura;
        costPerBattle = (double) (totCostTillNow / totDura);
        tempMaxDura -= 1;
        if ( minCPB &gt;=  costPerBattle )
        {
            minCPB = costPerBattle;
            optDura = tempMaxDura;
        }
    }
    cout&lt;&lt;"\n Minimum cost per battle = "&lt;&lt;minCPB&lt;&lt;" &amp; sell at 0/"&lt;&lt;optDura;
    return 0;
}
</code></pre>

<p>The problem occurs when repCost is >= 27414. For values less than that, it gives the desired result. I am unable to figure out why this is happening.</p>

<p>Thanks a lot</p>
