James J.

asked • 04/05/16

create a program c++

My program doesn't open the file and it gives me some errors the name of the file is Numbers.txt
 
here the code;
 
 
 
 
 
#include <iostream>
#include <fstream>
#include <limits> // for max and min float

using std::cout;

int main ()
{
// open file
std::ifstream infile("Numbers.txt");
if ( !infile.good() )
{
cout << "Error, can't open number's file.\n";
return -1;
}

// variables initialization
int count = 0;
float num,
min = std::numeric_limits<float>::max(), // to be sure to update it
max = std::numeric_limits<float>::lowest(),
sum = 0.0,
average = 0.0;
// read all numbers till EOF or invalid input
while( infile >> num )
{
// output the numbers in the same loop
cout << num << ' ';
sum += num;
if ( num < min )
min = num;
if ( num > max )
max = num;
++count;
}

if (count > 0)
{
cout << "\nThere are " << count << " numbers in the file"
<< "\nThe sum of all number is: " << sum
<< "\nThe average of all numbers in the file is: " << sum / count
<< "\nThe lowest of all numbers is: " << min
<< "\nThe highest of all numbers is: " << max << '\n';
}
else
{
cout << "No number read from file";
}

return 0;
}

1 Expert Answer

By:

Matthew B. answered • 06/12/17

Experienced Tutor Specializing in Computer Science

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.