Help with external files.

Hello there. I can guarantee this question has been answered before, but I have spent the past two hours scouring this and many other forums trying to find a straight-forward answer and have come up with nothing.

I have a small text file which is set up like this:

85
93
18
24
83
49

I have managed to get my code to read the file and display the number of lines in that file. All I am trying to do is understand how to get my code to find the sum of those. From there I need to calculate the average, which is easy (Average=(Sum/number_of_terms)). And finally determine the highest number and the lowest number of the file.

Here is the code I have thus 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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
int main ()
{
    double number_of_lines, Average, Sum(0);
    string line;
    ifstream myfile;
    myfile.open("herpderp.txt");
    //Checking if file is open
    if (myfile.is_open())
    {cout<<"File is open"<<endl<<endl;
    //If open, displaying # of terms, calculating sum,
    //average, and the maximum and minimum values
    while (myfile.good())
      {
          getline (myfile , line);
          //Displaying Content of "herpderp.txt"
          cout<<line<<"\n"<<endl;
          //Counting number of lines
          ++number_of_lines;
          
          
      }
      cout<<"Number of terms in file is "<<number_of_lines<<endl;
    }
    else cout<<"File is not open"<<endl;
    
system("Pause");
return 0;
}


All I need to know is how do I get my code to read each line and add all the terms together to equal a variable, Sum? Please help, I am writing this code as a way of studying for my exam tomorrow.
This should get you started.


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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
int main ()
{
    double number_of_lines, Average, Sum(0);
    string line;
    ifstream myfile;
    myfile.open("herpderp.txt");
    //Checking if file is open
    if (myfile.is_open())
    {cout<<"File is open"<<endl<<endl;
    //If open, displaying # of terms, calculating sum,
    //average, and the maximum and minimum values
    int sum = 0;
    while (myfile.good())
      {
          myfile >> inputNumber;
          sum = sum + inputNumber;
          //Displaying Content of "herpderp.txt"
          cout<<line<<"\n"<<endl;
           cout << "sum is now " << sum << endl;
          //Counting number of lines
          ++number_of_lines;
          
          
      }
      cout<<"Number of terms in file is "<<number_of_lines<<endl;
    }
    else cout<<"File is not open"<<endl;
    
system("Pause");
return 0;
}
Thanks a lot! That did help quite a bit! I was able to calculate the sum, average and maximum and minimum values of the data set. However, whenever I include the loop for calculating the minimum, the output says that I have only 6 inputs and actually skips over the second input! When I take that loop out, everything works perfectly.

Here is the exact format of the .txt file I am using:

6
10
2
5
7
9
17

And here is my current code, including the Minimum loop where Minimum is initialized to 100. I know that is not the best way to go about calculating the minimum, but if I don't initialize it, the value doesn't come back correct. Like I said above, if that loop is included, the code skips a term in the data set.
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
#include <iostream>
#include <fstream>
#include <string>

using namespace std;
int main ()
{
    double number_of_lines, Average, Sum(0), sum, inputNumber, FinalSum, Maximum(0), Minimum(100), hold, x;
    string line;
    ifstream myfile;
    myfile.open("herpderp.txt");
    //Checking if file is open
    if (myfile.is_open())
    {cout<<"File is open"<<endl<<endl;
    //If open, displaying # of terms, calculating sum
    //Average, and the Maximum and Minimum values
    while (myfile.good())
      {
          myfile >> inputNumber;
          Sum = Sum + inputNumber;
          
          //Displaying Content of "herpderp.txt"
          cout<<inputNumber<<endl;
          //Counting number of lines
          ++number_of_lines;
          FinalSum=Sum;
          Average=(FinalSum/number_of_lines);
          //Determining Maximum
          
          hold=inputNumber;
          while (hold>Maximum)
          {
                Maximum=hold;
                getline(myfile, line);           
          }
          
          while (hold<Minimum)
          {
                
                Minimum=hold;
                getline(myfile, line);           
          }
          
          
      }
      cout<<endl;
      cout<<"Number of terms in file is "<<number_of_lines<<endl;
      cout<<"The sum of all terms is "<<FinalSum<<endl;
      cout<<"The average of all terms is "<<Average<<endl;
      cout<<"The highest value of the terms is "<<Maximum<<endl;
      cout<<"The lowest value of the terms is "<<Minimum<<endl;
      
    }
    else cout<<"File is not open"<<endl;
    
system("Pause");
return 0;
}

This code skips the second line in the file, 10, and continues to use the rest. It is something with the minimum loop, because without that the code works as intended.
Topic archived. No new replies allowed.