Getting proper calculations to work?

I am trying to have my program calculate the min, max, total, average, etc, of Hawks. But it keeps giving strange results like 0 and inf. What am I doing wrong?
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
#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 
Last edited on
That code shouldn't even compile in the first place. What compiler or IDE are you using? You should also use an IDE that helps you indent properly, because your code is hard to read without proper indentation.

The most flagrant issue I see is that on line 67 you are using something called 'max'. This is actually being interpreted as the name of the function std::max, and this should be a compiler error.
 In function 'int main()':
67:18: error: invalid operands of types 'double' and '<unresolved overloaded function type>' to binary 'operator>' 


Other issues:

1
2
3
4
5
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;
This serves no purpose since you overwrite these values once you parse the file.

1
2
birds >> time >> H_Hawk_given >> S_Hawk_given; // initial input read the first data point.
  while (birds >> time >> H_Hawk_given >> S_Hawk_given) 

You are discarding the first time/H_Hawk_given/S_Hawk given in your dataset.
Last edited on
As re-formatted for readability:

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 


L74 - max is not defined
L19 - 39 - this only creates a file with one entry from L28. The data entered L31-38 is not saved.

Last edited on
As a possible first refactor, consider:

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
#include <iostream>
#include <fstream>
#include <string>

constexpr unsigned NoValues {8};

int main() {
    std::string filename;

    std::cout << "Enter the name of the data file to create: ";
    std::getline(std::cin, filename);

    std::ofstream report(filename);

    if (!report)
        return (std::cerr << "Cannot open file\n"), 1;

    for (unsigned k {}; k < NoValues; ++k) {
        double time {}, H_Hawk_given {}, S_Hawk_given {};

        std::cout << "Enter three values - day, # of Harris Hawks, # of Swainson’s Hawk to: ";
        std::cin >> time >> H_Hawk_given >> S_Hawk_given;

        report << time << " " << H_Hawk_given << " " << S_Hawk_given << '\n';
    }

    report.close();
    std::cin.ignore();

    std::cout << "Enter the name of the data file you want to analyse: ";
    std::getline(std::cin, filename);

    std::ifstream birds(filename);

    if (!birds)
        return (std::cerr << "Error opening data file\n"), 2;

    report.open("MonaesReport.txt");

    if (!report)
        return (std::cerr << "Cannot open report file\n"), 3;

    unsigned num_data_pts {};
    double H_Hawk_total {}, max_H_Hawk {}, min_H_Hawk {1000}, min_H_Hawk_day {}, max_H_Hawk_day {};
    double S_Hawk_total {}, max_S_Hawk {}, min_S_Hawk {1000}, max_S_Hawk_day {}, min_S_Hawk_day {};

    for (double time {}, H_Hawk_given {}, S_Hawk_given {}; birds >> time >> H_Hawk_given >> S_Hawk_given; ++num_data_pts) {
        H_Hawk_total += H_Hawk_given;
        S_Hawk_total += S_Hawk_given;

        if (H_Hawk_given > max_H_Hawk) {
            max_H_Hawk = H_Hawk_given;
            max_H_Hawk_day = time;
        }

        if (H_Hawk_given < min_H_Hawk) {
            min_H_Hawk = H_Hawk_given;
            min_H_Hawk_day = time;
        }

        if (S_Hawk_given > max_S_Hawk) {
            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;
        }
    }

    report.setf(std::ios::fixed);
    report.setf(std::ios::showpoint);
    report.precision(2);

    report << "Number of Harris Hawks: " << H_Hawk_total << '\n';
    report << "Average number of Harris Hawks: " << H_Hawk_total / num_data_pts << '\n';
    report << "Maximum # of Harris Hawks: " << max_H_Hawk << " was on the " << max_H_Hawk_day << '\n';
    report << "Minimum # of Harris Hawks: " << min_H_Hawk << " was on the " << min_H_Hawk_day << '\n';

    std::cout << "Number of Harris Hawks: " << H_Hawk_total << '\n';
    std::cout << "Average # of Harris Hawks: " << H_Hawk_total / num_data_pts << '\n';
    std::cout << "Maximum # of Harris Hawks: " << max_H_Hawk << " was on the " << max_H_Hawk_day << '\n';
    std::cout << "Minimum # of Harris Hawks: " << min_H_Hawk << " was on the " << min_H_Hawk_day << '\n';

    report << "Number of Swainson Hawks: " << S_Hawk_total << '\n';
    report << "Average number of Swainson Hawks: " << S_Hawk_total / num_data_pts << '\n';
    report << "Maximum # of Swainson Hawks: " << max_S_Hawk << " was on the " << max_S_Hawk_day << '\n';
    report << "Minimum # of Swainson Hawks: " << min_S_Hawk << " was on the " << min_S_Hawk_day << '\n';

    std::cout << "Number of Swainson Hawks: " << S_Hawk_total << '\n';
    std::cout << "Average # of Swainson Hawks: " << S_Hawk_total / num_data_pts << '\n';
    std::cout << "Maximum # of Swainson Hawks: " << max_S_Hawk << " was on the " << max_S_Hawk_day << '\n';
    std::cout << "Minimum # of Swainson Hawks: " << min_S_Hawk << " was on the " << min_S_Hawk_day << '\n';
    std::cout << "All Hawks in total: " << S_Hawk_total + H_Hawk_total << '\n';
}

Last edited on
Topic archived. No new replies allowed.