Jump to content

Can't find the error

Go to solution Solved by Mira Yurizaki,

I added comments:

#pragma endregion
	private: System::Void btnAction_Click(System::Object^  sender, System::EventArgs^  e) {
	
	int iMin = 0, iMax = 0, i =0, iDrapeau = 0;
	
	if(txtEntier1->Text != "")
	{
		if(txtEntier2->Text != "")
		{
			iMin.TryParse(txtEntier1->Text,iMin);
			iMax.TryParse(txtEntier2->Text,iMax);
			
			while(iMin < iMax)
			{
				for(i = 2;i <= iMin/2; ++i)
				{
					if(iMin % i == 0)
					{
						iDrapeau = 1;
						break; // This only breaks out of the for loop
					}
				}
				if(iDrapeau == 0)
				{
					lblNombrePremier->Text += ", " + iMin.ToString();
					iMin++; // If iDrapeau is 1, this never gets ran, and so iMin < iMax is always going to be true
                    // And it doesn't appear you have iDrapeau = 0 anywhere else
				}
			}
		}
		else
		{
			MessageBox::Show("Entre un nombre entier maximum valide!");
		}
	}
	else
	{
		MessageBox::Show("Entre un nombre entier minimum valide!");
	}

}

 

Hello there, this is a program in windows form based in C that prints the prime numbers within an interval into a label when the button is pressed. The program compiles with no errors but when the button is clicked, the program just freezes. Iv'e rechecked my loop as the typical error would be an infinite loop but I can't seem to find where it breaks up.

 

#pragma endregion
	private: System::Void btnAction_Click(System::Object^  sender, System::EventArgs^  e) {
	
	int iMin = 0, iMax = 0, i =0, iDrapeau = 0;
	
	if(txtEntier1->Text != "")
	{
		if(txtEntier2->Text != "")
		{
			iMin.TryParse(txtEntier1->Text,iMin);
			iMax.TryParse(txtEntier2->Text,iMax);
			
			while(iMin < iMax)
			{
				for(i = 2;i <= iMin/2; ++i)
				{
					if(iMin % i == 0)
					{
						iDrapeau = 1;
						break;
					}
				}
				if(iDrapeau == 0)
				{
					lblNombrePremier->Text += ", " + iMin.ToString();
					iMin++;
				}
			}
		}
		else
		{
			MessageBox::Show("Entre un nombre entier maximum valide!");
		}
	}
	else
	{
		MessageBox::Show("Entre un nombre entier minimum valide!");
	}

}

 

CPU: Intel Core i9-10900K  MOBO: ROG MAXIMUS XII FORMULA GPU: 2080ti Hall of Fame 10th anniversary limited edition  PSU: Asus ROG THOR 1200W  COOLER: Optimus foundation black acetal RADS: 3x EKWB CoolStream PE 360  LOOP: EKWB torque HDC fittings / EKWB ZMT 15,9/9,5mm / EKWB CryoFuel Clear MONITOR: Acer predator X34

Link to comment
https://linustechtips.com/topic/775506-cant-find-the-error/
Share on other sites

Link to post
Share on other sites

I added comments:

#pragma endregion
	private: System::Void btnAction_Click(System::Object^  sender, System::EventArgs^  e) {
	
	int iMin = 0, iMax = 0, i =0, iDrapeau = 0;
	
	if(txtEntier1->Text != "")
	{
		if(txtEntier2->Text != "")
		{
			iMin.TryParse(txtEntier1->Text,iMin);
			iMax.TryParse(txtEntier2->Text,iMax);
			
			while(iMin < iMax)
			{
				for(i = 2;i <= iMin/2; ++i)
				{
					if(iMin % i == 0)
					{
						iDrapeau = 1;
						break; // This only breaks out of the for loop
					}
				}
				if(iDrapeau == 0)
				{
					lblNombrePremier->Text += ", " + iMin.ToString();
					iMin++; // If iDrapeau is 1, this never gets ran, and so iMin < iMax is always going to be true
                    // And it doesn't appear you have iDrapeau = 0 anywhere else
				}
			}
		}
		else
		{
			MessageBox::Show("Entre un nombre entier maximum valide!");
		}
	}
	else
	{
		MessageBox::Show("Entre un nombre entier minimum valide!");
	}

}

 

Link to comment
https://linustechtips.com/topic/775506-cant-find-the-error/#findComment-9783027
Share on other sites

Link to post
Share on other sites

1 hour ago, M.Yurizaki said:

I added comments:


#pragma endregion
	private: System::Void btnAction_Click(System::Object^  sender, System::EventArgs^  e) {
	
	int iMin = 0, iMax = 0, i =0, iDrapeau = 0;
	
	if(txtEntier1->Text != "")
	{
		if(txtEntier2->Text != "")
		{
			iMin.TryParse(txtEntier1->Text,iMin);
			iMax.TryParse(txtEntier2->Text,iMax);
			
			while(iMin < iMax)
			{
				for(i = 2;i <= iMin/2; ++i)
				{
					if(iMin % i == 0)
					{
						iDrapeau = 1;
						break; // This only breaks out of the for loop
					}
				}
				if(iDrapeau == 0)
				{
					lblNombrePremier->Text += ", " + iMin.ToString();
					iMin++; // If iDrapeau is 1, this never gets ran, and so iMin < iMax is always going to be true
                    // And it doesn't appear you have iDrapeau = 0 anywhere else
				}
			}
		}
		else
		{
			MessageBox::Show("Entre un nombre entier maximum valide!");
		}
	}
	else
	{
		MessageBox::Show("Entre un nombre entier minimum valide!");
	}

}

 


Thank you, the problem was the iMin++ being inside the flag == 0 instead of outside. Its working now.

 

int iMin = 0, iMax = 0, i, iFlag, iReset = 0, iComma = 0;
    
    iReset++;
	if(txtEntier1->Text != "")
	{
		if(txtEntier2->Text != "")
		{
			if(iMin.TryParse(txtEntier1->Text,iMin))
			{
				if(iMax.TryParse(txtEntier2->Text,iMax))
				{
	
					if(iReset > 0)
					{
						lblNombrePremier->Text = "";
						iReset = 0;
					}
					while (iMin < iMax)
					{
						iFlag = 0;

						for(i = 2; i <= iMin/2; ++i)
						{
							if(iMin % i == 0)
							{
								iFlag = 1;
								break;
							}
						}

						if (iFlag == 0)
						{
							if(iComma == 0)
							{
								lblNombrePremier->Text += iMin.ToString();
								iComma++;
							}
							else
							{
							lblNombrePremier->Text += ", " + iMin.ToString();
							}
						}
						++iMin;
					}
					iComma = 0;
				}
				else
				{
					MessageBox::Show("Entre un nombre entier maximum valide!");
				}
			}
			else
			{
				MessageBox::Show("Entre un nombre entier minimum valide!");
			}
		}
		else
		{
			MessageBox::Show("Entre un nombre entier maximum valide!");
		}
	}
	else
	{
		MessageBox::Show("Entre un nombre entier minimum valide!");
	}

 

CPU: Intel Core i9-10900K  MOBO: ROG MAXIMUS XII FORMULA GPU: 2080ti Hall of Fame 10th anniversary limited edition  PSU: Asus ROG THOR 1200W  COOLER: Optimus foundation black acetal RADS: 3x EKWB CoolStream PE 360  LOOP: EKWB torque HDC fittings / EKWB ZMT 15,9/9,5mm / EKWB CryoFuel Clear MONITOR: Acer predator X34

Link to comment
https://linustechtips.com/topic/775506-cant-find-the-error/#findComment-9783382
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×