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.