Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <fstream>
- using namespace std;
- int main() {
- int numValue = 0;
- double temp = 0;
- double sum = 0;
- double average = 0;
- cout << fixed << setprecision(2);
- ifstream infile;
- infile.open("input.txt");
- if (infile) {
- infile >> numValue;
- if (numValue != 0) {
- for (int i = 0; i < numValue; i++) {
- infile >> temp;
- sum += temp;
- }
- average = (sum / temp);
- cout << "The average of these numbers are " << average << endl;
- }
- else {
- cout << "Error! Cannot compute the average if there are no values." << endl;
- }
- }
- else {
- cout << "Error! Unable to open the file." << endl;
- }
- infile.close();
- cin.ignore();
- cin.get();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement