0

Hello,
I'm in a beginners c++ class and I need to write a program that calculates the area and perimeter of a triangle of sides: a, b, and c.
The lengths of each side (values for a, b, c) are written in an input file (.txt), so the program has to calculate area and perimeter only for those values.
The lengths of each side contained in the input file are:
5 5 10
5 4 10
5 6 10
Each line is a different case. For instance, case 1 is: a=5, b=5, c=10, case 2 is: a=5, b=4, and so on.

My code so far is:

#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;

void calculations(ifstream& infile, ofstream& outfile, double& first, double& second);

int main()
{
	ifstream in_stream;
	ofstream out_stream;

	in_stream.open("infile.txt");
	if (in_stream.fail())
	{
		cout << "Input file opening failed.\n";
		exit(1);
	}
	out_stream.open("outfile.dat");
	if (out_stream.fail())
	{
		cout << "Output file opening failed.\n";
		exit(1);
	}

	double semiperimeter, area;
		
	calculations(in_stream, out_stream, semiperimeter, area);

	in_stream.close();
	out_stream.close();

	cout << "End of program.\n";
	return 0;
}
void calculations(ifstream& infile, ofstream& outfile, double& first, double& second)
{
	const int number_of_cases = 3, variables = 3;
	int i, A[number_of_cases][variables];
	double next, area, perimeter, semiperimeter;
	
	cout << "This program calculates the area and perimeter of a triangle of sides\n"
		 << "a, b, and c, for the baseline cases given in class.\n"
		 << endl;

I'm not sure if what I have so far is right, but I'm stuck in the void function because I don't know how to read the values for each case (each line) as a 2 dimensional array, and plug them into the variables a, b, c, in order to perform the following calculations:

semiperimeter = (a+b+c)/2
area = sqrt(s*(s-a)*(s-b)*(s-c))
perimeter = 2 * semiperimeter

I appreciate any help.

4
Contributors
3
Replies
5
Views
5 Years
Discussion Span
Last Post by WaltP
0

Use infile with the get method, so you could go through each line and collect the input until you reach an escape sequence.

For example :

// I presume that infile is already linked to a open file ?
char buffer = 0x00;

while (!infile.eof()) // checks if the end of the file has been reached
{
    infile.get(buffer); // reads the current character as the buffer
    
    if (buffer == '\n')
    {
        // Begin your calculation for case n here
        // Don't forget to save the values while going through the file with get()
    }
}

Edited by Dman01: bad variablename

0

1) Write functions that do one thing and one thing only. In your case:
-- 1 function to calculate and return the perimeter.
-- 1 function to calculate and return the area.
2) main() should contain a loop that
-- reads a single triangle
-- calculates the perimeter (by calling your function)
-- calculates the area
-- outputs the data

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.