bool isSame=false;
do
{
	bool isSame=false;
	for (int i=1; i<=corners; i++)
	{
		cout << "Please enter the cordinates for corner " << i << ':';
		cin >> x[(i-1)] >> y[(i-1)];
	}
	int k;
	for (k=0;k<(corners-1);k++)
	{
		int j;
		for(j=(k+1);j<corners;j++)
		{
			if ((x[k]==x[j])&&(y[k]==y[j]))
			{
				isSame=true;
			}
		}
	}
	if (isSame==true)
	{
		cout << "One or more of the coordinates you have entered are the same."<<endl
			<<"Please enter different coordinates."<<endl;
		pause();
		system("cls");
	}
}while (isSame==true);

Above is code I have written that allows a user to enter some coordinates and then checks to ensure they are not the same. The issue I am having is that even though at the start isSame=true when debugging the value is shown as false .

I debugged with stop points at every point where isSame is defined. At the first two outright declarations of false , the debugger is showing true . When it gets to isSame=true it becomes false . Then it reverts back to true and executes the cout statement. Finally it returns to false and exits the do-while loop.

I have a feeling I am missing something very obvious but I can't seem to find it.

Recommended Answers

All 2 Replies

bool isSame=false;
do
{
  bool isSame=false;
  for (int i=1; i<=corners; i++)

  //...

}while (isSame==true);

This section of code is a big no-no. The "isSame" on Line 1 is not the same "isSame" as the one on Line 4. Your do-while loop defines a program scope that is separate from the function that it is part of. As a result, when you re-declare "isSame" on Line 4, you are actually masking it and hiding it from proper access with a different version of the variable stored in a different area of memory.

You should remove the "bool" part of Line 4 to change it from a declaration/initialization into an assignment.

Ah thanks! Have changed the code and it works perfectly now! I knew I was doing something silly, just couldn't spot it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.