While Loop Problems

I have another homework assignment that is asking:

Write a program that asks the user for points in a graph.
Determine what quadrant the points are in and display an appropriate message.
Ask if the use wants to continue, and if the answer is "yes" then continue, otherwise do not. Count how many points were in each quadrant and display the count at the end. If the point is on either axis, display a message and do not count it.

a while loop must be used in this to have keep asking for points until you say no. I really am only have a problem setting up the while loop to actually work, other than that I am pretty sure I can make everything else work. i dont know anything fancy just basic stuff. Any help would be great. here is the code i have so far:


#include <iostream>
#include <string>
using namespace std;
int main()
{
int x, y, counter=1, quad1=0, quad2, quad3, quad4;
string answer, Yes, No;
cout << "Please enter the x coordinate: ";
cin >> x;
cout << "Please enter the y coordinate: ";
cin >> y;

while (answer == Yes)
{
if ((x > 0)&&(y > 0))
{
cout << "Quadrant I" << endl;
cout << "Do you want to continue (\"Yes\"" << " or \"No\"" << ")? ";
cin >> answer;
}
}
}
I don't know about this compiler but in my old one I have to use strcmp for string comparision.and using "" with string.

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
#include <iostream.h>
#include <string.h>

int main()
{
int x, y;
char answer[3];                      //why not use simple y and n
strcpy(answer,"Yes");
while (!strcmpi(answer,"Yes"))
{
cout << "Please enter the x coordinate: ";
cin >> x;
cout << "Please enter the y coordinate: ";
cin >> y;

if ((x > 0)&&(y > 0))
	cout << "Quadrant I" << endl;
else if ((x < 0)&&(y > 0))
	cout << "Quadrant II" << endl;
else if ((x < 0)&&(y < 0))
	cout << "Quadrant III" << endl;
else if ((x > 0)&&(y < 0))
	cout << "Quadrant IV" << endl;

cout << "Do you want to continue (\"Yes\" or \"No\")? ";
cin >> answer;
}
return 0;
}
Hey Akshit you forgot to count points in Quadrant

define integer,
int quadrant1=0,quadrant2=0,quadrant3=0,quadrant4=0;


if ((x > 0)&&(y > 0))
{
	cout << "Quadrant I" << endl;
        quadrant1++;
}
else if ((x < 0)&&(y > 0))
{
	cout << "Quadrant II" << endl;
        quadrant2++;
}
else if ((x < 0)&&(y < 0))
{
	cout << "Quadrant III" << endl;
        quadrant3++
}
else if ((x > 0)&&(y < 0))
{
	cout << "Quadrant IV" << endl;
        quadrant4++;
}
else if(y==0)
        cout<<"point is on X-axis"<<endl;
else if(x==0)
        cout<<"point is on Y-axis"<<endl;
Last edited on
oh no.I edited the above code.There was not even defination for quadrants other then one.So defined that only.I am not making his complete program.
sorry for misunderstood..
simple is best. Hopefully a do while loop would be accepted.

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
#include <stdio.h>
#include <iostream>
using namespace std;

int main()
{

	int x, y;
	char answer('y');

	do {
		printf("Please enter the x coordinate: \n"); //printf() is just my personal preference here  
		cin >> x;
		printf("Please enter the y coordinate: \n");
		cin >> y;

		if ((x > 0) && (y > 0))
			printf("Quadrant I\n");

		if ((x < 0) && (y > 0))
			printf("Quadrant II\n");

		if ((x < 0) && (y < 0))
			printf("Quadrant III\n");

		if ((x > 0) && (y < 0))
			printf("Quadrant IV\n");

			printf("Do you want to continue ( 'y' or 'n'? \n");
			cin >> answer;
	} while (answer == 'y' || answer == 'Y');

	return 0;
}



you could also avoid all of the if statement checks by making all of the if statements have continue statements, and then the yes or no input at the beginning of the loop (which would be a while(true) loop), when the input is taken at the beginning of the loop, an if statement could lead to a break statement when they entered not a 'y' char.

That would look something like this:

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
#include <stdio.h>
#include <iostream>
using namespace std;

int main()
{

	int x, y;
	char answer('y');

	while (true) {

		printf("Do you want me to tell you the Quadrant?('y' or 'n'\n)");
		cin >> answer;

		if (answer != 'y' && answer != 'Y')
			break;

		printf("Please enter the x coordinate: \n");
		cin >> x;
		printf("Please enter the y coordinate: \n");
		cin >> y;

		if ((x > 0) && (y > 0))
			printf("Quadrant I\n");
			continue;

		if ((x < 0) && (y > 0))
			printf("Quadrant II\n");
			continue;

		if ((x < 0) && (y < 0))
			printf("Quadrant III\n");
			continue;

		if ((x > 0) && (y < 0))
			printf("Quadrant IV\n");
			continue;
	}
	return 0;
}
Last edited on
Topic archived. No new replies allowed.