-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyProg02.cpp
More file actions
46 lines (40 loc) · 934 Bytes
/
Copy pathmyProg02.cpp
File metadata and controls
46 lines (40 loc) · 934 Bytes
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
// myProg02.cpp
// Mason Corey, CS 16 Spring 2018
#include <iostream> // for printf()
#include <cstdlib> // for exit(), perror()
#include <fstream> // for ifstream
using namespace std;
int main(int argc, char *argv[])
{
if (argc!=2) {
// if argc is not 2, print an error message and exit
cerr << "Usage: "<< argv[0] << " inputFile" << endl;
exit(1); // defined in cstdlib
}
ifstream ifs;
ifs.open(argv[1]);
int num_Ducks = 0;
int num_Animals = 0;
int num_Non_Ducks = 0;
string line;
if (ifs.good())
{
while(ifs.good()) {
getline(ifs, line);
if(ifs.eof()==1) {
break;
}
if (line=="duck") {
num_Ducks++;
}
if (line!="duck") {
num_Non_Ducks++;
}
num_Animals++;
}
cout << "Report for " << argv[1] << ":\n Animal count: " << num_Animals
<< "\n Duck count: " << num_Ducks << "\n Non duck count: " << num_Non_Ducks << endl;
ifs.close();
return 0;
}
}