The loop is not ending if we losse!

#include

void pi(int df)

{

 std::cout <<"\nwelcome hacker";

 std::cout <<"\nU have to enter the correct combination for level="<<df;

}

bool p(int df)

{

 pi(df);

    int a=rand()%df+df;

    int b=rand()%df+df;

    int c=rand()%df+df;

      int sum=a+b+c;

      int  prod=a*b*c;



std::cout <<"\t\nGUESS: sum of three number is=" <<sum;

std::cout <<"\t\t\nGUESS: product ot three number is="<<prod;

       int G1, G2, G3;

                      std::cout<<"\n\tenter the number code to move forward\n";

       std::cin>>G1;

       std::cin>>G2;

       std::cin>>G3;



 std::cout<<"the numbers are\n"<<G1 <<std::endl <<G2 <<std::endl <<G3;

 int GS=G1+G2+G3;

 int GP=G1*G2*G3;

 if (  sum==GS && prod==GP)

 {

    std::cout<<"\nU cracked the code weldone\n";

    return true;

 }

       else

       {

           std::cout<<"\nU loose to crack the code u noob\n";

        return false;

       }

}

int main()

{

     int d=1;

     int const md=5;

       while (d <= md)

       {

         bool bcompletelvl=p(d);    

         std::cin.ignore();

         std::cin.clear();  

         if(d)

         ++d;

       }

return 0;

}

The loop will only exit if d>5, not if you lose. And d will never be higher than two, because if(d) is doing nothing. If you want to increase the level when the player wins, the code would be:

if (bcompletelv1) ++d;

If you want the program to exit then the code would be

if (bcompletelv1) ++d;
else break;

1 Like

Tysm❤️

1 Like

As an aside I would highly encourage you to give your functions and variables good names. pi for example, without looking at anything other than the name, would make me think that function would return an approximation of the number pi (3.14…)

1 Like

Noted❤️

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms