So I have to write a program to output whether a point is inside or outside a circle, but I can only get the program to ask me for one set of coordinates.

#include <iostream>
#include <cmath>
#include <string>
using namespace std;
bool promptYesNo (string question);
int main ()
{
	double x, y, r, d, px, py, dx, dy, da;
	char Yes, No;
	string promptYesNo;
	cout << "Please enter the x and y coordinates of the center of the circle" << endl;
	cin >> x >> y;
	cout << "Please enter the radius of the circle" << endl;
	cin >> r;

	{cout << "Please enter the x and y coordinates of a point" << endl;
		cin >> px >> py;
		dx = pow (px-x, 2);
		dy = pow (py-y,2);
		da = dx + dy;
		d = sqrt (da);
		
		
	
		if (d <= r)
		cout << "Inside" << endl;
		else if (d > r)
		cout << "Outside" << endl;
	
	}
	cout << "Enter another point? <Yes or No>" << endl;
if(cin>> Yes)
{
		cout << "Please enter the x and y coordinates of a point" << endl;
		cin >> px >> py;
		dx = pow (px-x, 2);
		dy = pow (py-y,2);
		da = dx + dy;
		d = sqrt (da);
		
		
	
		if (d <= r)
		cout << "Inside" << endl;
		else if (d > r)
		cout << "Outside" << endl;
} 


return 0;
}

Any help would be appreciated. Thanks.

Recommended Answers

All 4 Replies

You are trying some creative coding there. You need to make Yes a string and then compare it to "yes." For example,

cout <<"Enter another pt? ";
cin >>Yes;
if(Yes == "yes" || Yes == "Yes") //and any other variants you want 
{
   //do something
}

The fact that your variable is called Yes has nothing to do with what it contains.

Note that you might want a do/while loop around your prompts so you can ask for points over and over (and you wouldn't have to repeat all the code for your questions).

ok, so I updated my code and this is what I have now

#include <iostream>
#include <cmath>
#include <string>
using namespace std;
bool promptYesNo (string question);
int main ()
{
	double x, y, r, d, px, py, dx, dy, da;
	char Yes, No;
	string promptYesNo;
	cout << "Please enter the x and y coordinates of the center of the circle" << endl;
	cin >> x >> y;
	cout << "Please enter the radius of the circle" << endl;
	cin >> r;

	do
	{cout << "Please enter the x and y coordinates of a point" << endl;
		cin >> px >> py;
		dx = pow (px-x, 2);
		dy = pow (py-y,2);
		da = dx + dy;
		d = sqrt (da);
		
		
	
		if (d <= r)
		cout << "Inside" << endl;
		else if (d > r)
		cout << "Outside" << endl;
	
	cout << "Enter another point? <Yes or No>" << endl;
	cin >> Yes;
	} while (cin >>Yes);


return 0;
}

After saying "Yes," it says "Enter the coordinates of a point" but then immediately says "Inside" and then says "Exit Program."

For clarification, which brackets should I put the do/while loop outside of?

while (cin >>Yes); is not doing what you think it is doing. You've already asked for input into Yes within the loop on line 32. This will prompt you for input again but regardless of what you put any input will cause the loop to continue because cin returns the equivalent of true (in reality an object of type istream whenever something is successfully converted vs. a null value if nothing remains) thereby driving your loop for one more cycle. You also haven't changed the type of Yes to a string. Once you've done that, having your while condition be while(Yes =="yes"); will do the trick. To make sure you catch all the variants,Yes, YeS, etc, consider converting (the variable) Yes to all lower case.

Thank you for the help.

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.