Program crashes when variable takes values larger than 7

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
  	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;
Last edited on
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <limits>

void waitForEnter();

int main()
{
    std::cout << "Keni zgjedhur te perdorni fillimisht 2 monedha per produkt, "
                 "pastaj nga 1 monedhe + mbetja nga 2 monedhat e para\n"
                 "Jepni buxhetin tuaj ne Euro: ";
    double Buxheti;
    std::cin >> Buxheti;
    std::cin.ignore(1);
    std::cout << "Jepni llojen e monedhave: ";
    double LlojiMonedhave;
    std::cin >> LlojiMonedhave;
    std::cin.ignore(1);
    double Monedhat = Buxheti / LlojiMonedhave;
    double e = 1.0, p = 0.0, Mbetja=0.0;
    while (Monedhat > e)
    {
        e += 2.0;
        p++;
        Mbetja += 1.5;
        while (Monedhat >= e)
        {
            if(Mbetja > 0.0)
            {
                e++;
                p++;
                Mbetja -= 0.5;
            }
            std::cout << "Monedhat: " << Monedhat << "; e: " << e
                      << "; p: " << p << "; Mbetja: " << Mbetja << '\n';
            waitForEnter();
        }
    }

    std::cout << "\nMe "<< Monedhat <<" monedha duke perdorur 2+1 ju mund te blini "
              << p << " produkte si dhe do keni mbetje prej " << Mbetja
              << " euro.\n";

    return 0;
}

void waitForEnter()
{
    std::cout << "\nPress ENTER to continue...\n";
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
}

Topic archived. No new replies allowed.