need help with programing with fstream to find area of triangle

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2004
Posts: 2
Reputation: knucklehead is an unknown quantity at this point 
Solved Threads: 0
knucklehead knucklehead is offline Offline
Newbie Poster

need help with programing with fstream to find area of triangle

 
-1
  #1
Nov 7th, 2004
I'm having trouble with a program to calculate a triangles area using fstream. The problem is I need the program to open the function to calculate the area in a file ("project3_1.dat") and to read in another file with coordinates of triangles. I wrote what I could in my program but now I'm stuck. Any help could do. Thanks.

  1. #include<iostream>
  2. #include<fstream>
  3. #include<cstdlib>
  4. #include<cmath>
  5. double main()
  6.  
  7. {
  8.  
  9. using namespace std;
  10. ifstream in_stream;
  11. ofstream out_stream;
  12.  
  13.  
  14.  
  15. in_stream.open("project3_1.dat");
  16. if(in_stream.fail())
  17. {
  18. cout<<"Input file opening failed.\n";
  19. exit(1);
  20. }
  21.  
  22. out_stream.open("result3_1.dat");
  23. if(out_stream.fail())
  24. {
  25. cout<<"Output file opening failed.\n";
  26. exit(1);
  27. }
  28.  
  29. double x_1,y_1,x_2,y_2,x_3,y_3,i,a,b,c,area;
  30. i=0;
  31. while (i<5)
  32. in_stream>>x_1>>y_1>>x_2>>y_2>>x_3>>y_3;
  33.  
  34. out_stream<<"The area of the points in project3_1.dat is\n"
  35. <<(a= sqrt(pow(x_1-x_2,2)+pow(y_1-y_2,2)));
  36. b= sqrt(pow(x_2-x_3,2)+pow(y_2-y_3,2));
  37. c= sqrt(pow(x_3-x_1,2)+pow(y_3-y_1,2));
  38.  
  39.  
  40. if (area>0)
  41. { cout<<"The area of your triangle is\n"<<area<<endl;
  42.  
  43. }
  44. else
  45. cout<<"The points you have entered lie on the same line\n";
  46. return 0;
  47. }
  48. double triangle_area (double a, double b, double c)
  49. {
  50. double result,s,i;
  51. s= (a+b+c)/2.0;
  52. result= sqrt(s*(s-a)*(s-b)*(s-c));
  53. i=i+1;
  54. }
  55. in_stream.;close();
  56. out_stream;.close();
  57.  
  58. return,
  59. }
Last edited by alc6379; Nov 7th, 2004 at 5:28 pm. Reason: added [code] tags
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,775
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 743
Team Colleague
Narue's Avatar
Narue Narue is online now Online
Code Goddess

Re: need help with programing with fstream to find area of triangle

 
0
  #2
Nov 7th, 2004
>I wrote what I could in my program but now I'm stuck.
You don't have any problems that a good book on C++ and a compiler wouldn't solve.

>double main()
I don't see this often. Usually the return type is either void or not present. I'll say this once, very clearly for you:
  1. //Headers
  2.  
  3. int main()
  4. {
  5. // Program here
  6. }
Anything else is wrong.

>exit(1);
1 isn't a portable return value. You've already included <cstdlib>, so you might as well use EXIT_FAILURE.

>i=0;
>while (i<5)
>in_stream>>x_1>>y_1>>x_2>>y_2>>x_3>>y_3;
This is suspicious. What it does is read these values until one of the attempts fails. If you have several lines of points then you will only process the last of them. When a loop has a single statement you can omit braces, otherwise you need a compound statement:
  1. while ( condition )
  2. simple statement;
  3.  
  4. or
  5.  
  6. while ( condition ) {
  7. simple statement;
  8. simple statement;
  9. ...
  10. }
>out_stream<<"The area of the points in project3_1.dat is\n"
><<(a= sqrt(pow(x_1-x_2,2)+pow(y_1-y_2,2)));
>b= sqrt(pow(x_2-x_3,2)+pow(y_2-y_3,2));
>c= sqrt(pow(x_3-x_1,2)+pow(y_3-y_1,2));
You realize you're only printing a, yes?

>if (area>0)
area might be greater than 0, but it might not. At this point you haven't given area a value, so this test introduces undefined behavior.

>double triangle_area (double a, double b, double c)
Function definitions cannot be nested inside of other function definitions.

>i=i+1;
Not only does this serve no purpose at all, i is uninitialized, so yet again you have undefined behavior.

>in_stream.;close();
>out_stream;.close();
I have no suggestions for how this could have happened. There's no ;. or .; operator, and if you had bothered to read a book on C++ before whipping out Notepad and typing gibberish, you would have known that.

>return,
Semicolons end a statement, but even then this would be illegal because you defined main to return double and you say you're returning nothing. The three standard return values for main are all int: 0, EXIT_SUCCESS, and EXIT_FAILURE. 0 and EXIT_SUCCESS mean the same thing.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC