944,183 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4300
  • C++ RSS
Nov 7th, 2004
-1

need help with programing with fstream to find area of triangle

Expand Post »
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.

C++ Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
knucklehead is offline Offline
2 posts
since Nov 2004
Nov 7th, 2004
0

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

>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:
C++ Syntax (Toggle Plain Text)
  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:
C++ Syntax (Toggle Plain Text)
  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.
Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Graphics in borland!
Next Thread in C++ Forum Timeline: Insertion in a binary search tree





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC