Strcmp for fstream

I am trying to write a program that counts the number of 'the' and 'is' in a file. Why is it that my code won't work when I use
1
2
3
4
5
6
7
8
9
10
11
12
13
14
                if (strcmpi(a, "is"))
			c++;
		if (strcmpi(a, "the"))
			c++;
[\code]

but works when I use

[code]

if (!strcmpi(a, "is"))
			c++;
		if (!strcmpi(a, "the"))
			c++;


whole code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
  int main() {
	int c = 0;
	char a[1000];
	ofstream filout("hanzo", ios::out);
	cout << "Enter paragraph";
	gets_s(a);
	filout << a;
	filout.close();
	ifstream filin("hanzo", ios::in);
	filin.seekg(0);
	while (!filin.eof()) {
		filin >> a;
		if (!strcmpi(a, "is"))
			c++;
		if (!strcmpi(a, "the"))
			c++;
	}
	cout << "\n\n number of the and is are:" << c << endl;
	filin.close();
	return 0;
}



Last edited on
I don't look closely at your code, but I think strcmpi returns 0 when two strings are equal.

1
2
if (!strcmpi(a, "is")) c++;
if (!strcmpi(a, "the")) c++;


You can write like this :
1
2
if (strcmpi(a, "is") == 0) c++;
if (strcmpi(a, "the") == 0) c++;

You are writing a C++ program; so use C++ facilities: in this case std::string
https://cal-linux.com/tutorials/strings.html

Something along hese lines, perhaps:
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
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>

bool write_paragraph_to( std::string file_name ) {

    std::ofstream file(file_name) ; // open for output; overwrite if existing

    std::cout << "enter paragraph line by line\n"
              << "terminate input with eof (ctrl+D on unix, ctrl+Z on windows)\n" ;

    std::string line ;
    // http://www.cplusplus.com/reference/string/string/getline/
    while( std::getline( std::cin, line ) ) file << line << '\n' ;

    return file.good() ;
}

std::string to_lower( std::string str ) { // convert to all lower case

    // http://www.stroustrup.com/C++11FAQ.html#for
    // http://en.cppreference.com/w/cpp/string/byte/tolower
    for( char& c : str ) c = std::tolower(c) ;
    return str ;
}

bool cmpi( std::string a, std::string b ) { // compare for equality ignoring case

    return to_lower(a) == to_lower(b) ;
}

bool print_counts( std::string file_name ) { // print counts of "is" and "the"

    std::ifstream file(file_name) ; // open for input

    if( !file.is_open() ) return false ;

    int is_cnt = 0 ;
    int the_cnt = 0 ;

    std::string str ;
    while( file >> str ) { // for each string read from the file

        // TO DO: take care of punctuation eg. 'is: ' in "the reason is: blah"
        if( cmpi( str, "is" ) ) ++is_cnt ;
        else if( cmpi( str, "the" ) ) ++the_cnt ;
    }

    std::cout << "'is' occurred " << is_cnt << " times.\n"
              << "'the' occurred " << the_cnt << " times.\n"
              << "total: " << is_cnt + the_cnt << " times.\n" ;

    return true ;
}

int main() {

    const std::string file_name = "hanzo" ;

    if( write_paragraph_to(file_name) ) print_counts(file_name) ;
    else std::cerr << "could not open file for output\n" ;
}
Topic archived. No new replies allowed.