473,770 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Distance of two points and area of two circles.

6 New Member
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 4064
sksriharsha
31 New Member
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
3904
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 are pre-calculated and passed to the query. SELECT * FROM Locations
7
11025
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 equations :), so I was hoping I could get some help. Below is what I'm using and it is, as best as I can figure, the correct formula. It is not however, giving me correct results. Some are close, others don't seem right at all. Any ideas? SET...
4
17437
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
3858
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 point? For example, i know two positions from my gps, longitude and latitude. how to know to caculate how many miles between these two points. There are many websites doing this in search, enter the miles as radius, and only show the results fall in...
3
2240
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
17827
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 position is from a given object and what the angle is between them. Any help please? Thanks, Gil
9
2324
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 list. For instance, if I did something like distance(list.end(), list.begin()), I would get -list.size(). The STL's iterator distance function amounts to something like this: distance_type distance(Iterator first, Iterator last) {...
1
1615
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
1694
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 other points already collected, in other words, if you're drawing a line it will only record points on the line every 6 pixels. How do I determine how far away one point is from another? if they are restricted to straight lines that's fine but as...
0
9618
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10101
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8933
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7456
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6710
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5482
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4007
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3609
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2849
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.