string::find() skips parts of string?.....

Does string::find() skip parts of a string inclosed in quotation marks?

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
void keyfind(string phrase,int startpoint=0){
//cout<<phrase<<' '<<startpoint<<endl;
int pos=0;
string p2;
p2=phrase;
bool g;
int i;

for (i=27;i>0;i--){
phrase=shift(i,p2);
pos=phrase.find(32);
//cout<<phrase<<endl;
g=isword(phrase.substr(0,pos));
if(g) {cout <<phrase.substr(0,pos)<<' ';break;}
}
if (pos==string::npos) return;
phrase.erase(0,pos+1);
startpoint++;
if (i!=27) {
tuple t;
t.x=startpoint-1;
t.y=i;
cout<<(int)t.x<<' '<<(int)t.y<<endl;
MKEY.push_back(t);
keyfind(phrase,startpoint+pos);
}
else {
keyfind(phrase,startpoint+pos);
}
}


that is the part of my code giving me strife. In the output I notice that there are skips like so:

1
2
3
4
5
6
save 367 24
himself. 372 2
381 0
440 0
my 452 2
details. 455 13


what this code is supposed to do is described in this file http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00sc-introduction-to-computer-science-and-programming-spring-2011/unit-2/lecture-10-hashing-and-classes/MIT6_00SCS11_ps4.pdf it is the last part of the problem set
The standard string find algorithm does not know anything about your string; it will find the first instance in the string of whatever you want. "Quotes" and the like mean nothing.

Without doing your assignment or trying to figure out everything that your code is supposed to do, it is difficult to understand what difficulty you are having.

Don't search on ASCII numbers. Use it properly:
pos=phrase.find(' ');

Are you trying to split the phrase on whitespaces?

[edit] BTW, it looks like you are supposed to be coding in Python

[edit 2] Where can I find a copy of the files your professor gave you: "ps4.py", "words.txt", and "fable.txt"?
Last edited on
Thanks.
yea im suppose to be doing this in python but I'm just doing self study so I decided to do it in C++ as thats what I use most of the time.

the problem set files are at the bottom of that page http://ocw.mit.edu/courses/electrical-engineering-and-computer-science/6-00sc-introduction-to-computer-science-and-programming-spring-2011/unit-2/lecture-10-hashing-and-classes/ EDIT: its in Code Files (ZIP)

that function is suppose to store a key in a global vector called MKEY with x being the character position and y being the shift amount. All the "cout"s in that function are for debugging purposes. i can't seem to figure out why the it seems to skip some words like shown in my output example.
Last edited on
Topic archived. No new replies allowed.