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.

	double main()


		using namespace std;
		ifstream in_stream;
		ofstream out_stream;

			cout<<"Input file opening failed.\n";

			cout<<"Output file opening failed.\n";

		double x_1,y_1,x_2,y_2,x_3,y_3,i,a,b,c,area;
		while (i<5)

		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;

	    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));

Use 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:


int main()
  // Program here

Anything else is wrong.

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

>while (i<5)
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:

while ( condition )
  simple statement;


while ( condition ) {
  simple statement;
  simple 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.

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

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.

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.

This article has been dead for over six months. Start a new discussion instead.