the question asked me make a program that would find opens an read a txt file and calculate or display the following.
- the number of numbers
- sum of all the numbers in the file (running total)
-average of numbers in file

the .txt file just has 100,200,300,400,500,600,700 each in its own line one after another.

im completly stuck on how to find the second two parts. so far i managed to read the file and get it to show the numbers and give a final count of how many there are using a while loop. but everything ive tried didnt work. A point in the right direction would be great thanks in advance.

heres my program so far.

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream inputfile;
int number, sum = 0, average, c = 0,rt=0;
inputfile.open("ListOfNumbers.txt");
while(inputfile >> number)
{
cout <<number<<endl;



c++;
}


cout << "Number of numbers is " << c << endl;
cout << "total is " << rt << endl;
inputfile.close();



return 0;
}

added rt+=number and it worked

#include <iostream>
#include <fstream>
using namespace std;
int main()
{
ifstream inputfile;
int number, sum = 0, average, c = 0,rt=0;
inputfile.open("ListOfNumbers.txt");
while(inputfile >> number)
{
cout <<number<<endl;
rt=rt+number;

c++;
}

cout << "Number of numbers is " << c << endl;
cout << "total is " << rt << endl;
inputfile.close();


return 0;
}

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.