#include <iostream>
#include <fstream>
#include <string>
#include <cctype>
int main()
{
constchar* const path = "inferno.txt" ;
constchar* const the_word = "the" ;
// the constructor opens the file, and the destructor will close it
std::ifstream fin(path) ;
int count = 0 ;
// you have #include <string>, why not use std::string
std::string this_word ;
// while(!fin.eof()) // this is a wrong idiom
// instead, check for success *after* the attempted input
while( fin >> this_word )
{
// convert this word to all lower case
for( char& c : this_word ) // for each character in this word
c = std::tolower(c) ; // http://en.cppreference.com/w/cpp/string/byte/tolowerif( this_word == the_word ) // std::string is EqualityComparable
++count ;
}
std::cout << count << '\n' ;
}
As for your second doubt, its easy. Just check if a character is the newline character('\n') and if it is, then input from file again the next character to check if it's 'p'.
> Yea, and it looks like he's not gonna update his compiler anytime soon.
> Maybe because his institution still insists Turbo or something like that.
You have absolutely no evidence to conclude either that hkrishnan81 is not using a current compiler or that if he were using one, he would be averse to moving to a later version. Or for that matter, that his institution 'still insists Turbo or something like that'.
> But I didn't really meant to hurt anyone or be offensive. I'll try to be more careful next time before posting.
> But I did try to help him
Fair enough.
Newbies making a genuine effort to learn C++, and coming here expecting help, need all the encouragement and support that they can get. They need to be treated differently from forum veterans.