0

I am a beginner in C++. This is an assignment I am completing therefore I must use a the function problem1() as there is another function that I need to create to find max and min. The following text file was provided to me for the assignment.

I am having a hard time with having the program read a text file that contains a set of numbers such as the following :

[253, 676, 114]

[527, 160, 277]

[364, 193, 169]

[246, 651, 378]

[536, 479, 695]

[569, 548, 83]

my code is :

using namespace std; void problem1(string); int main() { string s= "data2.txt"; problem1(s); return 0; } void problem1(string s) { double num, sum = 0, avg; int count=0; ifstream file; file.open(s); if (file.is_open()) { while(file>>num) { sum= sum + num; count++; } avg= sum/count; cout <<"Average is " << avg << endl; file.close(); } } 

It works for text files that contine the numbers without [].

For example:

text file contains the following..

1

2

3

4

5

and finds the avg of 3.

1

1 Answer 1

2

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'; } } 
Sign up to request clarification or add additional context in comments.

3 Comments

Okay I see. Thank you so much for clearing that up that was very helpful!
Could I use the same format to find max and min?
Yes. I edited my answer. Please see the additional code.