#include <iostream>
#include <fstream>
#include <cstdlib>
int main()
{
constchar *name = "TopicD.cpp";
std::ifstream input_file( name );
if ( !input_file )
{
std::cerr << "Error: file " << name << " can not be opened" << std::endl;
std::exit( 1 );
}
int sum = 0;
int count = 0;
int number;
while ( input_file >> number ) sum += number, ++count;
if ( count != 0 )
{
std::cout << "There are " << count << " numbers in the file" << std::endl;
std::cout << "The sum of the numbers is " << sum << std::endl;
std::cout << "The average of the numbers is " << sum / count << std::endl;
}
return 0;
}
does the file that im opening have to be in a specific place?
or how does that work in this program? and the file name that im trying to open is called random.txt
@booradley60
For median and mode I would agree with you, but the average could still have fractional data even if all the inputs are integers.
I do not argue because I do not see the subject of the discussion. I only see that you have some problems with integers. It is your problems not main. You personally can use a floating type but I prefered to use the integer type for the average of a sequence of integers.
I only see that you have some problems with integers.
My problem with integers is that in many cases they cannot faithfully represent the actual value of the average of a sequence of integers.
It is your problems not main.
It is a problem to anyone who takes the word 'average' in this case to imply 'arithmetic mean'.
@tycastill0
You can try specifying the entire path to your file. Otherwise, the executable will look in certain directories depending on your environment.