473,378 Members | 1,721 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Distance of two points and area of two circles.

I'm a little lost on my program. Everything works fine except function 3. It gives out garbage numbers. Its suppose to give the distance between two points and then the area of 2 circles.

Expand|Select|Wrap|Line Numbers
  1.  
  2. #include <cmath>
  3. #include <string>
  4. #include <iostream>
  5. #include <fstream>
  6. #include <cstdlib>
  7. using namespace std;
  8.  
  9.  
  10.  
  11. class Coord
  12. {  friend Coord ConvPolar(float&, float&);
  13.    public:
  14.            float xval;
  15.            float yval;
  16.  
  17.    public:
  18.           Coord(float=0.0, float=0.0);
  19.           void fol_Car(float, float);
  20.           };
  21.  
  22.  Coord::Coord (float x1, float y1)
  23.  {
  24.               xval=x1;
  25.               yval=y1;
  26. }
  27. Coord ConvPolar(float& r, float& angle)
  28. {
  29.      float xx,yy;
  30. xx= r * cos(angle);
  31. yy=r * sin(angle);
  32.      Coord zz(xx,yy);
  33.      return zz;
  34.  
  35. }        
  36.  void getfunction(Coord contact[], ifstream &file)
  37. {
  38.      int i = 0; 
  39.      string line;
  40.      if (file.is_open())
  41.      {
  42.          while (! file.eof() )
  43.          {
  44.                //read  line by line
  45.               getline (file,line);  
  46.               getline (file,line);
  47.               i = i+1;
  48.          };       
  49.    for (int k = 0; k < i; k++) 
  50.          {  
  51.              cout << contact[k].xval << endl;
  52.              cout << contact[k].yval << endl;
  53.  
  54.          }
  55.      }
  56. }      
  57. ;
  58.   class PolarCoord
  59. {  friend PolarCoord ConvPolar3(float&, float&);
  60.    public:
  61.            float dist;
  62.            float theta;
  63.  
  64.    public:
  65.           PolarCoord(float=0.0, float=0.0);
  66.           void fol_Car(float, float);
  67.           };
  68.  
  69.  PolarCoord::PolarCoord (float x3, float y3)
  70.  {
  71.               dist=x3;
  72.               theta=y3;
  73. }
  74. PolarCoord ConvPolar3(float& newx, float& newy)
  75. {
  76.      float r, theta;
  77. r= sqrt( newx*newx + newy*newy);
  78. theta=atan(newy/newx);
  79.      PolarCoord jj(r,theta);
  80.      return jj;
  81.  
  82.      };
  83.  
  84.   class RecCoord
  85. {  friend RecCoord ConvPolar2(float&, float&);
  86.    public:
  87.            float xval2;
  88.            float yval2;
  89.  
  90.    public:
  91.           RecCoord(float=0.0, float=0.0);
  92.           void fol_Car(float, float);
  93.           };
  94.  
  95.  RecCoord::RecCoord (float x2, float y2)
  96.  {
  97.               xval2=x2;
  98.               yval2=y2;
  99. }
  100. RecCoord ConvPolar2(float& newr, float& newtheta)
  101. {
  102.      float x,y;
  103. x= newr * cos(newtheta);
  104. y=newr * sin(newtheta);
  105.      RecCoord kk(x,y);
  106.      return kk;
  107.  
  108.  
  109.      };
  110.  
  111.  const double PI = 2.0 * asin(1.0);
  112.  
  113. // class declaration
  114. class Point
  115. {
  116.   public:
  117.     float x;
  118.     float y;
  119.   public:
  120.     Point(float = 0.0, float = 0.0);  //constructor
  121.     float distance(Point&);
  122.  
  123. };
  124.  
  125. // implementation section
  126. Point::Point(float xval4, float yval4)
  127. {
  128.   x = xval4;
  129.   y = yval4;
  130. }
  131.  
  132. float Point::distance(Point& b)
  133. {
  134.       float dd;
  135.   dd= (sqrt(pow((x-b.x),2) + pow((y-b.y),2)));
  136.    return dd;
  137. };
  138. // class declaration for the derived Circle class
  139. class Circle : public Point
  140. {
  141.   public:
  142.     double radius; // add an additional data member
  143.   public:
  144.     Circle(float = 0.0, float = 0.0, float = 1.0);  // constructor
  145.     float distance(Circle&);
  146.     float area();
  147. };
  148.  
  149. // implementation section for Circle
  150. Circle::Circle(float centerx, float centery, float r)  // constructor
  151. {
  152.   x = centerx;
  153.   y = centery;
  154.   radius = r;
  155. }
  156.  
  157. float Circle::distance(Circle& b)
  158. {
  159.    return (Point::distance(b)); // note the base function call
  160. }
  161.  
  162. float Circle::area()   // this calculates an area
  163.  {
  164.   return (PI * pow(radius,2));
  165. };
  166.  
  167.  
  168. int main()
  169. {
  170.     string line;
  171.     float r, angle, newx, newy, yval, dis,theta, newr, newtheta;
  172.     Coord zz;
  173.     RecCoord kk;
  174.     PolarCoord jj; 
  175.     float dd;
  176.     cout<< "Function 1\n";
  177.      ifstream file("a8.txt");
  178.  
  179.   if (file.is_open())
  180.   {
  181.     while (! file.eof() )
  182.     {
  183.       getline (file,line);
  184.     cout<<" Point a is initally at \n"<< line; // displays object a's value in rectangular coordinates.
  185.      getline (file,line);
  186.      cout<<","<<line;
  187.     cout<<" \nEnter a new distance and angle for point a:";
  188.     cin>>r>>angle;
  189.     zz = ConvPolar(r, angle);  //coversion from polar to rectangular
  190.     cout<<" Point a is now located at"<< zz.xval<<","<< zz.yval<<endl; //displaying new value in rectangular coordinates. 
  191.     system("PAUSE");
  192.  
  193.  
  194.  
  195. PolarCoord c;
  196. cout<<"\n function 2";
  197. ifstream file("a8.txt");
  198.  
  199.   if (file.is_open())
  200.   {
  201.     while (! file.eof() )
  202.     {
  203.   getline (file,line);
  204.   getline (file,line) ;  
  205.   getline (file,line);
  206.    getline (file,line);
  207. cout<<"\n Rectangular point is initially located at"<<line;
  208. getline (file,line);
  209.  cout<<","<<line;
  210.  cout<<"\n Polar point b is initially located at "<<c.dist<<","<<c.theta; 
  211.  cout<< "\n Enter a new distance and angle for point b:";
  212.  cin>>newr>>newtheta;
  213.  cout<<"\n Polar point b is now located at:" <<newr<<" ," <<newtheta<<"\n";
  214.  kk = ConvPolar2(newr, newtheta);
  215.  cout<<"\n After conversion point a is now located at"<<kk.xval2<<","<<kk.yval2;
  216.  cout<<"\n Enter a new x and y value for point a:";
  217.  cin>>newx>>newy;
  218.  cout<<"\n Rectangular point a is now located at "<<newx<<","<<newy;
  219.  jj = ConvPolar3(newx, newy);
  220.  cout<<"\n After conversion, point b is now located at"<<jj.dist<<","<<jj.theta<<endl;
  221.   system("PAUSE");
  222.  
  223. Circle area;
  224. Circle distance;
  225. float x, y,r;
  226. Point b;
  227.  cout<<"\n function 3";
  228.  ifstream file("a8.txt");
  229.  
  230.   if (file.is_open())
  231.   {
  232.     while (! file.eof() )
  233.     {
  234.   getline (file,line);
  235.   getline (file,line) ;  
  236.   getline (file,line);
  237.    getline (file,line);
  238.    getline (file,line) ;  
  239.   getline (file,line);
  240.    getline (file,line);
  241.    cout<<"\nThe poinrs are "<<line;
  242.    b.x=atof(line.c_str());
  243.    getline (file,line);
  244.  cout<<", "<<line;
  245.  x=atof(line.c_str());
  246.   getline (file,line);
  247.  cout<<", "<<line;
  248.  b.y=atof(line.c_str());
  249.   getline (file,line);
  250.  cout<<", "<<line;
  251.  y=atof(line.c_str());
  252.  cout<<" The distance between these points is "<<dd<<endl;
  253.  r=b.x;
  254.  cout<< "\n The area of circle 1 is"<<area.radius;
  255.  cout<<"\n The area of circle 2 is "<<area.radius;
  256.  cout<<"\n The distance between circle centers is"<<distance.area();
  257. system("PAUSE");
  258. return EXIT_SUCCESS;  // exits the program
  259. }
  260. }
  261. }
  262. }
  263. }
  264. }
  265. }
  266.  
