0

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;
}

Edited by Nick Evan: Fixed formatting

2
Contributors
2
Replies
3
Views
5 Years
Discussion Span
Last Post by gevorg1808
0

#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;
}

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.