lundi 1 mars 2021

how to return value from while loop in c++?

I have changed the scenario of the originally code. Generally, given that there is a rate of the factory producing certain number of sweets per day. For example, rate is 0.5. And if at first (day 0) the factory produce 1000 sweets. After day 1, the factory should be produced 1000*(1+0.5)=1500 sweets. And after day 2, the factory should be produced 1000*(1+0.5)*(1+0.5)=2250 sweets. And now i want to create a function to calculate the number of sweets that the factory has produced within a particular days

int ProduceSweets(float rate, int swt=10, int days=6)
{
// i want to calculate the number of sweets the factory has produced.
while (days !=0)
{
    swt = swt* (1+rate);
    -- days;

}
return swt;
   
}

int main()
{
float rate;
int swt;
int days;

cout << "enter the number of sweets that the factory has produced" <<endl;
cin >> swt ;
cout << "enter the rate of producing sweets per day" << endl;
cin >> rate ;
cout << "enter the number of days that the factory is producing sweets" <<endl;
cin >> days ;

ProduceSweets(rate, swt, days);
cout << " the factory has produced " << swt << "sweets" << endl;
}

However, if the rate is 0.5, the sweets is 1000 and the days is 2, the output(aka total sweets that the factory has been produced) should be 2250. But the output is still 1000 after i run the program. I cannot return the swt value from the function to the caller. What have i do wrong?

Aucun commentaire:

Enregistrer un commentaire