-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsumafile.cpp
More file actions
60 lines (53 loc) · 1.32 KB
/
Copy pathsumafile.cpp
File metadata and controls
60 lines (53 loc) · 1.32 KB
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
59
60
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
const int SIZE = 70;
int main(int argc, char *argv[])
{
char filename[SIZE];
ifstream inFile;
std::cout << "enter name of data file" << std::endl;
cin.getline(filename, SIZE);
inFile.open(filename);
if(!inFile.is_open())
{
std::cout << "Could not open the file " << filename << std::endl;
std::cout << "Program terminating." << std::endl;
exit(EXIT_FAILURE);
}
double value;
double sum = 0.0;
int count = 0;
inFile >> value;
while(inFile.good())
{
++count;
sum += value;
inFile >> value;
}
if(inFile.eof())
{
std::cout << "End of file reached." << std::endl;
}
else if(inFile.fail())
{
std::cout << "Input terminated by data mismatch." << std::endl;
}
else
{
std::cout << "Input terminalted for unkown reason." << std::endl;
}
if(count == 0)
{
std::cout << "No data processed." << std::endl;
}
else
{
std::cout << "Item read:" << count << std::endl;
std::cout << "Sum: "<< sum << std::endl;
std::cout << "Average: " << sum/count << std::endl;
}
inFile.close();
return 0;
}