any help??

Pages: 12
Display the number of "the" in a text file
Please don't ask us to do your homework. If you have tried, then show your code and we'll help.

I'll give a clue:

first create a file with some text(remember to use "the").
second, open it using an ofstream or ostream object.
third, use get() get the contents of the file byte by byte.
fourth, compare the obtained values with 't', 'h' and 'e' one by one in a if statements.
Hi there,

Please be more specific as to what you are having difficulties with. Is it opening the file, counting the words? Do you have any code so far?

If you need help getting started:

You need to create a string which will temporarily hold the words of the file as well as a counter variable. Then, open the file and read word for word into the temporary string until the end of the file. While doing so, check if the word is "the" and if so, increment the counter.

That actually holds a lot of hints in there. Please do let us know if you require any further help.

Edit: Using std::ifstream::operator>>() you can read in word for word, rather than byte for byte as The illusionist mirage suggested.

All the best,
NwN
Last edited on
as u know i can't copy from my program itself and i need to copy from notepad but even i am not able to get the notepad file . if u can solve this out i will show my code
@NwN


Yes, it was silly of me to compare character by character rather than word by word. Thanks for stating that particularly.
#include<fstream.h>
#include<string.h>
#include<process.h>
void main()
{
ifstream fin;
fin.open("inferno.txt",ios::in);
char s[75];int count=0;
while(!fin.eof())
{ fin >> s;
if(strcmp(s," THE ")==0)

count++;

}
fin.close();
cout<<count;

}
Well "the" and "THE" are different, aren't they? Try "the" in your code.
Last edited on
but in my text i have used "THE" and still i am getting the number of "THE'S' as
0
Last edited on
but still the answer is 0
is my code correct????
Your are doing:
fin >> s;

which probably is reading a whole string rather than just "THE". SO s is not a word but a whole string and a total string with other words is no equal to "THE". Maybe there's the issue.
Last edited on
so now what should i do???
Hi there,

You are comparing against " THE ", but the spaces are omitted when you read in the words.

I suggest you use:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include<fstream>
#include<string>
#include<process.h>

void main()
{
ifstream fin;
fin.open("inferno.txt",ios::in);
char s[75];
std::string s;
int count=0;
while(!fin.eof())
{ fin >> s;
if(strcmp(s," THE ")==0)

if (s.compare("THE") == 0 || s.compare("the") == 0)

count++;

}
fin.close();
cout<<count;

}


Hope that helps.

All the best,
NwN
Last edited on
@NwN

If you haven't noticed, this guy has the return type of main as void, which suggests he isn't ANSI compatible, so he can't use std::string(he maybe still using Turbo or Borland).
Last edited on
nope
still not getting it and now even getting 3 errors

If he can't use std::string, why is he including <string.h>?
Not because of his own implementation of c-strings, because he used a regular one as far as I could tell.

All the best,
NwN
If you haven't noticed, this guy has the return type of main as void, which suggests he isn't ANSI compatible


it doesnt suggest that at all.

You WILL get an error cause you're no doubt using Turbo, which is pre ANSI and hasn't got std::string which NwN suggested.
now let us leave that
i need to display all the lines which are starting with 'p'..........can somebody give hint how to make it???
Ok, here you are. Now its working:
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
#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 == 't')
		{
			fin.get(ch);
			if(ch == 'h')
			{
				fin.get(ch);
				if(ch == 'e')
					count++;
			}
		}
	}
	
	cout << count << endl;
	
	getch();
}


And make sure you have inferno.txt in your directory.

NOTE: The above code is for pre ANSI users only
Last edited on
@The illusionist: Your code will also count for "theosaurus", you'd have to check on blanks before and after :)

All the best,
NwN
Pages: 12