Little bit of help pls

Hi,

I dont know where is my error, after i put my word nothing is happning...

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
  //User enters 2 character sequences(each doesn’t exceed 100 characters).
//Print out how many times a word from the second sequence repeats in the first.
//For example, “My name is Bill.I am a programmer.” and „am”, the output will be 3. 
//For “My name is Bill.I am a programmer.” and „name”, the output will be 1.

using namespace std;

int main()
{
	int i = 0;
	int j = 0;
	char*a = NULL;
	char*b = NULL;
	a = new char[100];
	b = new char[100];
	cout << "Enter the first character sequence: " << endl;
	cin.getline(a,100);
	//cin >> a;
	cout << "Enter the word that you are looking for :" << endl;
	cin.getline(b,100);
	//cin >> b;
	int counter = 0;
	int counter2 = 0;
	string word="";
	string worduser = "";

	for (int p = 0; b[p]=! '/0'; j++)
	{
		worduser = worduser + b[p];
	}
	for (int o = 0; a[o]!= '/0'; i++)
	{
			if (a[o] >= 'a' && a[o] <= 'z' || a[o] >= 'A' && a[o] <= 'Z')
			{
				word = word + a[o];
				if (worduser == word)
				{
					counter2 = counter2 + 1;
				}
				else { word = ""; }
			}
	}
	cout << "Your word is" << counter2 << "in the text"<< endl;
	system("pause");
	return 0;
}
this section:
1
2
3
4
5
6
7
8
9
10
11
12
	for (int o = 0; a[o]!= '/0'; i++)
	{
			if (a[o] >= 'a' && a[o] <= 'z' || a[o] >= 'A' && a[o] <= 'Z')
			{
				word = word + a[o];
				if (worduser == word)
				{
					counter2 = counter2 + 1;
				}
				else { word = ""; }
			}
	}


puts you in an infinite loop.

a[o]
it looks like yo are using a letter to index your array??
Last edited on
it seems that you are confuse with using loops

http://www.cplusplus.com/doc/tutorial/control/
you can read this, this will help you understand it
I have corrected but the same result ...
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

#include <stdio.h> 
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream> 
#include <math.h>

//User enters 2 character sequences(each doesn’t exceed 100 characters).
//Print out how many times a word from the second sequence repeats in the first.
//For example, “My name is Bill.I am a programmer.” and „am”, the output will be 3. 
//For “My name is Bill.I am a programmer.” and „name”, the output will be 1.

using namespace std;

int main()
{
	int i = 0;
	int j = 0;
	char*a = NULL;
	char*b = NULL;
	a = new char[100];
	b = new char[100];
	cout << "Enter the first character sequence: " << endl;
	cin.getline(a,100);
	cout << "Enter the word that you are looking for :" << endl;
	cin.getline(b, 100);
	int counter = 0;
	int counter2 = 0;
	string word="";
	string worduser = "";

	for (int p = 0; b[p]!= '/0'; p++)
	{
		worduser = worduser + b[p];
	}
	for (int o = 0; a[o]!= '/0'; o++)
	{
			if (a[o] >= 'a' && a[o] <= 'z' || a[o] >= 'A' && a[o] <= 'Z')
			{
				word = word + a[o];
				if (worduser == word)
				{
					counter2 = counter2 + 1;
				}
				else { word = ""; }
			}
	}
	cout << "Your word is" << counter2 << "in the text"<< endl;
	system("pause");
	return 0;
}
Last edited on
Nothing happens? For me, your program crashes.

'/0' on lines 33 and 37 should be '\0' (i.e. backslash not forward slash.)

With this fix your program runs for me but fails to find a match.

To help yourself, add diagnostic code, e.g.

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
#include <stdio.h> 
#include <vector>
#include <algorithm>
#include <functional>
#include <iostream> 
#include <math.h>

//User enters 2 character sequences(each doesn't exceed 100 characters).
//Print out how many times a word from the second sequence repeats in the first.
//For example, "My name is Bill.I am a programmer." and "am", the output will be 3. 
//For "My name is Bill.I am a programmer." and "name", the output will be 1.

using namespace std;

int main()
{
	int i = 0;
	int j = 0;
	char*a = NULL;
	char*b = NULL;
	a = new char[100];
	b = new char[100];
	cout << "Enter the first character sequence: " << endl;
	cin.getline(a,100);
	cout << "Enter the word that you are looking for :" << endl;
	cin.getline(b, 100);
	int counter = 0;
	int counter2 = 0;
	string word="";
	string worduser = "";
 
	for (int p = 0; b[p]!= '\0'; p++)
	{
		worduser = worduser + b[p];
	}
	cout << "worduser = " << worduser << endl;
	for (int o = 0; a[o]!= '\0'; o++)
	{
			if (a[o] >= 'a' && a[o] <= 'z' || a[o] >= 'A' && a[o] <= 'Z')
			{
				word = word + a[o];
				cout << "test word = " << word << endl;
				if (worduser == word)
				{
					cout << "match!" << endl;
					counter2 = counter2 + 1;
				}
				else
				{
					cout << "reset word to \"\"" << endl;
					word = ""; }
			}
	}
	cout << "Your word is" << counter2 << "in the text"<< endl;
	system("pause");
	return 0;
}


Enter the first character sequence:
one two three
Enter the word that you are looking for :
two
worduser = two
test word = o
reset word to ""
test word = n
reset word to ""
test word = e
reset word to ""
test word = t
reset word to ""
test word = w
reset word to ""
test word = o
reset word to ""
test word = t
reset word to ""
test word = h
reset word to ""
test word = r
reset word to ""
test word = e
reset word to ""
test word = e
reset word to ""
Your word is0in the text


Andy

PS I think you need to think a bit more about what's going on around lines 41-46

Even if you fix the over keen resetting of word, it might still not behave quite right. e.g. for the string "That green pen belongs to Tux the penguin" and searching for "pen" it would prob match the pen at the start of penguin rather than the pen towards the start of the sentence.

PPS Also...
1. the memory you're allocating on lines 22 and 23 is leaking (you could prob just use a buffer rather than new-ing (and deleting!!) the storage?)
2. with the help of standard routines you could reduce your code up a bit? e.g.
http://www.cplusplus.com/reference/cctype/isalpha/
3. You should kill your unused variables
Last edited on
Hi,

Thanks a lot, im on it

:-)

Also, this

25
26
27
28
29
30
	string worduser = "";

	for (int p = 0; b[p]!= '\0'; p++)
	{
		worduser = worduser + b[p];
	}


does exactly the same as

string worduser = b;

And

1
2
3
4
5
6
7
8
	char* b = NULL;
	b = new char[100];
	// prompt user for input here!
	cin.getline(b, 100);
	string worduser = "";
	worduser = b;
	// use string...
	delete [] b; // remember to delete using []  


is a bit better written as

1
2
3
4
5
6
	char* b = new char[100]; // better to assign on same line as you declare variable
	// prompt user for input here!
	cin.getline(b, 100);
	string worduser = b; // ditto
	// use string...
	delete [] b;


but as you are new-ing and deleting a small buffer within the same function, a buffer would be better

1
2
3
4
5
	char b[100]; // now a buffer so don't need to remember to delete
	// prompt user for input here!
	cin.getline(b, 100);
	string worduser = b; // ditto
	// use string... 


finally, there's no need for the buffer as you can read into the string directly like this

1
2
3
4
	// prompt user for input here!
	string worduser; // no need for = "" as string's default constructor already does this
	getline(cin, worduser); // use getline function rather than method
	// use string... 


Andy
Topic archived. No new replies allowed.