c++ save me i need assistance

May 11, 2010 at 11:47pm
im very new to c++ and having a hard time doing this question if anyone could help me with me that be awesome!

Write a complete C++ program that prompts the user for the x and y coordinates of two Cartesian points, then calculates and displays the distance between them. Assume that the coordinate values are whole numbers. The distance between two points having the coordinates (x1, y1) and (x2, y2) is distance = the square root of ([x1 - x2]2 + [y1 - y2]2 ).
May 12, 2010 at 6:03am
#include<iostream.h>
#include<conio.h>
#include<math.h> // sqrt()

struct coordinates // structure for coordinates
{
int x;
int y;
}point1, point2; // two points x1,y1 & x2,y2


void read(coordinates &pt) // function to read values
{
cout<<"Enter the value for x coordinate";
cin>>pt.x;
cout<<"Enter the value for y coordinate";
cin>>pt.y;
}



void main()
{
clrscr();
float distance;
read(point1);
read(point2);
distance=sqrt(((x1 - x2)*(x1 - x2)) + ((y1 - y2)*(y1 - y2)));
cout<<" Distance between the two coordinates is"<<distance;
getch();
}

Good Luck
May 12, 2010 at 10:43am
thank u so much!! i had somewhat like that but i didnt add a void or a float distance no wonder lol thanks so muccccccccch!
May 12, 2010 at 1:49pm
void main()??? You should probably look this gift horse in the mouth if you want to get a good grade.
May 12, 2010 at 2:08pm
is that inncorrect? cause i never used a void main its usually a

int main {
}
May 12, 2010 at 3:07pm
so what is the correct answer?...i just tried mine and it won't work it keep saying i have all these errors but i cant pin point what is wrong :(
May 12, 2010 at 3:09pm
int main() ALWAYS int main(), the reason for this is so that the command shell gets a pass\fail result from the application when it is done executing. void main() returns nothing to what called it.
May 12, 2010 at 3:22pm
so why when i try to complie my file it doesnt work and it keep showing errors?
May 12, 2010 at 3:36pm
You do realize that we can't see you right? What are the errors?
May 12, 2010 at 3:47pm
lol i no aha..i fixed it i just figured it out its one of those adding an extra bracket somewhere
May 12, 2010 at 3:56pm
Yeah those will always get you from time to time. It's good that you were able to spot and fix it on your own.
May 12, 2010 at 4:33pm
lol why thank you i thought i give a highfive...this is my first time ever doing C++ so some may think its so0o easy and like me may take awhile but you learn something everyday which is good :)..it was just one of those things i kept starring at and missed but its cool :)
May 12, 2010 at 7:21pm
void main() does actually return a value to the command shell but it returns an indeterminate garbage value which is actually worse than nothing since it may fool the shell into thinking that the code failed when it really didn't. If code you write returns random errors once or twice every 10,000 times it runs you will have some very tricky debugging to do.

C and C++ standards both explicitly prohibit void main(), ignore them at your own risk.
May 12, 2010 at 7:42pm
GCC will actually refuse to compile something if it has void main(). Use int main() or int main(int argc, char * argv[]), but main must return an int.

Sorry.

-Albatross
May 12, 2010 at 10:05pm
ok thanks everyone :) great fed back ya i figured out what where i went wrong and now my code works hurray :)
Topic archived. No new replies allowed.