Search code examples
c++garbage

Why is this returning Garbage?


I am making a calculator for a game.
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.

Here is the code :-

#include <iostream>
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<<"\n Cost without any repair = "<<costPerBattle;

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

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.

Thanks a lot


Solution

  • If you rewrite to intialise your variables instead of using the "declare and assign" anti-"pattern" (I also removed unused variables):

    int main(){  
        long int currDura = 58;
        long int maxDura = 59;
        long int tempMaxDura = maxDura;
        long int tempDura = currDura;
        long int totDura = tempDura;
        long int optDura; 
        double iniCost = 25500;
        double repCost = 27414;
        double se = 0.90;
        double sc = 0.85;
        double totCostTillNow = iniCost;
        double costPerBattle = totCostTillNow / totDura;
        double minCPB = costPerBattle;
    
        cout<<"\n Cost without any repair = "<<costPerBattle;
    
        for(int i=1; i<=maxDura; i++)
        {
            totCostTillNow += repCost * sc;
            tempDura = tempMaxDura * se;
            totDura += tempDura;
            costPerBattle = totCostTillNow / totDura;
            tempMaxDura -= 1;
            if ( minCPB >=  costPerBattle )
            {
                minCPB = costPerBattle;
                optDura = tempMaxDura;
            }
        }
        cout<<"\n Minimum cost per battle = "<<minCPB<<" & sell at 0/"<<optDura;
        return 0;
    }
    

    then optDura sticks out as the only one missing initialisation.
    The only time it's assigned a value is if minCPB >= costPerBattle, so if that condition never holds you're left with an indeterminate value.

    Initialise it to something sensible.