| | |
need help with programing with fstream to find area of triangle
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2004
Posts: 2
Reputation:
Solved Threads: 0
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)
#include<iostream> #include<fstream> #include<cstdlib> #include<cmath> double main() { using namespace std; ifstream in_stream; ofstream out_stream; in_stream.open("project3_1.dat"); if(in_stream.fail()) { cout<<"Input file opening failed.\n"; exit(1); } out_stream.open("result3_1.dat"); if(out_stream.fail()) { cout<<"Output file opening failed.\n"; exit(1); } double x_1,y_1,x_2,y_2,x_3,y_3,i,a,b,c,area; i=0; while (i<5) in_stream>>x_1>>y_1>>x_2>>y_2>>x_3>>y_3; 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)); if (area>0) { cout<<"The area of your triangle is\n"<<area<<endl; } else cout<<"The points you have entered lie on the same line\n"; return 0; } double triangle_area (double a, double b, double c) { double result,s,i; s= (a+b+c)/2.0; result= sqrt(s*(s-a)*(s-b)*(s-c)); i=i+1; } in_stream.;close(); out_stream;.close(); return, }
Last edited by alc6379; Nov 7th, 2004 at 5:28 pm. Reason: added [code] tags
>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:
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:
>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.
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)
//Headers int main() { // Program here }
>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)
while ( condition ) simple statement; or while ( condition ) { simple statement; simple statement; ... }
><<(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.
![]() |
Similar Threads
- easy ? regarding area of a triangle (C)
- Help with Perimete and Area of triangle (Java)
- My Book Assignment (C++)
- Need help - C++ program to determine area of triangle NOT using fstream (C++)
- [Problem] it deosn't go past the first stage?? (C++)
Other Threads in the C++ Forum
- Previous Thread: Graphics in borland!
- Next Thread: Insertion in a binary search tree
| Thread Tools | Search this Thread |
api application array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg simple sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






