any help??

Pages: 12
Oct 2, 2013 at 2:38pm
Display the number of "the" in a text file
Oct 2, 2013 at 2:47pm
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.
Oct 2, 2013 at 2:48pm
closed account (o3hC5Di1)
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 Oct 2, 2013 at 3:17pm
Oct 2, 2013 at 2:52pm
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
Oct 2, 2013 at 2:52pm
@NwN


Yes, it was silly of me to compare character by character rather than word by word. Thanks for stating that particularly.
Oct 2, 2013 at 3:02pm
#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;

}
Oct 2, 2013 at 3:04pm
Well "the" and "THE" are different, aren't they? Try "the" in your code.
Last edited on Oct 2, 2013 at 3:04pm
Oct 2, 2013 at 3:05pm
but in my text i have used "THE" and still i am getting the number of "THE'S' as
0
Last edited on Oct 2, 2013 at 3:05pm
Oct 2, 2013 at 3:11pm
but still the answer is 0
is my code correct????
Oct 2, 2013 at 3:14pm
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 Oct 2, 2013 at 3:15pm
Oct 2, 2013 at 3:16pm
so now what should i do???
Oct 2, 2013 at 3:17pm
closed account (o3hC5Di1)
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 Oct 2, 2013 at 3:18pm
Oct 2, 2013 at 3:20pm
@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 Oct 2, 2013 at 3:20pm
Oct 2, 2013 at 3:22pm
nope
still not getting it and now even getting 3 errors

Oct 2, 2013 at 3:22pm
closed account (o3hC5Di1)
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
Oct 2, 2013 at 3:23pm
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.

Oct 2, 2013 at 3:23pm
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.
Oct 2, 2013 at 3:32pm
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???
Oct 2, 2013 at 3:33pm
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 Oct 2, 2013 at 3:40pm
Oct 2, 2013 at 3:37pm
closed account (o3hC5Di1)
@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