double e=1,p=0,Monedhat,Buxheti,LlojiMonedhave,Mbetja=0;
cout <<"Keni zgjedhur te perdorni fillimisht 2 monedha per produkt, pastaj nga 1 monedhe + mbetja nga 2 monedhat e para\n";
cout << "Jepni buxhetin tuaj ne Euro: ";
cin >> Buxheti;
cout <<endl<<"Jepni llojen e monedhave: ";
cin >> LlojiMonedhave;
cout <<endl;
Monedhat=Buxheti/LlojiMonedhave;
while (Monedhat>e)
{
e=e+2;p=p+1;Mbetja=Mbetja+1.5;
while (Monedhat>=e)
if(Mbetja>0)
{
e=e+1;p=p+1;Mbetja=Mbetja-0.5;
} ;
};
cout << "Me "<<Monedhat<<" monedha duke perdorur 2+1 ju mund te blini "<<p<<" produkte si dhe do keni mbetje prej "<<Mbetja<<" euro.";
return 0;
Hello I wrote a code and the problem stands when the value at "Monedhat" is higher than 7 the program crashes. Can you help me fix the problem please.
It doesn’t crash, but it enters into an endless loop.
The problem is that after this line: e=e+2;p=p+1;Mbetja=Mbetja+1.5;
Mbetja == 1.5
But later it’s decreased by 0.5 at every iteration. After 3 cycles, it becomes equal to 0, so the if statement if(Mbetja>0)
is not executed any more.
Therefore, ‘e’ does not increase any more, so the “while” condition while (Monedhat>e)
holds true forever.
I’m so little used to such a cramped coding style that I was quite unable to read yours, so I needed to change it a while to write the following example which displays the variables content at every iteration: