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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
|
#include <iostream> //Required for cin, cout, cerr
#include <fstream> //Required for ifstream, ofstream.
#include <string> //Required for string.
using namespace std;
int main() {
int num_data_pts(0), k; // Declare and initialize objects (variables and i0 objects pointing to files).
double time, H_Hawk_given(0), H_Hawk_total(0), max_H_Hawk(0), min_H_Hawk(0), sum(0), min_H_Hawk_day(0), max_H_Hawk_day(0), S_Hawk_given(0), S_Hawk_total(0), max_S_Hawk(0), max_S_Hawk_day(0), min_S_Hawk(0), min_S_Hawk_day(0);
string filename;
ifstream birds; // read a file and use for getting data in
ofstream report; // create and write to a file
// first we will create a data file
cout << "Enter the name of the data file you want"; // Prompt user for name of input file.
cin >> filename;
report.open(filename.c_str());
cout << " Enter three values, day, # of Harris Hawks, and # of Swainson’s Hawk to put into the data file, first enter the day." << endl;
cin >> time;// get the values from keypad
cout << "Next the amount for Harris Hawks" << endl;
cin >> H_Hawk_given;
cout << "Finally the amount Swainson Hawks" << endl;
cin >> S_Hawk_given;
report << time << " " << H_Hawk_given << " " << S_Hawk_given << endl; // send values to data file
for (k = 1; k <= 8; k = k + 1) {//How many times we want it to ask
cout << " Enter three values, day, # of Harris Hawks, and # of Swainson’s Hawk to put into the data file, first enter the day." << endl;
cin >> time;// get the values from keypad
cout << "Next the amount for Harris Hawks" << endl;
cin >> H_Hawk_given;
cout << "Finally the amount Swainson Hawks" << endl;
cin >> S_Hawk_given;
}
report.close(); // close created data file
cout << "Enter the name of the data file you want to analyse"; // Prompt user for name of input file.
cin >> filename;
birds.open(filename.c_str()); // Open file and check if it exists.
if (birds.fail()) {
cerr << "Error opening input file\n";
exit(1);
}
report.open("MonaesReport.txt"); // open report file.
birds >> time >> H_Hawk_given >> S_Hawk_given; // initial input read the first data point.
while (birds >> time >> H_Hawk_given >> S_Hawk_given) // While not at the end of the file read and accumulate information
{
num_data_pts++;
H_Hawk_total += H_Hawk_given;
S_Hawk_total += S_Hawk_given;
sum = H_Hawk_given + S_Hawk_given;
if (H_Hawk_given > max_H_Hawk) {
max_H_Hawk = H_Hawk_given;//Maximum amount of Harris Hawks
max_H_Hawk_day = time;//To find the day
}
if (H_Hawk_given < min_H_Hawk) {
min_H_Hawk = H_Hawk_given;//Minimum amount of Harris Hawks
min_H_Hawk_day = time;//To find the day
H_Hawk_total += H_Hawk_given;
if (max_S_Hawk > max) {
max_S_Hawk = S_Hawk_given;
max_S_Hawk_day = time;
}
if (S_Hawk_given < min_S_Hawk) {
min_S_Hawk = S_Hawk_given;
min_S_Hawk_day = time;
}
}
birds >> time >> H_Hawk_given; // input next go back to while
birds >> time >> S_Hawk_given;
} // end while
report.setf(ios::fixed);
report.setf(ios::showpoint);
report.precision(2);
// Print summary information to report file
report << "Number of Harris Hawks:" << H_Hawk_total << endl // will the use of report output work here?
<< "Average number of Harris Hawks:" << sum / H_Hawk_total << endl;
report << "Maximum # of Harris Hawks:" << max_H_Hawk << "was on the " << max_H_Hawk_day << endl;
report << "Minimum # of Harris Hawks:" << min_H_Hawk << " was on the" << min_H_Hawk_day << endl;
cout << "Number of Harris Hawks" << H_Hawk_total << endl; // will the use of report output work here?
cout << "Average # of Harris Hawks:" << sum / num_data_pts << endl;
cout << "Maximum # of Harris Hawks:" << max_H_Hawk << "was on the " << max_H_Hawk_day << endl;
cout << "Minimum # of Harris Hawks:" << min_H_Hawk << " was on the" << min_H_Hawk_day << endl;
report << "Number of Swainson Hawks:" << S_Hawk_total << endl; // will the use of report output work here?
report << "Average number of Swainson Hawks:" << sum / S_Hawk_total << endl;
report << "Maximum # of Swainson Hawks:" << max_S_Hawk << "was on the " << max_S_Hawk_day << endl;
report << "Minimum # of Swainson Hawks:" << min_S_Hawk << "was on the " << min_S_Hawk_day << endl;
cout << "Number of Swainson Hawks" << S_Hawk_total << endl; // will the use of report output work here?
cout << "Average # of Swainson Hawks:" << sum / S_Hawk_total << endl;
cout << "Maximum # of Swainson Hawks:" << max_S_Hawk << "was on the " << max_S_Hawk_day << endl;
cout << "Minimum # of Swainson Hawks:" << min_S_Hawk << "was on the " << min_S_Hawk_day << endl;
cout << "All Hawks in total" << S_Hawk_total + H_Hawk_total << endl;
// Close file and exit program.
report.close();
birds.close();
return 0;
} //end main
|