Nov 28 '07 #1
1 4037
Hi,

1.You haven't assigned any value to float variable 'dd' in int main().

2.The function, area.radius() accepts void and returns float. Parantheses missing. Also, assign another float variable, say, area to area.radius(). Print area.
Nov 29 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

20
by: Xenophobe | last post by:
I have successfully converted the ASP code included in the following article to PHP: http://www.4guysfromrolla.com/webtech/040100-1.shtml As described the high and low latitudes and longitudes...
7
by: csumner | last post by:
I am trying to use the haversine function to find the distance between two points on a sphere, specifically two zip codes in my database. I'm neither horribly familiar with SQL syntax nor math...
4
by: DellaCroce | last post by:
Does anyone here have the formula for calculating distance give two pairs of Longitude/Latitude coordinates? Please share this with me if you would. -- Greg
4
by: Nick | last post by:
hi, guys I don't know where should I put this post, because this is a general question, not really a c# question. The question is how to caculate the real distance between two geographical...
3
by: byteschreck | last post by:
Is it possible to change the distance from the form's border the form designer suggests when you use snaplines? I think it should be smaller.
8
by: giloosh | last post by:
how would i get the distance and angle between 2 points on the screen. For example: what is the distance between (100,50) and (250,70) what i really wanna do is see how far the current mouse...
9
by: nottheartistinquestion | last post by:
As an intellectual exercise, I've implemented an STL-esque List<and List<>::Iterator. Now, I would like a signed distance between two iterators which corresponds to their relative position in the...
1
by: ccaddiso | last post by:
I want to retrieve my x1, x2, y1, and y2 from a text file using getline, but i don't know how. const double PI = 2.0 * asin(1.0); // class declaration class Point { protected:
6
by: Tom P. | last post by:
I am writing a drawing program but I want to keep the scale down (there could end up being several hundred objects on the screen). I want to limit the points collected to a certain distance from...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.