If you look at one line of the input file, then you will see the following. It consists of
- opening square bracket
- first number
- comma
- second number
- comma
- third number
- closing bracket
If you want to read the line and all lines in a file, then you must read everything. For the character that you are not interested in, you could use a char temp temporary variable and read those characters into it, like so:
fileStream >> temp >> first >> temp >> second >> temp >> third >> temp;
Of course you can use also more "speaking" and separate variables like so:
fileStream >> openingBracket >> first >> comma1 >> second >> comma2 >> third >> closingBracket;
Then you could also validate in an if statement, if the variables contain the expected values:
if (openingBracket == '[' and comma1 == ',' and comma2 ==',' and closingBracket==']') ....
But forget this at the moment.
So, now we know how to read a line. If we want to read all lines, we put the above statement in a while. This will then run, until it hits an End-Of-File or some other error occurs.
In the while loop body, you can do your calculations. And after the while loop body, you can output the resulting average as a double value.
Please note:
- The constructor of the
std::ifstream opens the file for you automatically. The destructor will close it automatically - The bool operator of the streams is overwritten. It returns, if the state is ok, or if one of the status bits is set. Therefore
if (fileStream) is sufficient for a check. - the extractor operator
>> returns a reference to the stream. So, if you write fileStream >> openingBracket then the result will be "fileStream", this will be used for the next extraction operation fileStream >> first and so on and so on. At the end, you will have something similar like while(fileStream). And this will run until "eof" or other failure bits are set. (See bool operator above).
Your whole program could then look like this:
#include <iostream> #include <fstream> #include <string> void problem1(std::string fileName) { // Open the file std::ifstream fileStream{ fileName }; // Check, if everything is ok with the stream if (fileStream) { // These are dummy variables char openingBracket{}, comma1{}, comma2{}, closingBracket{}; // The values in one line int first{}, second{}, third{}; // For summing up and claculating the average int sum{}, counter{}; // Read data, as long as there are any while (fileStream >> openingBracket >> first >> comma1 >> second >> comma2 >> third >> closingBracket) { // Do the necessary calculations sum += first + second + third; counter += 3; } // Output avaerage value as double std::cout << "Average is " << static_cast<double>(sum) / static_cast<double>(counter) << '\n'; } } int main() { std::string s = "data2.txt"; problem1(s); return 0; }
Edit:
Searching for max and min:
#include <iostream> #include <fstream> #include <string> #include <limits> void problem1(std::string fileName) { // Open the file std::ifstream fileStream{ fileName }; // We want to know the min max value int max = std::numeric_limits<int>::min(); int min = std::numeric_limits<int>::max(); // Check, if everything is ok with the stream if (fileStream) { // These are dummy variables char openingBracket{}, comma1{}, comma2{}, closingBracket{}; // The values in one line int first{}, second{}, third{}; // For summing up and claculating the average int sum{}, counter{}; // Read data, as long as there are any while (fileStream >> openingBracket >> first >> comma1 >> second >> comma2 >> third >> closingBracket) { // Do the necessary calculations sum += first + second + third; counter += 3; if (first > max) max = first; if (second > max) max = second; if (third > max) max = third; if (first < min) min = first; if (second < min) min = second; if (third < min) min = third; } // Output average value as double std::cout << "Average is " << static_cast<double>(sum) / static_cast<double>(counter) << "\nMax = " << max << "\nMin = " << min << '\n'; } }