In my programming class we just began using files and our first assignment deals with reading and integer from a file giving us the number of a triangle then reading three floating point numbers following the triangle number which represent the side lengths. I don't think I will have a problem figuring out ways in which to calculate and display the results but I cannot find a way in which to read integers and floating points into my program for use. The only option my professor told us about was inFile.get . The errors I'm getting seem to be telling me that I can only use this to read characters with this. I was looking up solutions to read floats and integers in from files but they all went way over my head. I want to understand what I am doing, not just copy things from the web. Thank you for any help. Here is my code so far.
//----------------------------------------\\
// 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>
usingnamespace std;
int main()
{
string inFileTriangle, outFileTriangle;
ifstream (inFile);
ofstream (outFile);
int triangleNumber;
int sideLength;
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(Output.fail( ))
{
cout << "Error opening output file"<<endl;
return 0;
}//end if
cout << "Triangle No SideA SideB SideC Perimeter Area\n";
while(!inFile.eof())
{
//reads integer number which represents triangle number
inFile.get(triangleNumber);
cout << " " << triangleNumber << " ";
//reads the next three chracters( spaces included?)
//these represent the side lengths
for(int i = 1; i < 4 ; i++)
{
inFile.get(sideLength);
}//end for
}// end while
//Things to do:
//reads the data for each triangle and finds and displays the perimeter and the area if it is a proper triangle
//displays an error message if it is not a proper triangle
//copies the data of the proper triangles and their perimeter and the area to another file
//finds maximum, minimum, and average values of the perimeters and the areas of the proper triangles
//(Please note that since we have no idea what the minimum and maximum values can be we need to initialize our variables by the limits of double variables.)
//repeats this job for as many files as user requires
return 0;
}
Alright, no parenthesis. Thank you for that one. I think I found the solution for my problem. I guess I just was not aware that you could put the file name, the >> then variables to store the information in. Thanks for the correction above.