Saturday, May 9, 2009

C++ help?!?!?

Well, i have another c++ issue. I have this program completly written out and it should work but for some reason it says x and y are undefined? Please help!?





#include%26lt;iostream%26gt;





using namespace std;





struct point


{


int x;


int y;


};





void greeting();


//Displays greeting.





int main()


{


point p1, p2, p3;





greeting();


cout %26lt;%26lt; "Please enter coordinates for point one: ";


cin %26gt;%26gt; x.p2 %26gt;%26gt; y.p2;


cout %26lt;%26lt; "Please enter coordinates for point two: ";


cin %26gt;%26gt; x.p2 %26gt;%26gt; y.p2;


x.p3 = x.p1 + x.p2;


y.p3 = y.p1 + y.p2;


cout %26lt;%26lt; "The coordinates of point three are: " %26lt;%26lt; x.p3 %26lt;%26lt; y.p3;





system("pause");


return 0;


}





void greeting()


{


cout %26lt;%26lt; "This program will take in two points the user enters and compute a "


%26lt;%26lt; "third point.\n\n";


}

C++ help?!?!?
I think you're accessing the structure members backwards. You allocated 3 structures (p1-p3). When you need to access a structure member, the structure member goes on the right side of the '.' character and the name of the structure goes on the left side.





Example:


p3.y=p1.y + p2.y;
Reply:X and Y must be set to a value. Try setting them both to zero at the start.





int x =0;


int y =0'
Reply:replace x.p2 by p2.x and so on


No comments:

Post a Comment