Help with While Loops Please?

I am writing a graphing program and I have the basic concept down but still having problems. If someone could help that would be great, I dont know "char" functions or "strcpy" functions. Look at the code I have and you should be able to tell what I know. Thank You.

#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: "; //All of these work fine
cin >> x;
cout << "Please enter the y coordinate: ";
cin >> y;

//When the program is run it asks for the coordinates and if you want to continue but wont display
//the sum of the equation
while (answer == Yes) //Thought this what I thought would work
if ((x > 0)&&(y > 0))
{
cout << "Quadrant I" << endl;
cout << "Do you want to continue (\"Yes\"" << " or \"No\"" << ")? ";
cin >> answer; //Not sure this is where it should go.
cout << endl;
}
else
{
quad1 = quad1 + counter; //This seems to only equal 0
cout << "There are " << quad1 << " point(s) in quadrant 1" << endl;
}
while (answer == Yes) //Thought this what I thought would work


I see that answer is a string object that you never set the value of. It is in effect a blank string.

I think you meant something like the following, although I have to guess that you're trying to count all the points in Quadrant 1.

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
#include <iostream>
#include <string>
using namespace std;
int main()
{
  int x, y, quad1=0;
  string answer("Yes"); // set string to "Yes" at the start

  while (answer == "Yes") //Thought this what I thought would work
  {

   cout << "Please enter the x coordinate: "; //All of these work fine
    cin >> x;
    cout << "Please enter the y coordinate: ";
    cin >> y;

     if ((x > 0)&&(y > 0))
    {
       quad1++;
       cout << "Quadrant I" << endl;
    }
    cout << "Do you want to continue (\"Yes\"" << " or \"No\"" << ")? ";
     cin >> answer; 
  }
  cout << "There are " << quad1 << " point(s) in quadrant 1" << endl;  
}



Last edited on
Topic archived. No new replies allowed.