any help??

Pages: 12
the function 'getchar' should have a prototype
> the return type of main as void, ... , so he can't use std::string
> You WILL get an error cause you're no doubt using Turbo

Or Visual Studio 2012. http://msdn.microsoft.com/en-us/library/6wd819wh.aspx

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
#include <iostream>
#include <fstream>
#include <string>
#include <cctype>

int main()
{
    const char* const path = "inferno.txt" ;
    const char* 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/tolower

        if( 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'.

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
#include <iostream.h>
#include <fstream.h>
#include <conio.h>

void main()
{
	ifstream fin;
	fin.open("inferno.txt", ios::in);

	if(!fin)
	{
		cout << "No such file in directory!" << endl;
		getchar();
		return -1;
	}

	char ch;
	int count = 0;
	while(fin.get(ch))
	{
		if(ch == '\n')
		{
			fin.get(ch);
			if(ch == 'p')
				count++;
		}
	}
	
	cout << count << endl;
	
	getch();
}


NOTE: The above code is for pre ANSI users only
Last edited on
leave it ........i am quitting
> 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'.

If you can't help, just shut up and get out.
I am sorry that I said 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(I've posted two full codes for his convenience).

Again SORRY for such comments and this won't happen again.
I apologize.
> 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.
Topic archived. No new replies allowed.
Pages: 12