Hello kseokjk,
To go along with what
seeplus said a complete program would include the input file that you are using. Posting the file allows everyone to use the same information and also to check if your reads are correct and also the writes.
In the following code I changed some variable names. I think you will find that the new names make it easier to follow in the code.
You have a really good start, but I believe a misunderstanding of how some things work.
Look over this and see if it makes more sense.
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
|
#include <iostream>
#include <iomanip>
#include <limits>
#include <string>
//#include <cmath>
#include <fstream>
using namespace std; // <--- Best not to use.
void Highest(ifstream& inFile, ofstream& outfile);
int main()
{
ifstream inFile("data.txt");
ofstream outFile("output.txt", std::ios::app);
//outFile.open("output.txt");
if (!inFile) // if file didn't open
return (cerr << "\n ERROR : cannot open file \"data.txt\".\n"), 1;
if (!outFile) // if file didn't open
return (cerr << "\n ERROR : cannot open file \"output.txt\".\n"), 2;
string data;
while (getline(inFile, data))
{
outFile << " " << data << endl << endl;
cout << " " << data << endl << endl;
}
Highest(inFile, outFile);
outFile.close(); // <--- Not required as the dtor will close the file when the function looses scope.
inFile.close();
return 0;
}
void Highest(ifstream& inFile, ofstream& outFile)
{
inFile.clear();
inFile.seekg(0);
int highest1{};
int highest2{};
for (int year{}, class1{}, class2{}, class3{};
inFile >> year >> class1 >> class2 >> class3; )
{
if (class1 > highest1)
highest1 = class1;
if (class2 > highest2)
highest2 = class2;
}
cout << endl << " The highest number for class 1 is " << highest1 << endl;
cout << " The highest number for class 2 is " << highest2 << endl;
outFile << endl << " The highest number for class 1 is " << highest1 << endl;
outFile << " The highest number for class 2 is " << highest2 << endl;
}
|
At the beginning I find the,mostly alphabetical order, helps especially when you forget a header file.
"<cmath>" is not necessary here if all you are doing is (+, -, *, / and %). "<cmath>" provides functions like "pow()", "cos()", "sin()" and others. Have a look at
http://www.cplusplus.com/reference/cmath/
As mentioned line 11 is how the forward declaration or prototype should be written.
Line 15 is considered the more proper way to create a file stream and open the file at the same time.
Lines 16 and 17 work, but you should save the ".open()" for when you close a file steam and need to reopen it. Also when you setup the "ofstream" you might want to set it up to append the file and not clear its contents when it is open. Sometimes this can help when testing the program or you may want to start with an empty file.
The if statement, line 19, is good, but you also need to check the output file stream as there is a chance that it may not have opened. Notice that lines 20 and 23 end with a (1) and (2). The idea here is that the numbers can help track down where it went wrong. In this short program this is not a big deal, but in a larger program where you may open a file stream it can help.
The rest of the code and the function should be easy enough to understand.
Without the input file I can not test the function having no idea what is being read from the file.
Andy