Cin function wont work?

I am trying to get my cin function to work with this program, and it was working before, but now after some changes it is giving me an error for using the operator ">>" which I am pretty sure is correct when using cin. (Line 13 is where the issue is.)

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
120
121
122
123
#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);
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" << endl;
cin >> time >> H_Hawk_given >> S_Hawk_given; // get the values from keypad
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" << endl;
cin >> time >> H_Hawk_given >> S_Hawk_given; 
report << time << " " << H_Hawk_given << "  " << S_Hawk_given << endl; 
}
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.eof()) // While not at the end of the file read and accumulate information
{
num_data_pts++;
  H_Hawk_total += H_Hawk_given;

if (num_data_pts == 1)
{
max_H_Hawk = min_H_Hawk = H_Hawk_given;
}
sum += H_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
}


 
report << " The amount of Harris Hawks is " << " " << H_Hawk_total<< endl;//out to report
cout << " The amount of Harris Hawks is" << " " << H_Hawk_total << endl;//echo to screen
birds >> time >> H_Hawk_given; // input next go back to while
} // 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;

//Now Swainson Hawks!
double  S_Hawk_total(0),max_S_Hawk(0),min_S_Hawk(0),max_S_Hawk_day(0),min_S_Hawk_day(0);
while (!birds.eof()) // While not at the end of the file read and accumulate information
{
num_data_pts++;
 S_Hawk_total += S_Hawk_given;
if (num_data_pts == 1)
{
max_S_Hawk = min_S_Hawk = S_Hawk_given;
}
sum += S_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;
}
report << " The amount of Swainson Hawks is " << " " << S_Hawk_total<< endl;//out to report
cout << " The amount of Swainson Hawks is" << " " << S_Hawk_total << endl;//echo to screen
birds >> time >> S_Hawk_given; // input next go back to while
} // end while
// Set format flags.
report.setf(ios::fixed);
report.setf(ios::showpoint);
report.precision(2);
// Print summary information to report file again
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;
// Close file and exit program.
report.close();
birds.close();
return 0;
} //end main 
Last edited on
First, please use proper indentation. Your code is 100 times harder to read without it. Indent the contents within functions, if statements, loops, etc.

cin >> birds

cin is the standard input stream.
birds is an input file stream.

Doing cin >> birds doesn't make sense. If birds were a string or an int or something, then you'd be putting the content the user inputs into the variable.
Last edited on
cin of a file object certainly won't work.

try
cin >> filename;
instead of
cin >> birds
I did try using filename, but now it is saying that it is not declared in this scope. What else should I change?
You never declared any variable called filename. Perhaps you meant to put string filename; before line 14.
Last edited on
I have updated my code, but now it is telling me that on line 92, if (max_S_Hawk > max), is incorrect.
Let's back up a bit. This is not how you should be learning. You need to start with a small amount of code, then you try compile + run it. Then, you build up the code from there. Bit by bit, compile early and often. As a beginner, you shouldn't be writing 100 lines of code at once without trying to compile it.

it is telling me that on line 92, if (max_S_Hawk > max), is incorrect.
The compiler has never given me an error message with the word "incorrect" in it, so I take you're paraphrasing. But unless you understand what the compiler is saying in the first place, you are simply losing information that you could be giving us. Instead of giving your interpretation of the error message, why not tell us what the exact error message is? You should be able to copy and paste it. We can help you interpret it.
Last edited on
I have been building up this code based on a template and help from others, however it has recently been giving me trouble. I have figured out the issue by putting "max" in the double section. Now the only problem is getting it to output my calculations properly, which is in a different question I have posted recently.
Topic archived. No new replies allowed.