I'm trying to collect some data point from a file and display and perform various calculations with them. I suppose it would help if I knew the correct way to go about storing the correct numbers in the correct variables but after hours of research, looking over my notes and pouring through my book I cannot for the life of me figure out how to do what I want to do. Here's the assignment...
A data file contains unknown number of lines of values representing triangles and each line contains;
Triangle number (integer)
Side lengths (three floating point numbers)
Write a program that
reads the data for each triangle and finds and displays the perimeters, areas and .....(other things I have to do).... of the triangles.
Here's what a data file looks like:
1 25.19 24.48 43.85 2 17.66 22.47 48.18 3 2.11 48.65
9.46 4 33.36 29.32 33.76 5 18.05 31.01 40.56 6 0.96 4.19 48.74
7 32.57 11.56 20.17 8 6.10 13.42 12.89 9 16.58 7.61 17.40 10
6.08 44.21 4.71 11 46.50 19.95 2.37 12 17.12 36.80 39.73 13 27.25 34.31 44.68 (etc...)
This is all represented on one/two line(s) in a .dat file.
The output is to look something like
Triangle No SideA SideB SideC Perimeter Area
2 21.47 12.86 14.88 49.21 93.86
3 21.24 5.96 24.75 51.95 54.91
4 35.32 12.18 39.25 86.75 212.03
But my program, I'm sure because I'm reading in my data incorrectly is spewing out this little number.
1 25.1924.4843.85 217.6622.47 48.1832.11
48 0.659.464 33.3629.3233.76 518.0531.01
40 0.5660.96 4.1948.747 32.5711.5620.17
8 6.113.4212.89 916.587.61 17.4106.08
44 0.214.7111 46.519.952.37 1217.1236.8
39 0.731327.25 34.3144.6814 2.7415.182.31
15 9.7736.0136.09 1643.8929.12 3.531746.14
40 0.0214.318 27.1849.2435.78 1941.9521.66
23 0.532028.04 13.4537.4521 25.1932.3415.39
22 6.9423.7818.12 2339.4139.01 33.43246.68
1 0.0827.9925 15.0446.9749.05 2614.3340.04
44 0.812729.88 44.247.1928 27.4636.4228.84
I can see my data in there but all messed up. I'm thinking it has to do with the decimal points?
Here is my code so far...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
|
//----------------------------------------\\
// Purpose: To determine if data given by
// a file creates real triangles and if so
// to then calculate data sbout them.
// Name: Will Beeler
// Date:October 28, 2009
//----------------------------------------\\
#include <iostream>
#include <iomanip>
#include <cctype>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string inFileTriangle, outFileTriangle;
ifstream inFile;
ofstream outFile;
int triangleNumber;
double sideLength1, sideLength2, sideLength3;
cout << "Enter the name of the input file: ";
cin >> inFileTriangle;
inFile.open(inFileTriangle.c_str());
cout << "Enter the name of the output file: ";
cin >> outFileTriangle;
outFile.open(outFileTriangle.c_str());
if( !inFile.good() )
{
cout << "Error opening input file" << endl;
return 0;
}//end if
if( !outFile.good() )
{
cout << "Error opening output file"<<endl;
return 0;
}//end if
cout << "Triangle No SideA SideB SideC Perimeter Area\n";
while(!inFile.eof())
{
inFile >> triangleNumber ;
cout << endl << " " << triangleNumber << " ";
outFile << endl << " " << triangleNumber << " ";
for(int i = 1; i < 4 ; i++)
{
inFile >> sideLength1;
inFile >> sideLength2;
inFile >> sideLength3;
outFile << setw(9) << sideLength1 << sideLength2 << sideLength3;
cout << setw(9) << sideLength1 << sideLength2 << sideLength3;
}//end for
}// end while
return 0;
}
|
Any help greatly appreciated. Thank you.