-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcountDucks.cpp
More file actions
38 lines (33 loc) · 734 Bytes
/
Copy pathcountDucks.cpp
File metadata and controls
38 lines (33 loc) · 734 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
// countDucks.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;
string line;
if (ifs.good())
{
while(ifs.good()) {
getline(ifs, line);
if(ifs.eof()==1) {
break;
}
if (line=="duck") {
num_Ducks++;
}
}
cout << "There were " << num_Ducks << " ducks in " << argv[1] << "\n" << endl;
ifs.close();
return 0;
}
}